isql Command-line Utility

For SQL Server 6.5 information, see ISQLPASSWORD in What's New for SQL Server 6.5.

Allows you to enter SQL statements and system procedures.

Syntax

isql /U login_id [/e] [/E] [/p] [/n] [/d dbname] [/q "query"] [/Q "query"]
[/c cmdend] [/h headers] [/w columnwidth] [/s colseparator]
[/t timeout] [/m errorlevel] [/L] [/?] [/r {0 | 1}]
[/H wksta_name] [/P password]
[/S servername] [/i inputfile] [/o outputfile] [/a packet_size]

where

/U login_id
Is the user login ID. Login IDs are case-sensitive.
/e
Echoes input.
/E
Uses a trusted connection instead of requesting a password.
/p
Prints performance statistics.
/n
Removes numbering and the prompt symbol (>) from input lines.
/d dbname
Issues a USE dbname statement when isql is started.
/q "query"
Executes a query when isql starts. (Note, the query statement should not include GO). If you issue a query from a batch file, you can use %variables. Environment %variables% also work. For example:

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.

/Q "query"
Executes a query and immediately exits isql. Use double quotation marks around the query and single quotation marks around anything embedded in the query.
/c cmdend
Specifies the command terminator. By default, commands are terminated and sent to SQL Server by entering GO on a line by itself. When you reset the command terminator, do not use SQL reserved words or characters that have special meaning to the operating system, whether preceded by a backslash or not.
/h headers
Specifies the number of rows to print between column headings. The default is to print headings only once for each set of query results. Use -1 to specify that no headers will be printed. If using -1, there must be no space between the parameter and the setting (/h-1, not /h -1).
/w columnwidth
Allows the user to set the screen width for output. The default is 80 characters. When an output line has reached its maximum screen width, it is broken into multiple lines.
/s colseparator
Specifies the column-separator character, which is blank by default. To use characters that have special meaning to the operating system (for example, | ; & < >), precede the character with a backslash (\).
/t timeout
Specifies the number of seconds before a command times out. If no timeout is specified, a command runs indefinitely; the default timeout for logging in to isql is 8 seconds.
/m errorlevel
Customizes the display of error messages. The message number, state, and error level are displayed for errors of the specified severity level or higher. Nothing is displayed for errors of levels lower than the specified level. Use -1 to specify that all headers are returned with messages, even informational messages. If using -1, there must be no space between the parameter and the setting (/m-1, not /m -1).
/L
Lists the locally configured servers and the names of the servers broadcasting on the network.
/?
Displays the syntax summary of isql switches.
/r {0 | 1}
Redirects message output to stderr. If you don't specify a parameter, or if you specify 0, only error-level message severity (severity 17 or higher) is redirected. If you specify 1, all message output (including "print") is redirected.
/H wksta_name
Is a workstation name, changing the value in the dynamic system table sysprocesses, if the user logs in from a computer different from the usual login computer. If no workstation name is specified, the current computer name is assumed.
/P password
Is a user-specified password. If the /P option is not used, isql prompts for a password. If the /P option is used at the end of the command line without any password, isql uses the default password NULL. Passwords are case-sensitive.
/S servername
Specifies which SQL Server to connect to. The servername is the name of the server computer on the network. This option is required if you are executing isql from a remote computer on the network.
/i inputfile
Identifies the file that contains a batch of SQL statements or stored procedures. The less-than symbol (<) can be used in place of /i.
/o outputfile
Identifies the file that receives output from isql. The greater-than symbol (>) can be used in place of /o.
/a packet_size
Allows you to request a different size packet. The valid values for packet_size are 512 through 65535. The default value for the Windows NT - based version of isql is 8192; otherwise, the default value is 512 for MS-DOS, although larger sizes can be requested with that version as well. Increased packet size can enhance performance on larger script execution where the amount of SQL statements between GO statements is substantial. Microsoft testing indicates that 8192 is typically the fastest setting for bulk copy operations. A larger packet size can be requested, but will default to 512 if the request cannot be granted.

Remarks

The isql utility is started directly from the operating system with the case-sensitive options listed here. Once started, isql accepts SQL commands 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 ¾ for example, isqluser=(user) or isqlserver=(server), and uses those. 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 uses the currently logged in user account and will not prompt for a password. This is useful when using integrated or mixed security. For details about security, see the Microsoft SQL Server Administrator's Companion.

In addition to using Transact-SQL statements within isql, use the following commands:

Command Description
GO Executes a command.
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 Terminates 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.

Terminate 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. Operating-system commands can also be executed by starting a line with two exclamation points (!!) followed by the command. The editor is defined in the EDITOR environment variable. The default editor is "edit" for 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, at the operating-system prompt, type:

SET EDITOR=notepad

The command recall facilities of DOSKEY can be used to recall and modify previously entered isql statements on a Windows NT - based computer. The existing query buffer can be cleared by typing RESET on a line by itself. It causes any pending input to be discarded.

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 the operating-system 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). The results are displayed on the user's workstation.

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 filename. Do not include a command terminator in the file; enter the terminator interactively once 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. For more information, see the Comments topic.

For example:

SELECT au_lname, au_fname
 -- Retrieve authors' last and first names.
    FROM authors a, titles t, titleauthor ta
        WHERE a.au_id = ta.au_id
        AND t.title_id = ta.title_id
/* This is a three-way join that links authors
** to the books they have written. */

Note Do not include a GO command within a comment. A GO command within a comment produces an error message.

You can return the result of an integer SELECT 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:

The return values - 1 to - 99 are reserved by SQL Server; isql defines the following values:

- 100: Error encountered prior to selecting return value.

- 101: No rows found when selecting return value.

- 102: Conversion error when selecting return value.