Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

check if database exist before restore Expand / Collapse
Author
Message
Posted Tuesday, May 7, 2013 7:52 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, November 21, 2014 8:50 AM
Points: 95, Visits: 789
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
Post #1450148
Posted Tuesday, May 7, 2013 8:53 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, October 31, 2014 4:01 PM
Points: 835, Visits: 1,192
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.
Post #1450199
Posted Tuesday, May 7, 2013 9:21 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, November 21, 2014 8:50 AM
Points: 95, Visits: 789
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...
Post #1450214
Posted Tuesday, May 7, 2013 9:37 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, October 31, 2014 4:01 PM
Points: 835, Visits: 1,192
Do you already have Database named Test_Restore?
Post #1450217
Posted Tuesday, May 7, 2013 11:01 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, November 21, 2014 8:50 AM
Points: 95, Visits: 789
yes its already in instance
Post #1450257
Posted Tuesday, May 7, 2013 11:33 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, October 31, 2014 4:01 PM
Points: 835, Visits: 1,192
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.
Post #1450269
Posted Tuesday, May 7, 2013 12:05 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 6:07 PM
Points: 31,284, Visits: 15,744
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
Post #1450283
Posted Tuesday, May 7, 2013 12:08 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 11:25 AM
Points: 20,801, Visits: 32,725
Do not use sys.sysdatabases. It is a SQL Server 2000 system and is included for backward compatibility. You should use sys.databases instead.



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)
Post #1450285
Posted Wednesday, May 8, 2013 7:17 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, November 21, 2014 8:50 AM
Points: 95, Visits: 789
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
Post #1450544
Posted Wednesday, May 8, 2013 7:21 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, November 21, 2014 8:50 AM
Points: 95, Visits: 789
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
Post #1450550
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse