SET NOCOUNT ON;
DECLARE @sql NVARCHAR(MAX),
@servername NVARCHAR(255)
SET @servername = '[s-printstream]'
--Drop Table #Actual
CREATE TABLE #Actual
(
jobnumber INT,
firstNameCounts VARCHAR(25),
lastNameCounts VARCHAR(25),
address1Counts VARCHAR(25),
address2Counts VARCHAR(25),
cityCounts VARCHAR(25),
stateCounts VARCHAR(25),
zipCounts VARCHAR(25),
inHomeDateCounts VARCHAR(25)
)
SET @sql = 'INSERT INTO #actual (jobnumber,firstNameCounts,lastNameCounts , address1Counts, address2Counts, cityCounts, stateCounts, zipCounts, inHomeDateCounts) '
SET @sql = @sql + ' Select s.jobnumber, count(s.firstName) AS [firstNameCounts], Count (s.lastName) AS [lastNameCounts], Count (s.Address1) As [address1Counts], Count (s.address2)-Count (address2) AS '
SET @sql = @sql + ' [address2Counts], Count (s.City) AS [cityCounts], Count (s.State) AS [stateCounts], Count (s.Zip) AS [zipCounts], Count (jb.inHomeDate) AS [inHomeDateCounts] '
SET @sql = @sql + ' From' + ' @servername ' + '.[tdis_417133]' + '.[dbo].[tblStandardFinal] s '
SET @sql = @sql + ' INNER JOIN [s-printstream].[tdSchedule2].[dbo].[tblJobTicketActions] jb '
SET @sql = @sql + ' ON jb.psFlagJobNumber = s.jobNumber '
SET @sql = @sql + ' where jobNumber = @jobNumber '
SET @sql = @sql + ' group by jobNumber '
PRINT @SQL
EXEC sp_executesql @sql
,N'@JobNumber Varchar(25)'
,@JobNumber = 417133
有谁可以帮我找出如何动态传递我的服务器名称以及数据库名称?当我尝试这样做时,我收到了这个错误:
Msg 137,Level 15,State 2,Line 1
必须声明标量变量" @ servername"
感谢任何帮助。
由于
答案 0 :(得分:1)
或将@servername值传递给sql命令
EXEC sp_executesql @sql
,N'@JobNumber Varchar(25),@servername nvarchar(255)'
,@JobNumber = 417133,@servername=@servername