Whenever I attempt to set myself up as an expert on anything, a Higher Being seems to belabour me with a metaphorical baseball bat for my lack of humility. In consequence, this Blog entry is really more of a question than a nugget of information. How does one change the default backup directory for a server by using the SSMS or Enterprise Manager, without changing all the default directories at the same time?. What is the TSQL to do it? (the editor will send a Simple-Talk goodie bag for the best correct solution)
It is normally set to a value on installation and the value is sitting there smugly in the registry, but I'm BOL'd out. (Can't find the information on Books-on-line). Here is the way I normally do it...... This routine allows you to see or change the server's Default Backup Directory.
CREATE PROCEDURE [dbo].[spDMODefaultBackupDirectory] (
@SourceServer VARCHAR(30),
@SourceUID VARCHAR(30)=NULL,
@SourcePWD VARCHAR(30)=NULL,
@toName VARCHAR(100) = NULL
)
AS
/*
spDMODefaultBackupDirectory 'MyServer'--see what the default is
spDMODefaultBackupDirectory 'MyServer', 'myuserid', 'mypassword'
--see what the default is
spDMODefaultBackupDirectory 'MyServer', @toName='g:\DatabaseBackups'
*/
DECLARE @objServer INT,
@strErrorMessage VARCHAR(255),
@objErrorObject INT,
@objRegistry INT,
@hr INT,
@DefaultBackupDirectory VARCHAR(255)
-- Sets the server to the local server
IF @SourceServer IS NULL SELECT @SourceServer = @@servername
SET NOCOUNT ON
SELECT @strErrorMessage = 'instantiating the DMO',
@objErrorObject=@objServer
EXEC @hr= sp_OACreate 'SQLDMO.SQLServer2', @objServer OUT
IF @SourcePWD IS NULL OR @SourceUID IS NULL
BEGIN
--use a trusted connection
IF @hr=0 SELECT @strErrorMessage=
'Setting login to windows authentication on '
+@SourceServer, @objErrorObject=@objServer
IF @hr=0 EXEC @hr=sp_OASetProperty @objServer, 'LoginSecure', 1
IF @hr=0 SELECT @strErrorMessage=
'logging in to the requested server using windows authentication on '
+@SourceServer
IF @SourceUID IS NULL AND @hr=0
EXEC @hr=sp_OAMethod @objServer, 'Connect', NULL, @SourceServer
IF @SourceUID IS NOT NULL AND @hr=0
EXEC @hr=sp_OAMethod
@objServer, 'Connect', NULL, @SourceServer ,@SourceUID
END
ELSE
BEGIN
IF @hr=0 SELECT @strErrorMessage = 'Connecting to '''+@SourceServer+
''' with user ID '''+@SourceUID+'''', @objErrorObject=@objServer
IF @hr=0
EXEC @hr=sp_OAMethod @objServer, 'Connect', NULL,
@SourceServer, @SourceUID, @SourcePWD
END
IF @hr=0 SELECT @strErrorMessage = 'accessing the registry',
@objErrorObject=@objServer
IF @hr=0 EXEC @hr=sp_OAGetProperty @objServer, 'Registry',
@objRegistry output
IF @hr=0 SELECT @strErrorMessage = 'Getting the default Backup directory',
@objErrorObject=@objRegistry
IF @hr=0 EXEC @hr=sp_OAGetProperty @objRegistry, 'Backupdirectory',
@DefaultBackupDirectory output
IF @toName IS NOT NULL AND @hr=0
BEGIN
IF @hr=0 SELECT @strErrorMessage = 'Changing the backup directory',
@objErrorObject=@objRegistry
IF @hr=0 EXEC @hr=sp_OASetProperty @objRegistry, 'Backupdirectory',
@toName
END
IF @hr=0 SELECT @strErrorMessage = 'disconnecting from the server',
@objErrorObject=@objServer
IF @hr=0 EXEC @hr=sp_OAMethod @objServer, 'DisConnect'
IF @hr=0 SELECT @DefaultBackupDirectory, COALESCE(@toName,'not changed')
IF @hr<>0
BEGIN
DECLARE
@Source VARCHAR(255),
@Description VARCHAR(255),
@Helpfile VARCHAR(255),
@HelpID INT
EXECUTE sp_OAGetErrorInfo @objErrorObject,
@source output,@Description output,@Helpfile output,@HelpID output
SELECT @hr, @source, @Description,@Helpfile,@HelpID output
SELECT @strErrorMessage='Error whilst '
+COALESCE(@strErrorMessage,'doing something')
+', '+COALESCE(@Description,'')
RAISERROR (@strErrorMessage,16,1)
END
EXEC sp_OADestroy @objServer
RETURN @hr
PS: Gary Sexton has pointed out a link http://sqldev.net./misc/SQLLocationFunctions.htm which should do the trick, but xp_instance_regread and xp_instance_regaddmultistring,
xp_instance_regdeletekey ,
xp_instance_regdeletevalue ,
xp_instance_regenumkeys ,
xp_instance_regenumvalues ,
xp_instance_regremovemultistring ,
and xp_instance_regwrite
...are all undocumented stored procedures and could easily change from release to release. Many thanks to Gary, but I really wanted a Kosher way of doing it!