Microsoft SQL Server Microsoft SQL Server is a commercial DBMS that supports very large databases and numbers of transactions. It runs only on Microsoft Windows operating systems and is complex enough to require a full-time database administrator to run and maintain it. You can download a free 120-day evaluation version at www.microsoft.com/sql/evaluation.This book covers Microsoft SQL Server 2000. To determine which version of Microsoft SQL Server you're running, run the SQL Server command-line command osql -E -Q "SELECT @@VERSION;" (or run the query SELECT SERVERPROPERTY('ProductVersion');). See the sidebar if you're running SQL Server 2005 or SQL Server Express.
SQL Server 2005 SQL Server 2005, the successor to SQL Server 2000, is backward compatible with 2000 within the scope of this book, so all the examples that you find in here will run in 2005 as well. Here are a few things to know about running SQL programs on SQL Server 2005:- The new SQL Server Management Studio Query Editor replaces SQL Query Analyzer.
- The new sqlcmd command-prompt utility replaces osql. sqlcmd uses many of the same command-line options as osql. Run sqlcmd -? to show the syntax summary.
- SQL Server 2005 Express Edition (SQL Server Express) is a free, easy-to-use, lightweight version of SQL Server 2005. You can download it at www.microsoft.com/sql. It comes with the Express Manager graphical management tool.
| You can use the SQL Query Analyzer graphical tool or the osql command-line utility to run SQL programs.To use SQL Query Analyzer
| 1. | On the Windows desktop, choose Start > All Programs > Microsoft SQL Server > Query Analyzer. | | 2. | In the Connect to SQL Server dialog box, select the server and authentication mode; then click OK. | | 3. | On the toolbar (near the top edge of the window), select a database in the drop-down list (Figure 1.9 ).Figure 1.9. SQL Query Analyzer uses the selected database to resolve references in your SQL statements.
 | | 4. | To run SQL interactively, type or paste an SQL statement in the query window.or To run an SQL script, choose File > Open (or press Ctrl+Shift+P); navigate to and select the script file; then click Open. | | 5. | Choose Query > Execute (or press F5).SQL Query Analyzer displays the results in the bottom pane (Figure 1.10 ).Figure 1.10. The results of a SELECT statement in SQL Query Analyzer.
 |
Tip- You also can run isqlw at a command prompt to launch SQL Query Analyzer.
To use the osql command-line utility interactively
| 1. | At a command prompt, type: osql -E -d dbname
The -E option tells SQL Server to use a trusted connection instead of requesting a password. dbname is the name of the database to use. | | 2. | Type an SQL statement. The statement can span multiple lines. Terminate it with a semicolon (;) and then press Enter. | | 3. | Type go and then press Enter to display the results (Figure 1.11 ).Figure 1.11. The same SELECT statement in osql interactive mode.
 |
To use the osql command-line utility in script mode
| 1. | At a command prompt, type: osql -E -d dbname -n -i sql_script
The -E option tells SQL Server to use a trusted connection instead of requesting a password. dbname is the name of the database to use. The -n option suppresses numbering and prompt symbols (>) in the output. sql_script is a text file containing SQL statement(s) and can include an absolute or relative pathname. | | 2. | Press Enter to display the results (Figure 1.12 ).Figure 1.12. The same SELECT statement in osql script mode.
 |
To exit the osql command-line utility
To show osql command-line options
Tips- You can use the SET ANSI_DEFAULTS ON option to make SQL Server conform to standard SQL more closely.
- If SQL Server makes you specify a user name and password instead of using a trusted connection, replace the -E option with -U login_id. login_id is your user name. osql will prompt you for your password.
- If you're running osql from a remote network computer, add the option -S server to specify the SQL Server instance to connect to. Ask your database administrator for the connection parameters.
|