osql Utility
The osql utility allows you to enter Transact-SQL statements, system procedures, and script files. This utility uses ODBC to communicate with the server.
Important
|
|---|
|
This feature will be removed in a future version of SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use the feature. Use sqlcmd instead. For more information, see sqlcmd Utility. |
osql
[-?] |
[-L] |
[
{
{-Ulogin_id [-Ppassword]} | –E }
[-Sserver_name[\instance_name]] [-Hwksta_name] [-ddb_name]
[-ltime_out] [-ttime_out] [-hheaders]
[-scol_separator] [-wcolumn_width] [-apacket_size]
[-e] [-I] [-D data_source_name]
[-ccmd_end] [-q "query"] [-Q"query"]
[-n] [-merror_level] [-r {0 | 1}]
[-iinput_file] [-ooutput_file] [-p]
[-b] [-u] [-R] [-O]
]
The osql utility is started directly from the operating system with the case-sensitive options listed here. After osqlstarts, it accepts SQL statements and sends them to SQL Server interactively. The results are formatted and displayed on the screen (stdout). Use QUIT or EXIT to exit from osql.
If you do not specify a user name when you start osql, SQL Server checks for the environment variables and uses those, for example, osqluser=(user) or osqlserver=(server). If no environment variables are set, the workstation user name 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 Microsoft Windows Authentication Mode. Authentication is based on the Microsoft Windows account of the user running osql.
The osql utility uses the ODBC API. The utility uses the SQL Server ODBC driver default settings for the SQL Server ISO connection options. For more information, see Effects of ANSI Options.
Note
|
|---|
|
The osql utility does not support CLR user-defined data types. To process these data types, you must use the sqlcmd utility. For more information, see sqlcmd Utility. |
OSQL Commands
In addition to Transact-SQL statements within osql, these commands are also available.
|
Command |
Description |
|---|---|
|
GO |
Executes all statements entered after the last GO. |
|
RESET |
Clears any statements you have entered. |
|
QUIT or EXIT( ) |
Exits from osql. |
|
CTRL+C |
Ends a query without exiting from osql. |
Note
|
|---|
|
The !! and ED commands are no longer supported by osql. |
The command terminators GO (by default), RESET EXIT, QUIT, and CTRL+C, are recognized only if they appear at the beginning of a line, immediately following the osql prompt.
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, osql 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 osql utility works as if there is an implied GO at the end of any script executed, therefore all statements in the script execute.
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. osql does not accept more than 1,000 characters per line. Large statements should be spread across multiple lines.
The command recall facilities of Windows can be used to recall and modify osql statements. The existing query buffer can be cleared by typing RESET.
When running stored procedures, osql prints a blank line between each set of results in a batch. In addition, the "0 rows affected" message does not appear when it does not apply to the statement executed.
Using osql Interactively
To use osql interactively, type the osql 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 osql by typing a command similar to this:
osql -E -i stores.qry
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:
osql -E -i titles.qry -o titles.res
Security Note
|
|---|
|
When possible, use the -Eoption (trusted connection). |
When using osql interactively, you can read an operating-system file into the command buffer with :r file_name. This sends the SQL script in file_name directly to the server as a single batch.
Note
|
|---|
|
When using osql, SQL Server treats the batch separator GO, if it appears in a SQL script file, as a syntax error. |
Inserting Comments
You can include comments in a Transact-SQL statement submitted to SQL Server by osql. Two types of commenting styles are allowed: -- and /*...*/.
Using EXIT to Return Results in osql
You can use the result of a SELECT statement as the return value from osql. If it is numeric, the last column of the last 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 passes the entire 4-byte integer. The syntax is:
EXIT ( < query > )
For example:
EXIT(SELECT @@ROWCOUNT)
You can also include the EXIT parameter as part of a batch file. For example:
osql -E -Q "EXIT(SELECT COUNT(*) FROM '%1')"
The osql utility passes everything between the parentheses ( ) to the server exactly as entered. 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:
-
EXIT
Note
|
|---|
|
Does not execute the batch; quits immediately and returns no value. |
-
EXIT( )
Note
|
|---|
|
Executes the batch, and then quits and returns no value. |
-
EXIT(query)
Note
|
|---|
|
Executes the batch, including the query, and then quits after returning the results of the query. |
-
RAISERROR with a state of 127
Note
|
|---|
|
If RAISERROR is used within an osql script and a state of 127 is raised, osql will quit and return the message ID back to the client. For example: |
RAISERROR(50001, 10, 127)
This error will cause the osql script to end and the message ID 50001 will be returned to the client.
The return values -1 to -99 are reserved by SQL Server; osql defines these values:
-
-100
Error encountered prior to selecting return value.
-
-101
No rows found when selecting return value.
-
-102
Conversion error occurred when selecting return value.
Displaying money and smallmoney Data Types
osql displays the money and smallmoney data types with two decimal places although SQL Server stores the value internally with four decimal places. Consider the example:
SELECT CAST(CAST(10.3496 AS money) AS decimal(6, 4)) GO
This statement produces a result of 10.3496, which indicates that the value is stored with all decimal places intact.
Important
Note
Security Note