2

I have the following query which I am running using a batch file. In the batch file I use the following syntax:

echo populating Application table with values...

SET "installFile=%sqlDir%\Install\DataFiles\Insert_ApplicationNames.sql"

OSQL /n /i "%installFile%" /d%db% /U%user% /P%pswd% /S%serv%
echo
echo populated Application table with values in Insert_ApplicationNames.sql
echo

The sql shown below runs without any errors when executed from the SQL Management Studio, but it keeps erroring out when run as a part of the batch script. Could some one help me find what I may be doing wrong here?

Also, the rows do get inserted, but our nightly QA install breaks because of the error thrown by the batch script.

IF NOT EXISTS(SELECT * FROM Application WHERE name = '')
BEGIN
    INSERT INTO Application
    (Name)
    VALUES
    ('')
END
GO
IF NOT EXISTS(SELECT * FROM Application WHERE name = 'App1.exe')
BEGIN
    INSERT INTO Application
    (Name)
    VALUES
    ('App1.exe')
END
GO
IF NOT EXISTS(SELECT * FROM Application WHERE name = 'App2.exe')
BEGIN
    INSERT INTO Application
    (Name)
    VALUES
    ('App2.exe')
END
abatishchev
  • 98,240
  • 88
  • 296
  • 433
EndlessSpace
  • 1,320
  • 2
  • 24
  • 46

2 Answers2

2

GO is the (default) batch separator keyword in Management Studio, but it isn't a real SQL keyword (i.e., SQL Server doesn't recognize it).

Remove those from your script -- in the script you've provided, they are irrelevant anyway -- and you should be good to, um, go.

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
0

Curious whether your variables should be right up against the switches. Try this?

OSQL -n -i "%installFile%" -d %db% -U %user% -P %pswd% -S %serv%

What happens when you use the line above with your known good values right in the command?

OSQL -n -i "C:\foo.sql" -d MyDB -U MyUser -P MyPwd -S MyServ
p.campbell
  • 98,673
  • 67
  • 256
  • 322
  • Tried both your suggestions, but still the same error. Also, many other sql scripts are being executed along with this using the same syntax and all of them execute correctly – EndlessSpace May 06 '11 at 19:01