Link Search Menu Expand Document

Module 1: SQL Essentials - SQL Commands

Previous Tutorial - Next Tutorial

SQL Server Management Studio is a powerful graphical tool to accomplish almost everything you need to do, but sometimes a simple command-line tool comes to handy when to accomplish some of the tasks where UI is not needed. Now let's open the command prompt CMD. SQL CMD supports several arguments that how it's connected to over SQL server instance and how it communicates to a database. So to see that option type this command, c:\>sqlcmd/?.

As you can see, SQL CMD provides different arguments to communicate with the SQL server database.  So here, S means you need to specify the SQL server instance name to connect and here U means you need to specify the user name and P means you need to specify the password.

Here Q means you can write the select statement on this command line prompt and execute the result. Another option is O which is an output file. That means you can specify the output text file to return the result of this SQL CMD statement. We will see these various options by performing practically.

Let's connect to our SQL server instance and access our database. So for that, I need to write command c:\>sqlcmd -s.

S means server name, so here I need to specify the server name. I am not specifying the username and password because I'm accessing the SQL database on my local machine.

So there is no need to specify the username and password. I can directly specify the database name which I want to access. So for that, you need to use the [-d use database name] option. By using this option I can specify my database name and I want to access this database. Then I press enter and now I'm writing the SQL statement.

SQL Essential Commands Image

Now, when I press enter, it will not execute any result because SQL CMD runs all the commands in batch mode and does not actually execute any commands until you explicitly tell it which means you can write as many lines of code or as many lines of query you want, but they will not be executed until the GO command.

It means you need to specify the GO command. You can think of this GO command as the same as the execute button in the toolbar. Now let's say in our case I want to execute this query, then I need to write GO, now when I write go >, it will give me a result. So SQL CMD will not be executed any command until I specify in the GO command. It means I can write multiple select statements over here.

So, for example, I have written this query and then I am executing another query. As you can see that here I have specified two queries. Now when I write Go and enter, it will give me the result of two tables. First from the category table and another from the product table.

SQL Commands Image

Now my other requirement is I want to export the category table data into one text file and at one particular location. So for that, you can add this requirement very easily in SQL CMD and for that, I need to write this command c:\>sqlcmd -s, here we specify the server name and then specify the database name. We will specify the query using the Q option and query result I want to export into the text file.

This is the query I want to execute and export these tables data into a text file. Now to export data into the text file, I need to specify the path. So for that, I need to use an O option that means output file. So at this location, I want to export this table data into this text file. Now, when I press enter it will ask me to execute the command, so I write GO and then I  press enter.

Now when I open this location, I can find this text file with this table name data. Now, let me open that location. So, here you can see the text file, which contains our category table data.

SQL Commands Tutorial Image

So this way you can export any table data into the text file using SQL CMD very easily. In the same way, you can explore the different options of SQL CMD on your own.

  Web API for developers Free Trial Offline SDK

SQL video tutorial:

Other useful tutorials:

Back to top

© , Learn SQL 24 / 7 — All Rights Reserved - Terms of Use - Privacy Policy