search cancel

What are the MSDE OSQL command lines?


Article ID: 180150


Updated On:


Deployment Solution




What are the OSQL command lines?


OSQL Documentation

OQSL is the version of SQL that ships with MSDE. If you install Notification Server or Deployment Server and have the product install create a SQL database instead of installing into an existing SQL installation, it will install an OSQL database.

The OSQL utility allows you to enter Transact-SQL statements, system procedures, and script files. This utility uses ODBC to communicate with the server.


osql -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] [-I] [-L] [-?] [-r {0 | 1}] [-H wksta_name] [-P password] [-R] [-S server_name] [-i input_file] [-o output_file] [-u] [-a packet_size] [-b] [-O] [-l time_out]


  -U login_id   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 db_name   Issues a USE db_name statement when osql is started.
  -Q "query"   Executes a query and immediately exits OSQL. Use double quotation marks around the query and single quotation marks around anything embedded in the query.
  -q "query"   Executes a query when osql starts, but does not exit OSQL when the query completes. (Note that the query statement should not include GO). If you issue a query from a batch file, use %variables or environment %variables%. For example:

      SET table = sysobjects
      osql /q "Select * from %table%"

Use double quotation marks around the query and single quotation marks around anything embedded in the query.
  -c cmd_end   Specifies the command terminator. By default, commands are terminated and sent to Microsoft 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 one time 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 column_width   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 col_separator   Specifies the column-separator character, which is a blank space by default. To use characters that have special meaning to the operating system (for example, | ; & < >), enclose the character in double quotation marks (").
  -t time_out   Specifies the number of seconds before a command times out. If no time_out value is specified, a command runs indefinitely; the default time-out for logging in to osql is eight seconds.
  -m error_level   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).
  -I   Sets the QUOTED_IDENTIFIER connection option on.
  -L   Lists the locally configured servers and the names of the servers broadcasting on the network.
  -?   Displays the syntax summary of osql switches.
  -r {0 | 1}   Redirects message output to the screen (stderr). If you don't specify a parameter, or if you specify 0, only error messages with a severity level 17 or higher are redirected. If you specify 1, all message output (including "print") is redirected.
  -H wksta_name   Is a workstation name. The workstation name is stored in sysprocesses.hostname and is displayed by sp_who. If not specified, the current computer name is assumed.
  -P password   Is a user-specified password. If the -P option is not used, osql prompts for a password. If the -P option is used at the end of the command prompt without any password, osql uses the default password (NULL). Passwords are case-sensitive. The OSQLPASSWORD 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, OSQL first checks for the OSQLPASSWORD variable. If no value is set, osql uses the default password, NULL. The following example sets the OSQLPASSWORD variable at a command prompt and then accesses the osql utility:

      C:\>SET OSQLPASSWORD=abracadabra

  -R   Specifies that the SQL Server ODBC driver uses client settings when converting currency, date, and time data to character data.
  -S server_name   Specifies the SQL Server installation to which to connect. The server_name is the name of the server computer on the network. This option is required if you are executing osql from a remote computer on the network.
  -i input_file   Identifies the file that contains a batch of SQL statements or stored procedures. The less than (<) comparison operator can be used in place of -i.
  -o output_file   Identifies the file that receives output from osql. The greater than (>) comparison operator can be used in place of -o.

If input_file is not Unicode and -u is not specified, output_file is stored in OEM format.

If input_file is Unicode or -u is specified, output_file is stored in Unicode format.
  -u   Specifies that output_file is stored in Unicode format, regardless of the format of the input_file.
  -a packet_size   Allows you to request a different-sized packet. The valid values for packet_size are 512 through 65535. The default value OSQL is the server default. Increased packet size can enhance performance on larger script execution where the amount of SQL statements between GO commands is substantial. Microsoft testing indicates that 8192 is typically the fastest setting for bulk copy operations. A larger packet size can be requested, but OSQL defaults to the server default if the request cannot be granted.
  -b   Specifies that osql exits and returns a DOS ERRORLEVEL value when an error occurs. The value returned to the DOS ERRORLEVEL variable is 1 when the SQL Server error message has a severity of 10 or greater; otherwise the value returned is 0. Microsoft MS-DOS® batch files can test the value of DOS ERRORLEVEL and handle the error appropriately.
  -O   Specifies that certain OSQL features be deactivated to match the behavior of earlier versions of ISQL.

These features are deactivated:

      EOF batch processing
      Automatic console width scaling
      Wide messages

It also sets the default DOS ERRORLEVEL value to -1
  -l time_out   Specifies the number of seconds before an OSQL login times out. If no time_out value is specified, a command runs indefinitely. The default time-out for login to osql is fifteen seconds.


The OSQL utility is started directly from the operating system with the case-sensitive options listed here. After starting, osql 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 username 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 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 Microsoft Windows NT* 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 SQL-92 connection options, except for QUOTED_IDENTIFIER. Osql defaults to setting QUOTED_IDENTIFIER OFF. Use the -I switch to set it on.

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.
  ED   Calls the editor.
  !! command   Executes an operating-system command.
  QUIT or EXIT( )   Exits from osql.
  CTRL+C   Ends a query without exiting from osql.

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 OSQL prompt. Anything entered on the same line after these keywords is disregarded by OSQL.

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, 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.

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 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". 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

Operating system commands can also be executed by starting a line with two exclamation points (!!) followed by the command. The command recall facilities of Windows NT 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 doesn't 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 /U JoeUser /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:

      osql /U JoeUser /P /i titles.qry /o titles.res

When using osql 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.

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. 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:


For example:


      EXIT(SELECT 5)

You can also include the EXIT parameter as part of a batch file. For example:

      osql /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   Does not execute the batch; quits immediately and returns no value.
  EXIT( )   Executes the batch, and then quits and returns no value.
  EXIT(query)   Executes the batch, including the query, and then quits after returning the results of the query.
  RAISERROR - state 127   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 when selecting return value.

Login to the MSDE using the osql tool:

Using SQL authentication:

C:\> osql –U sa –P password –S localhost
1> use master
2> go
1> exec sp_password @old = null, @new = ‘newpassword’, @loginame = 'sa'
2> go
Password changed.


To add user account:

C:\ >osql -E -S localhost
1> use master
2> go
1> EXEC sp_addlogin 'user', 'password', 'express'
2> go


Add a user to db_owner role
Adding a user to a db_owner role will grant the user all of the permissions to that database.
To add a user into a role using the system stored procedures, use the sp_addrolemember stored procedure, passing the database role and the user name.

C:\ >osql -E -S localhost
1> use express
2> go
1> EXEC sp_addrolemember 'user', 'db_owner'
2> go
'user' added to role 'db_owner'.
Exit osql

1> quit


If you want to list all tables in an instance, please open the osql utility and do the following query: Type ‘U’ means “User Table”. Please try to change type to “S” if you want to list out all System tables.

C:\ >osql -E -S localhost
1> use express
2> go
1> select name from sysobjects where type = 'U'