I know that we have a lot of answers for this problem but I still don't understand what really the cause of this error.
I've created a stored procedure like this.
CREATE PROCEDURE [dbo].[storedProc_dataPull]
@serverName nvarchar(30),
@dbName nvarchar (30),
@tblName nvarchar(30)
AS BEGIN
DECLARE @sql nvarchar(500)
DECLARE @ds nvarchar(500)
SET @ds = 'Data Source=phmnldb16\eaudit;user id=YYYY;password=XXXX'
SET @sql = 'SELECT @serverName, @dbName, sdb1.* from
OPENDATASOURCE(''SQLOLEDB'', '+Char(39)+ @ds +Char(39)+ ').AUDIT_FSA_170_001.AUD170.Workflow sdb1)'
INSERT INTO sampleDatabase.dbo.WorkFlowCopy
([ServerName]
,[DBName]
,[ID]
,[ActivityDefinitionID]
,[ParentID]
,[Caption]
,[Description]
,[ShortDescription]
,[Name]
,[Order]
,[ReferenceNumber]
,[ShowOnNavigation]
,[Status]
,[InUseBy])
EXEC (@sql)
And when I tried to execute it..
EXEC storedProc_dataPull 'serverName', 'dbName', 'tblName'
I always got this error:
Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "serverName"