Jhe
Jhe

Reputation: 97

Must declare the scalar variable "@serverName"

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"

Upvotes: 3

Views: 1740

Answers (2)

Pரதீப்
Pரதீப்

Reputation: 93694

Use SP_EXECUTESQL to pass values to parameters inside dynamic sql

SET @sql = 'SELECT @serverName, @dbName, sdb1.* from
               OPENDATASOURCE(''SQLOLEDB'', '
           + Char(39) + @ds + Char(39)
           + ').AUDIT_FSA_170_001.AUD170.Workflow sdb1' -- unwanted close parenthesis

INSERT INTO sampleDatabase.dbo.WorkFlowCopy
            ([ServerName],
             [DBName],
             [ID],
             [ActivityDefinitionID],
             [ParentID],
             [Caption],
             [Description],
             [ShortDescription],
             [Name],
             [Order],
             [ReferenceNumber],
             [ShowOnNavigation],
             [Status],
             [InUseBy]) -- Missing close parenthesis
EXEC Sp_executesql
  @sql,
  N'@serverName nvarchar(30), @dbName nvarchar (30)',
  @serverName=@serverName,
  @dbName=@dbName 

Notes :

  • In Select list you have used ,* make sure the select list returns the same number of columns as Insert column list and in same order
  • There is a missing close parenthesis at the end of Insert column list and a unwanted close parenthesis at the end of OPENDATASOURCE query
  • @tblName input parameter is not used inside the procedure.

Upvotes: 3

Ash
Ash

Reputation: 6035

You're using @serverName as part of the string. Try

SET @sql = 'SELECT ' + @serverName + ','  + @dbName + ', sdb1.* from
           OPENDATASOURCE(''SQLOLEDB'','+Char(39)+ @ds + Char(39)+ ').AUDIT_FSA_170_001.AUD170.Workflow sdb1'

instead of

SET @sql = 'SELECT @serverName, @dbName, sdb1.* from
           OPENDATASOURCE(''SQLOLEDB'', '+Char(39)+ @ds +Char(39)+ ').AUDIT_FSA_170_001.AUD170.Workflow sdb1)'

I've had to remove an extra bracket at the end of @sql as well.

Upvotes: 1

Related Questions