question

DaroldRudolph avatar image
DaroldRudolph asked

How can I run a series of SQLCMD commands in a Batch Script

I am trying to run a SQLCMD statement in a batch. The statement runs fine in a command prompt, but I was to put is into a Batch scripts which will run several SQLCMD commands in sequence. These SQL command are simple update or select statements. The problem is these commands in batch will call up SQL Server Management Studio with the command statement asking for a logon. I am using SQL Server 2012 on Windows Server 2012 R2 with full Administration right, according to our system support personnel. I am working on a test system, so the command, batch and SQL Server are all located on the same machine. I am trying to replicate a process which works on Windows server 2003 when connecting to Oracle, with out much modifications.
sqlcmdbatch
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Wilfred van Dijk avatar image
Wilfred van Dijk answered
If you run `sqlcmd /?` you'll see there are various options to do an automated login: - use "-E" to do a trusted login - use "-U" and "-P" to specify a user To run multiple .SQL files against a server, add something like this in a batch file: for %%b in (*.sql) do sqlcmd -S{servername} -E -i%%b (replace {servername} with the correct name ...)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.