September 24, 2008 at 8:55 pm
I want to schedule a job to create subdirectories named by the user database names under K:\myfile. If subdirectories already exist, no need to create, if new database name, then create the subdirectory.
Does anyone know how to do it?
September 25, 2008 at 4:11 am
Hi,
I have written stored procedure 'create_DBFolders ' for you. This will do your work. Just create the sp and schedule this sp by creating new job.
CREATE PROCEDURE create_DBFolders
AS
BEGIN
--Declare Variables
DECLARE @DBname VARCHAR(300)
DECLARE @Path VARCHAR(300)
--Get Directory Names
CREATE TABLE #DIR_NAMES ( DIRNAME VARCHAR(300))
INSERT INTO #DIR_NAMES exec xp_cmdshell 'DIR /B C:\MYFILE\'
--Get only User Database Names
CREATE TABLE #DB_NAMES (DBNAME VARCHAR(300))
INSERT INTO #DB_NAMES SELECT name FROM sys.databases where database_id >4
--Get DBnames which does not have folder till now i.e
DECLARE cur_DBname CURSOR FOR
SELECT DBNAME FROM #DB_NAMES WHERE DBNAME NOT IN (SELECT DIRNAME FROM #DIR_NAMES WHERE DIRNAME <>'NULL')
OPEN cur_DBname
FETCH NEXT FROM cur_DBname INTO @DBname
--Create one directory for each database.
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Path = 'MD C:\myfile\'+@DBname
exec XP_CMDSHELL @Path
FETCH NEXT FROM cur_DBname INTO @DBname
END
DROP TABLE #DIR_NAMES
DROP TABLE #DB_NAMES
CLOSE cur_DBname
DEALLOCATE cur_DBname
END
Please let me know if you have any questions on this.
Have a Great Day
Rajesh Kasturi
September 25, 2008 at 12:38 pm
Addition to what rajesh provided you:
Alter PROCEDURE create_DBFolders
AS
BEGIN
--Declare Variables
DECLARE @DBname VARCHAR(300)
DECLARE @Path VARCHAR(300)
DECLARE @backup VARCHAR(300)
--Get Directory Names
CREATE TABLE #DIR_NAMES ( DIRNAME VARCHAR(300))
INSERT INTO #DIR_NAMES exec xp_cmdshell 'DIR /B C:\MYFILE\'
--Get only User Database Names
CREATE TABLE #DB_NAMES (DBNAME VARCHAR(300))
INSERT INTO #DB_NAMES SELECT name FROM sys.databases where database_id >4
--Get DBnames which does not have folder till now i.e
DECLARE cur_DBname CURSOR FOR
SELECT DBNAME FROM #DB_NAMES WHERE DBNAME NOT IN (SELECT DIRNAME FROM #DIR_NAMES WHERE DIRNAME <>'NULL')
OPEN cur_DBname
FETCH NEXT FROM cur_DBname INTO @DBname
--Create one directory for each database.
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Path = 'MD C:\myfile\'+@DBname
exec XP_CMDSHELL @Path
FETCH NEXT FROM cur_DBname INTO @DBname
END
DROP TABLE #DIR_NAMES
CLOSE cur_DBname
DEALLOCATE cur_DBname
DECLARE cur_DBnamebackup CURSOR FOR
SELECT DBNAME FROM #DB_NAMES
OPEN cur_DBnamebackup
FETCH NEXT FROM cur_DBnamebackup INTO @DBname
--Create one directory for each database.
WHILE @@FETCH_STATUS = 0
BEGIN
Set @backup='Backup Database '+@DBname+' To disk='+''''+'C:\myfile\'+@DBname+'\'+@DBname+'.bak'+''''+' With INIT'
Print (@backup)
Exec (@backup)
FETCH NEXT FROM cur_DBnamebackup INTO @DBname
END
DROP TABLE #DB_NAMES
CLOSE cur_DBnamebackup
DEALLOCATE cur_DBnamebackup
END
The above code will take backup too.
Thanks Rajesh.
HTH
MJ
September 25, 2008 at 8:32 pm
Thank you. This is exactly what I want.
I like this part very much, plus your have comments in your code.
--Get Directory Names
CREATE TABLE #DIR_NAMES ( DIRNAME VARCHAR(300))
INSERT INTO #DIR_NAMES exec xp_cmdshell 'DIR /B C:\MYFILE\'
one minor change: For SQL2000,
INSERT INTO #DB_NAMES SELECT name FROM sys.databases where database_id >4
should be:
INSERT INTO #DB_NAMES SELECT name FROM sysdatabases where dbid >4
September 25, 2008 at 8:43 pm
I felt embarrassing to ask code from the others for this question. I thought or the others would think I am a lazy DBA not to write the code by myself. Anyway, I was too busy to work on this. The good thing is there is always someone willing to share and help. Plus, I learned different coding style from the others.
September 25, 2008 at 11:06 pm
Hi Vivien Xing,
I am also feeling so happy because the code is working for you
Rajesh Kasturi
September 25, 2008 at 11:07 pm
Thanks MANU for modifying the code to take a backup
Rajesh Kasturi
May 6, 2009 at 3:52 am
Hi,
Is this similar to ddl triggers? I'm new to sql server dba and coding is not my strongest part. I am looking for a script that will create backup devices and jobs for me when a new db is created. I have no control over this as an application creates the db. Any assistance would be greatly appreciated.
Thanks
J
May 7, 2009 at 10:51 am
create the stored procedure; create a SQL scheduled job to exec this stored procedure in the job step.
May 8, 2009 at 11:32 am
Just a side note, but starting with SQL 2005, there is a system procedure that will do this for you: xp_create_subdir
Yet another reason to consider upgrading.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy