Advertisement
Guest User

Untitled

a guest
Dec 26th, 2016
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. # Using the OSQL Command Line Tool to Perform SQL Server Operations
  2. Applies To: Express Software Manager version 7.x and above
  3.  
  4. ## Summary
  5. If you do not have a version of Microsoft SQL Server that includes an interactive management
  6. application such as SQL Enterprise Manager or SQL Studio, you can still perform maintenance tasks
  7. such as backup and restore, on your Express database using OSQL, the command line SQL tool. This
  8. technical note describes how to perform such tasks using OSQL.
  9.  
  10. ## Using OSQL with the Express Database
  11. ### Basic OSQL syntax
  12. OSQL is a command line tool that allows you to issue commands to Microsoft SQL Server. To run
  13. OSQL, simply bring up a DOS box and type OSQL followed by any required switches. You can view
  14. the complete OSQL command syntax with:
  15.  
  16. ```
  17. OSQL -?
  18. ```
  19.  
  20. This technical note describes only those switches required to perform the operations included in the
  21. note. Note: OSQL switches are case-sensitive. Switches:
  22.  
  23. * `-S <sql-server-name>` - the name of the SQL Server, including instance, if applicable. You may
  24. use "." (without quotes) if you are running OSQL on the same machine as SQL Server.
  25. * `-d <database-name>' - the name of the database on which the operation is to be performed. For
  26. example `-d ExpressDB`.
  27. * `-U <user-name>` - the SQL Server user account under which to run the specified command.
  28. * `-P <password>` - the password associated with the specified user account.
  29. * `-E` - use NT authentication to interact with SQL Server
  30. * `-Q "<SQL-command>"` - the command to issue to SQL Server.
  31. * `-i <file-of-SQL-commands>` - a file containing one or more commands to issue to SQL Server.
  32.  
  33. OSQL can also operate interactively. If you specify only the server, database, user name, and
  34. password, you will be presented with a prompt allowing you to enter and execute commands one at a
  35. time. The command exit exists interactive mode.
  36.  
  37. The examples below using the following sample values for the switch parameters described above:
  38.  
  39. * Database: `ExpressDB`
  40. * SQL Server: `.\ESM` - that is, the ESM instance of SQL Server on the current machine
  41. * User: `sa` - the SQL Server administrative account
  42. * Password: `mysapwd` - the password associated with the sa account
  43.  
  44. **Important notes**
  45.  
  46. 1. The operations described in this technical note require SQL Server administrative privileges.
  47. This means you must either use the SQL Server sa account, another administrative-capable
  48. account, or be logged into Windows under an account that has SQL Server administrative
  49. rights.
  50.  
  51. 2. SQL commands are issued in the context of the machine running SQL Server. If you use
  52. OSQL on one machine to issue commands to SQL Server on a different machine, any file/path
  53. information specified must be in the context of the SQL Server machine, not your local
  54. machine.
  55.  
  56. ## Backing up your Express Database
  57. Backups of your Express Database should be done on a regular basis. To create a backup using
  58. OSQL, use the following command:
  59.  
  60. ```
  61. OSQL -S <sql-server-name> -U <user-name> -P <password> _
  62. -Q "BACKUP DATABASE <database-name> to disk = '<path>\<database-name>.bak' WITH
  63. INIT"
  64. ```
  65.  
  66. For example, using the sample parameters above and creating the backup file in the `\sqlbackups`
  67. folder on your `C:` drive, the command would be:
  68.  
  69. ```
  70. OSQL -S .\ESM -U sa -P mysapwd -Q "BACKUP DATABASE ExpressDB to disk =
  71. 'c:\sqlbackups\ExpressDB.bak'"
  72. ```
  73.  
  74. **Important note**
  75.  
  76. You can write the backup to a local folder or network share, however in either case, the account
  77. under which SQL Server is running must have write access to the folder in which the backup file is
  78. written.
  79.  
  80. ## Restoring an Express Database
  81. If you need to restore your Express Database from a backup, you can use the following OSQL
  82. command:
  83.  
  84. ```
  85. OSQL -S <sql-server-name> -U <user-name> -P <password> _
  86. -Q"RESTORE DATABASE <database-name> FROM DISK = '<backup-file-path>\<databasename>.bak'
  87. WITH MOVE '<database-name>' TO '<new-data-file-path>\<database-name>.mdf',
  88. MOVE '<database-name>_Log' TO '<new-data-file-path>\<database-name>_Log.ldf'"
  89. ```
  90.  
  91. Notice that when restoring a database, you specify where the backup file exists as well as where SQL
  92. Server will actually create the database files (`.mdf`, `.ldf`) when restoring. For example, using the
  93. sample parameters above, restoring a database backup found in the `\sqlbackups` folder on your `C:``
  94. drive, and creating the new database files in C:\Program Files\Microsoft SQL
  95. Server\MSSQL.1\MSSQL\Data` (the data folder for a typical instance of SQL Server 2005):
  96.  
  97. ```
  98. OSQL -S .\ESM -U sa -P mysapwd -Q "RESTORE DATABASE ExpressDB FROM DISK =
  99. 'c:\sqlbackups\ExpressDB.bak' WITH MOVE 'ExpressDB' TO 'C:\Program Files\Microsoft
  100. SQL Server\MSSQL.1\MSSQL\Data\ExpressDB.mdf', MOVE 'ExpressDB_Log' TO
  101. 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\ExpressDB_Log.ldf'"
  102. ```
  103.  
  104. **Important notes**
  105.  
  106. 1. You can specify that the backup file come from a network share, however, the account under
  107. which SQL Server is running, must have access to the share.
  108.  
  109. 2. The account under which SQL Server is running must also have write access to wherever the
  110. database files (`.mdf`, `.ldf`) are created.
  111.  
  112. 3. If you are moving a database from one machine to another (or from one SQL Server instance
  113. to another), you will need to recreate the Express account used by Express Software Manager
  114. to access the data. (Even if the target SQL Server already has an Express account, you may
  115. need to "bind" it to the account information restored from the backup.) Included with the
  116. database and database tools components of your Express Software Manager installation is a
  117. batch file, InitializeExpressUser.bat which facilitates the creation (if necessary) and binding
  118. of the Express account. From a DOS prompt, you can run `InitializeExpressUser.bat` without
  119. parameters to view its syntax.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement