2.8. New Command-Line UtilitiesSQL Server 2005 introduces the new command-line utilities described in Table 2-2.
2.8.1.2.8.1.1. 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 exampleAlternatively, 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 exampleThe QUIT or EXIT command exits sqlcmd batch mode. |