0
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
データベース名だけでなくサーバー名も動的に渡す方法はありますか?私が試してみると、私はこのエラーになります:SQL Serverでサーバー名を動的に渡す方法
Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "@servername"
何か助けていただければ幸いです。
おかげ
を@サーバ名値を渡します。 '+ '@servername' +'は '+ @servername +'でなければなりません。 –