Overview of SQLCMD command in Microsoft SQL Server



If you've enjoyed reading this post then please subscribe to my Full Text RSS Feed.

Microsoft SQL Server provides a tool to connect to the SQL Server database to run SQL queries all from the command line. This is useful for database administrators who need to quickly connect to run a SQL command without having to start the SQL Server Management Studio (SSMS).

In this example I will concentrate on 3 simple examples to give a quick overview of what you can do with SQLCMD. The first example shows you how to connect to a SQL Server database using the Windows Authentication. The second example shows you how to connect to SQL Server database using the SQL Authentication and finally the third example shows you how to run a simple SELECT query. All examples are based on SQL Server 2005.

Connect to the master database using Windows Authentication

Using sqlcmd to connect to SQL Server using Windows Authentication

Using sqlcmd to connect to SQL Server using Windows Authentication

-E indicates Windows Authentication.
-S the SQL Server name instance. In this example it is Merovingian.
-d name of the database. In this example the database is master.

Connect to the master database using SQL Authentication

using sqlcmd to connect to sql server using sql authentication

using sqlcmd to connect to sql server using sql authentication

-S the SQL Server name instance. In this example it is Merovingian.
-d name of the database. In this example the database is master.
-U the username used to login to SQL Server using SQL Authentication.
-P the password used to login to SQL Server using SQL Authentication.

SELECT query using SQLCMD

SELECT query using SQLCMD

SELECT query using SQLCMD

In this example, we want to query the TOP 10 contacts displaying the first name and last name.

Enhanced by Zemanta
Sphere: Related Content

Related Posts

About the Author

a tech junkie and a software developer. a apple fan and an avid photographer. a frequent traveller and loves art and graphic novels. My Google+