Search Suggest

SQL Server, How to execute T-SQL commands from the command line (attention this post may contain easy but useful tips ๐Ÿ˜ )

Hello friends,

You know, i love “useful but fast & easy” tips!

The question of today:
Have you ever needed to run T-SQL commands from the command line? Yes?

So, How to do it?


Well, an easy way is to use the sqlcmd command.

But what is the SqlCmd?

The SqlCmd is a command line utility that is part of the standard installation of SQL Server and that you can run from the command prompt.



First you need to prepare a script that contains the T-SQL commands to execute.

In my example i saved myscript.sql in C:\TEMP folder.


Second, just open the command prompt


And type in:

sqlcmd -U "XX" -P "YYYY" -S "ZZZZ" -i “c:\temp\MyScript.sql”  


You need to specifiy only the login information using:

-U username

-P password

-S the server name

-i the location of your script

 

 
 
 

Et voilร ! 

That's all for today!
Luca












Previous post:SQL Server, How to Read a list of files in a folder and compare its name with the value stored into a columns

 

Post a Comment