The isql utility allows you to enter Transact-SQL statements, system procedures, and script files; and uses DB-Library to communicate with Microsoft® SQL Server™.
Note DB-Library remains at the SQL Server version 6.5 level of functionality. DB-Library applications, such as isql, do not support some SQL Server 7.0 features. For example, they cannot retrieve Unicode ntext data. The osql utility has a user interface modeled on isql and supports the full set of SQL Server 7.0 features.
isql -U login_id [-e] [-E] [-p] [-n] [-d db_name] [-Q "query"] [-q "query"]
[-c cmd_end] [-h headers] [-w column_width] [-s col_separator]
[-t time_out] [-m error_level] [-L] [-?] [-r {0 | 1}]
[-H wksta_name] [-P password]
[-S server_name] [-i input_file] [-o output_file] [-a packet_size]
[-b] [-O] [-l time_out] [-x max_text_size]
SET table = sysobjects
isql /q "Select * from %table%"
Use double quotation marks around the query and single quotation marks around anything embedded in the query.
The ISQLPASSWORD environment variable allows you to set a default password for the current session. Therefore, you do not have to hard code a password into batch files.
If you do not specify a password with the -P option, isql first checks for the ISQLPASSWORD variable. If no value is set, isql uses the default password, NULL. The following example sets the ISQLPASSWORD variable at the command prompt and then accesses the isql utility:
C:\>SET ISQLPASSWORD=abracadabra
C:\>isql
It also sets the default DOS ERRORLEVEL value to -1.
All DB-Library applications, such as isql, work as SQL Server 6.5-level clients when connected to SQL Server 7.0. They do not support some of the new SQL Server 7.0 features. The osql utility is based on ODBC and does support all new SQL Server 7.0 features. Use osql to run scripts that isql cannot run.
The SQL Server Query Analyzer default is to save SQL scripts as Unicode files. The isql utility does not support Unicode input files. Attempting to specify one of these files in the -i switch results in a 170 error:
Incorrect syntax near ' '.
Use the osql utility to run these Unicode files. An alternative is to specify ANSI instead of Unicode in the File format list of the SQL Server Query Analyzer File/Save As dialog box.
Like most DB-Library applications, the isql utility does not set any connection options by default. Users must issue SET statements interactively or in their scripts if they want to use specific connection option settings.
The isql utility is started directly from the operating system with the case-sensitive options listed here. After starting, isql accepts Transact-SQL statements and sends them to SQL Server interactively. The results are formatted and printed on the standard output device (the screen). Use QUIT or EXIT to exit from isql.
If you do not specify a username when you start isql, SQL Server checks for the environment variables and uses those. For example, isqluser=(user) or isqlserver=(server). If no environment variables are set, the workstation username is used. If you do not specify a server, the name of the workstation is used.
If neither the -U or -P options are used, SQL Server attempts to connect using Windows NT Authentication Mode. Authentication is based on the Windows NT account of the user running isql.
In addition to using Transact-SQL statements within isql, these commands are also available.
Command | Description |
---|---|
GO | Executes all statements entered after the last GO. |
RESET | Clears any statements you have entered. |
ED | Calls the editor. |
!! command | Executes an operating-system command. |
QUIT or EXIT( ) | Exits from isql. |
CTRL+C | Ends a query without exiting from isql. |
The command terminators GO (by default), RESET, ED, !!, EXIT, QUIT, and CTRL+C are recognized only if they appear at the beginning of a line, immediately following the isql prompt. Anything entered on the same line after these keywords is disregarded by isql.
GO signals both the end of a batch and the execution of any cached Transact-SQL statements. When you press ENTER at the end of each input line, isql caches the statements on that line. When you press ENTER after typing GO, all of the currently cached statements are sent as a batch to SQL Server.
The current isql utility works as if there is an implied GO at the end of any script executed, so all statements in the script execute. Some earlier versions of isql would not send any statements to the server unless there was at least one GO in an input script. Any statements after the last GO would not be executed. Earlier versions of the isql utility may require that GO be at the start of a line, without any blanks preceding it.
End a command by typing a line beginning with a command terminator. You can follow the command terminator with an integer to specify how many times the command should be run. For example, to execute this command 100 times, type:
SELECT x = 1
GO 100
The results are printed once, at the end of execution. With isql, there is a limit of 1000 characters per line. Large statements should be spread across multiple lines.
The user can call an editor on the current query buffer by typing ED as the first word on a line. The editor is defined in the EDITOR environment variable. The default editor is “edit” for Microsoft MS-DOS® and Windows NT. You can specify a different editor by setting the EDITOR environment variable. For example, to make the default editor Notepad, enter at the operating-system prompt:
SET EDITOR=notepad
Operating system commands can also be executed by starting a line with two exclamation points (!!) followed by the command. The command recall facilities of DOSKEY can be used to recall and modify previously entered isql statements on a computer running Windows NT. The existing query buffer can be cleared by typing RESET.
When running stored procedures, isql prints a blank line between each set of results in a batch. In addition, the “0 rows affected” message does not appear when it doesn’t apply to the statement executed.
To use isql interactively, type the isql command (and any of the options) at a command prompt.
You can read in a file containing a query (such as Stores.qry) for execution by isql by typing a command similar to this:
isql /U alma /P /i stores.qry
The file must include a command terminator(s).
You can read in a file containing a query (such as Titles.qry) and direct the results to another file by typing a command similar to this:
isql /U alma /P /i titles.qry /o titles.res
When using isql interactively, you can read an operating system file into the command buffer with :r file_name. Do not include a command terminator in the file; enter the terminator interactively after you have finished editing.
You can include comments in a Transact-SQL statement submitted to SQL Server by isql. Two types of commenting styles are allowed: -- and /*...*/ .
For more information, see Using Comments.
You can use the result of a SELECT statement as the return value from isql. The first column of the first result row is converted to a 4-byte integer (long). MS-DOS passes the low byte to the parent process or operating system error level. Windows NT passes the entire 4-byte integer. The syntax is:
EXIT(query)
For example:
EXIT(SELECT @@rowcount)
EXIT(SELECT 5)
You can also include the EXIT parameter as part of a batch file. For example:
isql /Q "EXIT(SELECT COUNT(*) FROM '%1')"
The isql utility passes everything between the parentheses ( ) to the server exactly as entered. The EXIT( ) statement can span lines. If a stored system procedure selects a set and returns a value, only the selection is returned. The EXIT( ) statement with nothing between the parentheses executes everything preceding it in the batch and then exits with no return value.
There are four EXIT formats:
Does not execute the batch; quits immediately and returns no value.
Executes the batch, and then quits and returns no value.
Executes the batch, including the query, and then quits after returning the results of the query.
If RAISERROR is used within an isql script and a state of 127 is raised, isql will quit and return the message ID back to the client. For example:
RAISERROR(50001, 10, 127)
This error will cause the isql script to end and the message ID 50001 will be returned to the client.
The return values -1 through -99 are reserved by SQL Server; isql defines the following values:
Error encountered prior to selecting return value.
No rows found when selecting return value.
Conversion error when selecting return value.
Managing Security | /*...*/ (Comment) |
-- (Comment) | RAISERROR |