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


default data/log file location


default data/log file location

Author
Message
Adam Angelini
Adam Angelini
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2532 Visits: 1177
I'm struggling to find a way to get the default data and log locations using t-sql. Is there a way to either do this without reading the registry? if not, does anyone have a script that will grab those values from the registry regardless of default/named instance?

I have one that works well for default instance, but I need one I can drop on any server and have it grab those values correctly.

Any help would be greatly appreciated.
Thanks
Adam Bean
Adam Bean
SSCertifiable
SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)

Group: General Forum Members
Points: 6212 Visits: 2222
Data:

-- ***************************************************************************
-- nvarchar(4000) = dbo.fn_SQLServerDataDir()
-- ***************************************************************************
IF OBJECT_ID('SQLServerDataDir_fn','FN') IS NOT NULL
DROP FUNCTION SQLServerDataDir_fn
GO

create function dbo.SQLServerDataDir_fn()
returns nvarchar(4000)
as
begin

declare @rc int,
@dir nvarchar(4000)
exec @rc = master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\MSSQLServer',N'DefaultData', @dir output, 'no_output'

if (@dir is null)
begin
exec @rc = master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\Setup',N'SQLDataRoot', @dir output, 'no_output'
select @dir = @dir + N'\Data'
end

return @dir

end





Log

-- ***************************************************************************
-- nvarchar(4000) = dbo.SQLServerLogDir_fn()
-- ***************************************************************************
IF OBJECT_ID('SQLServerLogDir_fn','FN') IS NOT NULL
DROP FUNCTION SQLServerLogDir_fn
GO

create function dbo.SQLServerLogDir_fn()
returns nvarchar(4000)
as
begin

declare @rc int,
@dir nvarchar(4000)

exec @rc = master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\MSSQLServer',N'DefaultLog', @dir output, 'no_output'

if (@dir is null)
begin
exec @rc = master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',N'Software\Microsoft\MSSQLServer\Setup',N'SQLDataRoot', @dir output, 'no_output'
select @dir = @dir + N'\Data'
end

return @dir

end



---
SQLSlayer
Making SQL do what we want it to do.
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