2

I have a sql script say "abc.sql" which I am calling from a batch file using sqlcmd like

  1. Calling batch script like

    script.bat arg1 arg2
    
  2. In batch

    param1=%1
    param2=%2
    

Then calling SQL script like

sqlcmd -S server -i abc.sql -v var1=%param1% var2=%param2%
  1. In SQL script

    DECLARE @node as sysname;
    DECLARE @serv as sysname;
    
    SET @node = $(var1);
    SET @serv = $(var2);
    

But this giving erro while I call the batch script "Invalid column name 'XXXXXXXX'"

where 'XXXXXXX' is the value of var1. What I am doing wrong. any idea.

Again, even though the scalar variable @node and @serv is declared and set ... I get an error saying Must declare the scalar variable "@node"

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • Guys, there is another issue: I edited my question ... please let me know if I have to put it in another post – Rahul Nov 23 '11 at 13:40

1 Answers1

4

You need to include quotes in your script:

    SET @node = '$(var1)';
    SET @serv = '$(var2)';
Filip Popović
  • 2,637
  • 3
  • 18
  • 18
  • Fil, I edited my question with another issue ... if you have any idea pls do let me know. – Rahul Nov 23 '11 at 13:43
  • @Rahul, I've tested the same scenario on SQL Server 2008 R2 and it worked. Please check whether You included whole script in abc.sql, maybe You ommited DECLARE or have some typo. If not, please edit your question with full script so I can test it on my environment. – Filip Popović Nov 23 '11 at 13:49