SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


check if database exist before restore


check if database exist before restore

Author
Message
Robin35
Robin35
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1667 Visits: 1290
hi,

i'm in the process of automating the database restore using powershell....
but before getting the default paths of data and log files of an instance...i would like to check if database already exists or not ?

Please let me know how to get that information...

IF EXISTS (select d.name,f.physical_name from sys.master_files f, sys.databases d where f.database_id = d.database_id and d.name = 'Test_Restore')

select f.physical_name from sys.master_files f, sys.databases d where f.database_id = d.database_id and d.name = 'master'

before the restore i would like to check if db exist, if it's in it should generate the data and log file path . if it's not in we need to get default path of instance or master db path.
please let me know
Thanks in advance
Neeraj Dwivedi
Neeraj Dwivedi
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1931 Visits: 1341
You are doing it right. What issue you are facing?

Instaed of if exists use "If NOT EXISTS"

Check for DB from sys. sysdatabases then get path from sys.master_files.
Robin35
Robin35
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1667 Visits: 1290
Thanks Neeraj..

i'm executing like this

IF NOT EXISTS (select d.name, f.physical_name from sys.master_files f, sys.sysdatabases d where f.database_id = d.dbid and
d.name = 'Test_Restore')

select f.physical_name from sys.master_files f, sys.databases d where f.database_id = d.database_id and
d.name = 'master'

i didnt get any output...
Neeraj Dwivedi
Neeraj Dwivedi
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1931 Visits: 1341
Do you already have Database named Test_Restore?
Robin35
Robin35
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1667 Visits: 1290
yes its already in instance
Neeraj Dwivedi
Neeraj Dwivedi
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1931 Visits: 1341
So, if already exists, it will not return anything from the second sql. Because you are checking for not exists?

I thought that you want the path for Master DB only if the Test_Restore doesn't exist.
Steve Jones
Steve Jones
SSC Guru
SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)

Group: Administrators
Points: 139928 Visits: 19413
The default data path is stored in the registry. Here are some answers, but it's the one that reads the registry that you want

http://stackoverflow.com/questions/1883071/how-do-i-find-the-data-directory-for-a-sql-server-instance

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)SSC Guru (90K reputation)

Group: General Forum Members
Points: 90336 Visits: 38945
Do not use sys.sysdatabases. It is a SQL Server 2000 system and is included for backward compatibility. You should use sys.databases instead.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Robin35
Robin35
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1667 Visits: 1290
thanks guys for the response...

my requirement has changed a bit...

i'm looking for something like this...

check if destination database exists, if it exists select physical location path ( data and log file )
else get the default instance path.

query to get the default instance path :

DECLARE @defaultDataLocation nvarchar(400)
DECLARE @defaultLogLocation nvarchar(400)

EXEC master.dbo.xp_instance_regread
N'HKEY_LOCAL_MACHINE',
N'Software\Microsoft\MSSQLServer\MSSQLServer',
N'DefaultData',
@defaultDataLocation OUTPUT

EXEC master.dbo.xp_instance_regread
N'HKEY_LOCAL_MACHINE',
N'Software\Microsoft\MSSQLServer\MSSQLServer',
N'DefaultLog',
@defaultLogLocation OUTPUT

SELECT @defaultDataLocation AS 'Default Data Location',@defaultLogLocation AS 'Default Log Location'

Please let me know the script...this is for database restore automation....so when we give the destination db parameters...it must check the db first....

thanks in advance
Robin35
Robin35
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1667 Visits: 1290
Neeraj Dwivedi (5/7/2013)
So, if already exists, it will not return anything from the second sql. Because you are checking for not exists?

I thought that you want the path for Master DB only if the Test_Restore doesn't exist.


Hi Neeraj,

you are correct when i put if exists i get master db....for if not exists i dont see anything...

but i'm looking in a different way...please see my latest post....

thanks for your time
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search