Computing.Net > Forums > Programming > SQLCMD database exists

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

SQLCMD database exists

Reply to Message Icon

Name: trusp
Date: January 26, 2009 at 23:50:46 Pacific
OS: DOS
CPU/Ram: Intel DUO
Product: Ms / Ms/e6550
Subcategory: Batch
Comment:

Hi

I am creating a dos based batch file for database backup & Restore.

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'

This is the Script:
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'"


Please help me out in this issue



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: January 27, 2009 at 03:11:03 Pacific
Reply:

The solution is just outlined in the error message

Use sp_helpdb to show available databases.

I don't know your database application, but issuing the reported command before restoring seems to show a list of available DBs that can be filtered to be checked, so

...sp_helpdb | find /I "%DB-NAME%" > nul
if not errorlevel 1 (
EXEC sp_dboption '%DB-NAME%','dbo use only','TRUE'
)
...


0
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: SQLCMD database exists

how can i restore my tables www.computing.net/answers/programming/how-can-i-restore-my-tables-/13057.html

ASP Detecting Record Existence www.computing.net/answers/programming/asp-detecting-record-existence/6725.html

VB6 and database access question www.computing.net/answers/programming/vb6-and-database-access-question/925.html