SQL Server 2005 - Using the Sqlcmd Utility

In this tutorial you will learn about SQL Server 2005 - Using the Sqlcmd Utility, Running the sqlcmd utility, Sqlcmd rules - Quoted Strings, Code Page Conversion Rules and Interactive sqlcmd Example.

The interactive execution of T-SQL statements and scripts is enabled by the Win32 Command prompt utility called sqlcmd utility. This utility can be used to build scripts to be run by sqlcmd users or to interactively enter T-SQL statements into the utility in a manner that is similar to working with the command prompt window. Users can submit a sqlcmd job as a single T-SQL statement for execution or point the utility to a text file that contains the required T-SQL statements. The output is then displayed in the window or in a text file.

Running the sqlcmd utility

Open the command prompt window and type sqlcmd at the prompt. A number of switches can be used ot specify the options of the utility.

The –s switch is a server switch that identifies the instance of the SQL Server to which the utility is to be connected. –E,-U and –P specify the credentials sqlcmd uses to connect (the –E is default). Input switches are –Q, -q and –i and output switches are –o.

When the sqlcmd utility is executed without input files or queries the sqlcmd connects to the default instance of MSSQL Server and displays the 1> called the sqlcmd prompt. The1 signifies the first line of the transact sql statement to be entered. In other words in the sqlcmd prompt the user can type sqlcmd commands and T-SQL statements such as GO and EXIT. Each T-SQL statement is buffered in a statement cache and are sent to the SQL server after the GO statement followed by the Enter key. The sqlcmd can be terminated using the EXIT command. The statement cache can be cleared by typing RESET. ^C can be used to stop the execution of a statement cache after the GO command is given.

Read More...

http://www.exforsys.com/content/view/1852/356/