Executing Queries

Use

There are three utilities that can be used to execute Transact-SQL statements and stored procedures:

·        The SQL Server Query Analyzer

·        isql utility

·        osql utility

Of these utilities, the SQL Server Query Analyzer is the most frequently used. It has a graphical user interface and is therefore easy to use. In addition it offers quick access to help on the syntax of commands.

isql and osql are command line utilities. They have the advantage that they allow the specification of input and output files for scripts and therefore allow you to automate and schedule the execution of scripts.

Always use osql. The isql utility is included in SQL Server 2000 for compatibility reasons. It does not support all SQL Server 2000 features.

Procedure

The SQL Server Query Analyzer

...

       1.      To start the Query Analyzer in the Enterprise Manager, select your server and choose Tools ® SQL  Query Analyzer.

Alternatively, if the Enterprise Manager is not running, you can start the Query Analyzer from the Windows Start menu. Choose Programs ® Microsoft SQL Server ® Query Analyzer

The SQL Server Query Analyzer  window opens.

       2.      In the database field, select the database on which you want to execute a query.

       3.      Enter the query. To execute the Query choose Query ® Execute.

The results are displayed in a second pane that appears.

·         If you need help on the syntax of a statement, simply highlight the keyword and press SHIFT F1. This takes you directly to the Transact SQL-Help that gives detailed information on the statement and illustrates it with examples.

·         A convenient feature allows you to type in several queries on the screen, but only to execute one of them. Simply highlight the statement you wish to execute before choosing the execute icon.

The osql Utility

The osql utility allows you to execute Transact-SQL statements, stored procedures and script files. The utility is started in the command prompt.

To execute a script with the osqlutility enter a command as follows:

osql -S<server_name> -D<SAPSID> -U<login> -P<password> -i<script_name> -o<output_file>

For details refer to the topic “osql utility” in the MS SQL Server Books Online.