Click here to Skip to main content
15,887,214 members
Articles / Database Development / SQL Server
Tip/Trick

Tip: Run a Transact-SQL Script (.SQL File) From the Command Line

Rate me:
Please Sign up or sign in to vote.
4.75/5 (9 votes)
22 Dec 2010CPOL2 min read 82.9K   9   14
Provides the quick-and-dirty syntax to connect to a given Microsoft SQL Server instance so you can then run a .sql query script against it, e.g., if you are calling this from code as part of a larger pipeline
This tip isn't really anything much, just a tidbit to stuff away in the file folder of useful snippets. Therefore I am putting this in the Scrapbook section.

Running a Transact-SQL Query in a Quick-And-Dirty Fashion



Say I have a .sql file and I want to run it very quickly against a certain database on my computer. I happen to know I can connect to a database using plain-old-fashioned Windows Authentication and integrated security, so we aren't doing anything fancy.

Tutorial: Northwind Database



This small tutorial assumes you've downloaded and installed the Northwind database on your instance of SQL Server Express. If not, see my article at HowTo: Install the Northwind and Pubs Sample Databases[^] if you're using SQL Server 2005 Express, or HowTo: Install the Northwind and Pubs Sample Databases in SQL Server 2008 Express[^] if you're using SQL Server 2008 Express.

Let's create a .sql query file for use against the Northwind database, which I will say is on the SQLEXPRESS instance on my computer, called COMPUTER as its computer name.

The following code is in the MyQuery.sql file:

SQL
SELECT TOP 10 * FROM Customers


I typed the above into Notepad and then saved it as myQuery.sql on the Desktop. Now let's whip it off. Click the Start button, and then click Command Prompt.

Next, at the C:\> prompt, type:

C:\Users\bchart\Desktop\> sqlcmd -S COMPUTER\SQLEXPRESS -d Northwind -i myQuery.sql


That's all you do if you want to exec a quickie query from a .sql script file against a specific server instance and database. Type:

C:\Users\bchart\Desktop\> sqlcmd -?


For more juicy command-line goodness :)

Now Let's Put this To Work



This is a great tip, because what if you have a command-line console application you've written in C# to do, e.g., automated database reporting, and you have it living as, e.g., a Scheduled Task. Now what if you have some .sql Transact-SQL script you need that process to run and it's some ungodly long stored-procedure creation script or whatever.

Well, with the tip above, just make a C# call to run the command line above from your code, as in:

C#
using System.Diagnostics;

Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = "sqlcmd.exe";
process.StartInfo.Arguments = "-S COMPUTER\\SQLEXPRESS -d Northwind -i myQuery.sql";
process.StartInfo.WorkingDirectory = @"C:\Users\bchart\Desktop";
process.Start();
process.WaitForExit();


This C# code does identically the same thing as when I ran the command line above by hand from the Command Prompt. Clickety to this nice article[^] to learn more about spawning child processes from code!

Brian

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
United States United States
Brian C. Hart, Ph.D., is a strategic engagement leader on a mission to leverage space technology to protect U.S. interests and assets against adversaries. Throughout Dr. Hart's career, he has enjoyed: Working closely with business executives to provide strategic direction and leadership, translating customer and competitive intelligence into compelling capture strategies and solutions, and mentoring teams to enhance individual and company capabilities while fostering an engaging and accountable environment, being involved in STEAM initiatives and education to develop greater awareness in the community, and serving the armed forces with the U.S. Navy and U.S. Army National Guard. He is excited to begin developing his career in Jacobs's Critical Mission Systems business unit, supporting NORAD and the U.S. Space Force.

Comments and Discussions

 
Questionthanks for great tip Pin
Member 1047466311-Nov-14 5:51
Member 1047466311-Nov-14 5:51 
AnswerRe: thanks for great tip Pin
Brian C Hart11-Nov-14 5:56
professionalBrian C Hart11-Nov-14 5:56 
QuestionSQLCMD process is not getting stopped after execution is completed. Pin
Gowthu B23-Jul-14 2:42
Gowthu B23-Jul-14 2:42 
AnswerRe: SQLCMD process is not getting stopped after execution is completed. Pin
Brian C Hart11-Nov-14 6:07
professionalBrian C Hart11-Nov-14 6:07 
Questionsqlcmd output or status Pin
kkeyur21-Nov-12 0:18
kkeyur21-Nov-12 0:18 
AnswerRe: sqlcmd output or status Pin
Brian C Hart11-Nov-14 6:04
professionalBrian C Hart11-Nov-14 6:04 
GeneralReason for my vote of 4 great tip Pin
daltonrs@hotmail.com11-Jan-11 0:15
daltonrs@hotmail.com11-Jan-11 0:15 
GeneralRe: Reason for my vote of 4great tip Pin
Brian C Hart11-Nov-14 6:08
professionalBrian C Hart11-Nov-14 6:08 
GeneralReason for my vote of 3 good Pin
Sumit P27-Dec-10 7:02
Sumit P27-Dec-10 7:02 
GeneralRe: Reason for my vote of 3good Pin
Brian C Hart11-Nov-14 6:08
professionalBrian C Hart11-Nov-14 6:08 
GeneralReason for my vote of 5 Thanks for sharing this tip. Pin
linuxjr23-Dec-10 2:23
professionallinuxjr23-Dec-10 2:23 
GeneralRe: Reason for my vote of 5Thanks for sharing this tip. Pin
Brian C Hart11-Nov-14 6:22
professionalBrian C Hart11-Nov-14 6:22 
GeneralReason for my vote of 5 Good one............:-) Pin
Vivek Johari22-Dec-10 21:52
Vivek Johari22-Dec-10 21:52 
GeneralReason for my vote of 5 Thanks, I will be able to use this. Pin
Nueman22-Dec-10 11:21
Nueman22-Dec-10 11:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.