Using SQLCmd

SQLCmd is a command line tool that allows you to enter T-SQL sentences or script files by connecting to the SQL Server instance using OLE DB. SQLCmd is the replacement for the previous osql command.

Working with the SQLCmd Utility

To start the SQLCmd utility, you should launch SQLCmd from the command line. You can use the default SQL Server instance or connect to a named SQL Server instance.

Connecting to a Default SQL Server Instance
  1. From the Start menu, click Run. In the Open textbox, type cmd and click OK.

  2. In the command prompt, type sqlcmd and press Enter.

  3. You will enter the SQLCmd shell and observe a 1> prompt.

  4. Type Select name from sys.databases and press Enter.

  5. Type Go and press Enter. The result will be displayed.

  6. Type Exit and press Enter to quit SQLCmd.

If you want to connect to a named instance, you should use the S input parameter and specify the instance name in the Server\Instance format. You can view all input parameters by executing SQLCmd /?, as shown in Figure 3-20.

image from book
Figure 3-20: SQLCmd input parameters.

Executing Script Files

You can use SQLCmd to execute script files from the command line, which allows you to schedule scripts outside the SQL Server instance.

Executing the DTA.sql Script by Using the SQLCmd Utility
  1. From the Start menu, click Run.

  2. In the Open textbox, type cmd and click OK.

  3. In the command prompt, type sqlcmd i < path >\Ch03\SQLCmd.sql (replace <path> with the path in which you installed the sample files).

  4. Press Enter. A list of product names is displayed.



Solid Quality Learning, Microsoft Corporation Staff - Microsoft SQL Server 2005 Database Essentials Step by Step
Solid Quality Learning, Microsoft Corporation Staff - Microsoft SQL Server 2005 Database Essentials Step by Step
ISBN: N/A
EAN: N/A
Year: 2006
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net