Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi
This is the batch script i am using for database backup & Restore.set UN=%1
set PWD=%2
set SER=%3
set DB=%4
sqlcmd -U %UN% -P %PWD% -S %SER% -Q "EXEC sp_dboption '%DB%','dbo use only','TRUE'"
sqlcmd -U %UN% -P %PWD% -S %SER% -Q "RESTORE DATABASE %DB% FROM DISK='%DB%.bak'"Here I need to make the database to be "dbo use only" from the CLI
So before restoring I need to execute the following command
EXEC sp_dboption '%DB-NAME%','dbo use only','TRUE'But here the issue is:
If database already exists this line not makes any error.
Else if database was been dropped. Then it produces the following error:Msg 15010, Level 16, State 1, Server MT-SERVER123, Procedure sp_dboption, Line 64
The database 'testDatabase' does not exist. Use sp_helpdb to show available databases.
Available databases:
--------
.....
So here I need to check whether a database exist and need to run the command:
EXEC sp_dboption '%DB-NAME%','dbo use only','TRUE'Is there any way to check whether the database exists and then execute the above command.
Please help me out in this issue asap
Thanks in AdvanceTrusp

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |