Home All Groups Group Topic Archive Search About

Can't get SQLCMD to run from a .bat file

Author
25 Sep 2007 10:35 PM
Carol Cox
Using the Microsoft SQLCMD utility (on SQL Server 2005 Standard Edition), I
can run the .sql script I created from a Windows command prompt, passing to
it a variable name for the database I am creating:

sqlcmd -i c:\createdbandtables.sql -v db=newdbname

However, I cannot get the sqlcmd code above to run if I put it in a .bat
file and double click on the file.  (All files and the database are located
on the same server.) The code simply does not run. Are there other commands
that I need to put in the .bat file besides the line above?

Thanks in advance for any help you can provide.

Carol

Author
26 Sep 2007 7:08 AM
Tibor Karaszi
I've never had any problems running sqlcmd from a bat file. Can you post a repro so we can test?

Show quote
"Carol Cox" <inasoluti***@msn.com> wrote in message
news:C86A7B2B-8798-422C-BAA8-4D410F424776@microsoft.com...
> Using the Microsoft SQLCMD utility (on SQL Server 2005 Standard Edition), I can run the .sql
> script I created from a Windows command prompt, passing to it a variable name for the database I
> am creating:
>
> sqlcmd -i c:\createdbandtables.sql -v db=newdbname
>
> However, I cannot get the sqlcmd code above to run if I put it in a .bat file and double click on
> the file.  (All files and the database are located on the same server.) The code simply does not
> run. Are there other commands that I need to put in the .bat file besides the line above?
>
> Thanks in advance for any help you can provide.
>
> Carol
>
Author
26 Sep 2007 10:41 AM
bass_player [SBS-MVP]
Have you tried explicitly including the path which ocntains the sqlcmd.exe
file? Sometimes, this works.
SET PATH=<path location of sqlcmd.exe>

sqlcmd -i c:\createdbandtables.sql -v db=newdbname

Show quote
"Tibor Karaszi" <tibor_please.no.email_kara***@hotmail.nomail.com> wrote in
message news:26A2AD62-A4E1-499A-A02B-F0D1AC38CF0C@microsoft.com...
> I've never had any problems running sqlcmd from a bat file. Can you post a
> repro so we can test?
>
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://sqlblog.com/blogs/tibor_karaszi
>
>
> "Carol Cox" <inasoluti***@msn.com> wrote in message
> news:C86A7B2B-8798-422C-BAA8-4D410F424776@microsoft.com...
>> Using the Microsoft SQLCMD utility (on SQL Server 2005 Standard Edition),
>> I can run the .sql script I created from a Windows command prompt,
>> passing to it a variable name for the database I am creating:
>>
>> sqlcmd -i c:\createdbandtables.sql -v db=newdbname
>>
>> However, I cannot get the sqlcmd code above to run if I put it in a .bat
>> file and double click on the file.  (All files and the database are
>> located on the same server.) The code simply does not run. Are there
>> other commands that I need to put in the .bat file besides the line
>> above?
>>
>> Thanks in advance for any help you can provide.
>>
>> Carol
>>
>
Author
27 Sep 2007 1:37 AM
Carol Cox
Thanks for the replies. Unfortunately, I tried the SET PATH and the same
result - the code does not run.

This is the exact line that is in the file I created using Notepad that I
have called "runsqlcmd.bat" (I have attached it here.)

SETPATH="C:\Program Files\Microsoft SQL Server\90\Tools\Binn\sqlcmd.exe" -i
C:\createdbandtables.sql -o C:\OutputFile.txt -v db=newdatabase

OR

sqlcmd -i C:\createdbandtables.sql -o C:\OutputFile.txt -v db=newdatabase

Also attached is the .sql script.

Thanks for your help - this is driving me up the wall!!!

Carol



Show quote
"bass_player [SBS-MVP]" <bass_pla***@mvps.org> wrote in message
news:O9ovJnCAIHA.3548@TK2MSFTNGP06.phx.gbl...
> Have you tried explicitly including the path which ocntains the sqlcmd.exe
> file? Sometimes, this works.
> SET PATH=<path location of sqlcmd.exe>
>
> sqlcmd -i c:\createdbandtables.sql -v db=newdbname
>
> "Tibor Karaszi" <tibor_please.no.email_kara***@hotmail.nomail.com> wrote
> in message news:26A2AD62-A4E1-499A-A02B-F0D1AC38CF0C@microsoft.com...
>> I've never had any problems running sqlcmd from a bat file. Can you post
>> a repro so we can test?
>>
>> --
>> Tibor Karaszi, SQL Server MVP
>> http://www.karaszi.com/sqlserver/default.asp
>> http://sqlblog.com/blogs/tibor_karaszi
>>
>>
>> "Carol Cox" <inasoluti***@msn.com> wrote in message
>> news:C86A7B2B-8798-422C-BAA8-4D410F424776@microsoft.com...
>>> Using the Microsoft SQLCMD utility (on SQL Server 2005 Standard
>>> Edition), I can run the .sql script I created from a Windows command
>>> prompt, passing to it a variable name for the database I am creating:
>>>
>>> sqlcmd -i c:\createdbandtables.sql -v db=newdbname
>>>
>>> However, I cannot get the sqlcmd code above to run if I put it in a .bat
>>> file and double click on the file.  (All files and the database are
>>> located on the same server.) The code simply does not run. Are there
>>> other commands that I need to put in the .bat file besides the line
>>> above?
>>>
>>> Thanks in advance for any help you can provide.
>>>
>>> Carol
>>>
>>
>
>
Author
27 Sep 2007 6:56 AM
Tibor Karaszi
The SETPATH version didn't fly for me, I don't know what that is supposed to do. But after adjusting
the path for the input file, the second version worked just fine. I suggest you add a pause command
to your bat file to see what errors the command interpreter gives you. The content of my bat file,
which works fine, is:

sqlcmd -i C:\Temp\x\createdbandtables.sql -o C:\OutputFile.txt -v db=newdatabase
pause


Also, make sure you check the output file for errors from SQL Server.

Show quote
"Carol Cox" <inasoluti***@msn.com> wrote in message
news:6EFABEDD-EEC5-4261-9C30-0879AAC739F2@microsoft.com...
> Thanks for the replies. Unfortunately, I tried the SET PATH and the same
> result - the code does not run.
>
> This is the exact line that is in the file I created using Notepad that I
> have called "runsqlcmd.bat" (I have attached it here.)
>
> SETPATH="C:\Program Files\Microsoft SQL Server\90\Tools\Binn\sqlcmd.exe" -i
> C:\createdbandtables.sql -o C:\OutputFile.txt -v db=newdatabase
>
> OR
>
> sqlcmd -i C:\createdbandtables.sql -o C:\OutputFile.txt -v db=newdatabase
>
> Also attached is the .sql script.
>
> Thanks for your help - this is driving me up the wall!!!
>
> Carol
>
>
>
> "bass_player [SBS-MVP]" <bass_pla***@mvps.org> wrote in message
> news:O9ovJnCAIHA.3548@TK2MSFTNGP06.phx.gbl...
>> Have you tried explicitly including the path which ocntains the sqlcmd.exe
>> file? Sometimes, this works.
>> SET PATH=<path location of sqlcmd.exe>
>>
>> sqlcmd -i c:\createdbandtables.sql -v db=newdbname
>>
>> "Tibor Karaszi" <tibor_please.no.email_kara***@hotmail.nomail.com> wrote
>> in message news:26A2AD62-A4E1-499A-A02B-F0D1AC38CF0C@microsoft.com...
>>> I've never had any problems running sqlcmd from a bat file. Can you post
>>> a repro so we can test?
>>>
>>> --
>>> Tibor Karaszi, SQL Server MVP
>>> http://www.karaszi.com/sqlserver/default.asp
>>> http://sqlblog.com/blogs/tibor_karaszi
>>>
>>>
>>> "Carol Cox" <inasoluti***@msn.com> wrote in message
>>> news:C86A7B2B-8798-422C-BAA8-4D410F424776@microsoft.com...
>>>> Using the Microsoft SQLCMD utility (on SQL Server 2005 Standard
>>>> Edition), I can run the .sql script I created from a Windows command
>>>> prompt, passing to it a variable name for the database I am creating:
>>>>
>>>> sqlcmd -i c:\createdbandtables.sql -v db=newdbname
>>>>
>>>> However, I cannot get the sqlcmd code above to run if I put it in a .bat
>>>> file and double click on the file.  (All files and the database are
>>>> located on the same server.) The code simply does not run. Are there
>>>> other commands that I need to put in the .bat file besides the line
>>>> above?
>>>>
>>>> Thanks in advance for any help you can provide.
>>>>
>>>> Carol
>>>>
>>>
>>
>>
>
Author
27 Sep 2007 9:07 AM
Razvan Socol
Carol Cox wrote:

> Thanks for the replies. Unfortunately, I tried the SET PATH and the
> same result - the code does not run.
>
> This is the exact line that is in the file I created using Notepad
> that I have called "runsqlcmd.bat" (I have attached it here.)
>
> SETPATH="C:\Program Files\Microsoft SQL
> Server\90\Tools\Binn\sqlcmd.exe" -i C:\createdbandtables.sql -o
> C:\OutputFile.txt -v db=newdatabase
>
> [...]

The SET PATH command is supposed to work like this:

SET PATH=%PATH%;C:\Program Files\Microsoft SQL Server\90\Tools\Binn

and then you issue your SQLCMD command:

sqlcmd.exe -i C:\createdbandtables.sql -o  C:\OutputFile.txt -v
db=newdatabase

You can also configure the PATH environment variable in My Computer /
Properties / Advanced / Environment Variables / System variables.

Alternatively, you can specify the full path of sqlcmd directly, like
this:

C:\Program Files\Microsoft SQL Server\90\Tools\Binn\sqlcmd.exe -i
C:\createdbandtables.sql -o  C:\OutputFile.txt -v db=newdatabase

Also, it would be a good idea to use the PAUSE command in the batch
file to see what error message appears. If it still doesn't work, post
that error message and/or the contents of OutputFile.txt.

--
Razvan Socol
SQL Server MVP

AddThis Social Bookmark Button