Friday, June 15, 2012

OSQL Tool

OSQL is a command line tool that allows you to issue commands to Microsoft SQL Server. To run OSQL, simply bring up a DOS box and type OSQL followed by any required switches. You can view the complete OSQL command syntax with:
OSQL -?
This technical note describes only those switches required to perform the operations included in the note. Note: OSQL switches are case-sensitive. Switches:
·       -S <sql-server-name> - the name of the SQL Server, including instance, if applicable. You may use "." (without quotes) if you are running OSQL on the same machine as SQL Server.
·       -d <database-name> - the name of the database on which the operation is to be performed. For example -d ExpressDB.
·       -U <user-name> - the SQL Server user account under which to run the specified command.
·       -P <password> - the password associated with the specified user account.
·       -E - use NT authentication to interact with SQL Server
·       -Q "<SQL-command>" - the command to issue to SQL Server.
·       -i <file-of-SQL-commands> - a file containing one or more commands to issue to SQL Server.
OSQL can also operate interactively. If you specify only the server, database, user name, and password, you will be presented with a prompt allowing you to enter and execute commands one at a time. The command exit exists interactive mode.

The examples below using the following sample values for the switch parameters described above:
·       Database: ExpressDB
·       SQL Server: .\ESM - that is, the ESM instance of SQL Server on the current machine
·       User: sa - the SQL Server administrative account
·       Password: mysapwd - the password associated with the sa account
Important notes
1.    The operations described in this technical note require SQL Server administrative privileges. This means you must either use the SQL Server sa account, another administrative-capable account, or be logged into Windows under an account that has SQL Server administrative rights.
2.    SQL commands are issued in the context of the machine running SQL Server. If you use OSQL on one machine to issue commands to SQL Server on a different machine, any file/path information specified must be in the context of the SQL Server machine, not your local machine.
Steps to RESTORE database
Step 1: Start-> Run-> type “cmd”








Step 2: Type the below comments










-S  = <<Server>> - Server name
-E  = use NT authentication to interact with SQL Server
-Q = the command to issue to SQL Server (Normal SQL query which we used to run using SQL Management Studio Tool)












 

No comments:

Post a Comment