2.8. New Command-Line Utilities
SQL Server 2005 introduces the new command-line utilities described in Table 2-2.
126.96.36.199. SQL Server command-line tool (sqlcmd utility)
The SQL Server command-line tool lets you execute T-SQL statements, system procedures, and script files from the command prompt. The sqlcmd utility replaces the osql and isql utilities. sqlcmd uses OLE DB to communicate with the SQL Server Database Engine instead of using ODBC or DB-Library APIs.
The following example connects to the default instance of SQL Server and executes a query against the AdventureWorks database. Open a Command Prompt dialog box (Start All Programs Accessories Command Prompt) and execute the following command:
sqlcmd -q "SELECT TOP 3 ContactID, FirstName, LastName FROM AdventureWorks.Person.Contact"
The console output is shown in Figure 2-19.
The -q switch executes the specified query. The -? switch returns information about all switches for the sqlcmd utility. For a complete overview of the sqlcmd utility and its switches, see Microsoft SQL Server 2005 Books Online.
Figure 2-19. sqlcmd example
Alternatively, you can start sqlcmd and execute the preceding query in a batch with the following commands:
sqlcmd SELECT TOP 3 ContactID, FirstName, LastName FROM AdventureWorks.Person.Contact" GO
Figure 2-20 shows the sqlcmd batch.
Figure 2-20. SqlCmd batch example
The QUIT or EXIT command exits sqlcmd batch mode.