Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

script to delete old backup jobs

 
   Database Help (Home) -> Tools RSS
Next:  Cannot Install SQL Server 2005 SP2  
Author Message
opokad06

External


Since: Apr 22, 2008
Posts: 25



(Msg. 1) Posted: Tue Nov 25, 2008 8:58 am
Post subject: script to delete old backup jobs
Archived from groups: microsoft>public>sqlserver>tools (more info?)

I used the code below, what the difference between @command and fullPathName?



CREATE TABLE #BackupFiles
(FullPathName NVARCHAR(256))


DECLARE @Command NVARCHAR(256)


SET @Command = 'master.dbo.xp_cmdshell "DIR '
+ 'D:\Program Files\Microsoft SQL Server\MSSQL\Backup\' + '*.* /B /S"'
INSERT INTO #BackupFiles EXEC (@Command)


DECLARE @DeleteFiles NVARCHAR(4000)

DECLARE file_cursor CURSOR FOR

SELECT ' exec xp_cmdshell ''DEL "'
+ FullPathName + '"''' +CHAR(13)+CHAR(10)
FROM #BackupFiles
WHERE (FullPathName LIKE '%_%.BAK')
-- Trims punctuation and space from date time string
AND RIGHT(FullPathName,16) <
REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(16),
DATEADD(month, -1, GETDATE()),120) ,' ',''),':',''),'-','')


OPEN file_cursor
FETCH NEXT FROM file_cursor INTO @DeleteFiles


WHILE @@FETCH_STATUS = 0
BEGIN
EXEC (@DeleteFiles)
FETCH NEXT FROM file_cursor INTO @DeleteFiles
END


CLOSE file_cursor
DEALLOCATE file_cursor
--
KPoku

 >> Stay informed about: script to delete old backup jobs 
Back to top
Login to vote
Russell Fields

External


Since: Jan 10, 2008
Posts: 608



(Msg. 2) Posted: Tue Nov 25, 2008 2:07 pm
Post subject: Re: script to delete old backup jobs [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

KPoku

>I used the code below, what the difference between @command and
>fullPathName?

@command is set to run the command:
master.dbo.xp_cmdshell "DIR D:\Program Files\Microsoft SQL
Server\MSSQL\Backup\*.* /B /S"

However, this is incorrect. Perhaps I mixed up the single quotes and double
quotes. You want your set of @Command to look like this:

SET @Command = 'master.dbo.xp_cmdshell ''DIR '
+ '"D:\Program Files\Microsoft SQL Server\MSSQL\Backup\' + '*.*" /B /S'''



Second, what is in fullPathName after the EXECUTE(@Command)? The contents
of the DIR command are inserted into #BackupFiles, so each row of that table
has one column, FullPathName, with the full path qualified name of a file.
E.g.

D:\Program Files\Microsoft SQL Server\MSSQL\Backup\Database_081104.BAK
D:\Program Files\Microsoft SQL Server\MSSQL\Backup\Database_081105.BAK
D:\Program Files\Microsoft SQL Server\MSSQL\Backup\Database_081106.BAK

RLF

-- My response to your other thread with your latest question. --
If everything is set up correctly (rights to the drive, and so forth) then:

SET @Command = 'master.dbo.xp_cmdshell ''DIR '
+ '"D:\Program Files\Microsoft SQL Server\MSSQL\Backup\' + '*.*" /B /S'''

-- This will insert the full pathnames into #BackupFiles
INSERT INTO #BackupFiles EXEC (@Command)

Since you are not familiar with this syntax, you should read the details on
INSERT...EXEC in the Books Online for 2005, especially the examples toward
the end of:
http://msdn.microsoft.com/en-us/library/ms174335(SQL.90).aspx

Just try running that part of the script, then selecting from #BackupFiles
to see what you are getting.

RLF

RLF


"opokad06" wrote in message

>I used the code below, what the difference between @command and
>fullPathName?
>
>
>
> CREATE TABLE #BackupFiles
> (FullPathName NVARCHAR(256))
>
>
> DECLARE @Command NVARCHAR(256)
>
>
> SET @Command = 'master.dbo.xp_cmdshell "DIR '
> + 'D:\Program Files\Microsoft SQL Server\MSSQL\Backup\' + '*.* /B /S"'
> INSERT INTO #BackupFiles EXEC (@Command)
>
>
> DECLARE @DeleteFiles NVARCHAR(4000)
>
> DECLARE file_cursor CURSOR FOR
>
> SELECT ' exec xp_cmdshell ''DEL "'
> + FullPathName + '"''' +CHAR(13)+CHAR(10)
> FROM #BackupFiles
> WHERE (FullPathName LIKE '%_%.BAK')
> -- Trims punctuation and space from date time string
> AND RIGHT(FullPathName,16) <
> REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(16),
> DATEADD(month, -1, GETDATE()),120) ,' ',''),':',''),'-','')
>
>
> OPEN file_cursor
> FETCH NEXT FROM file_cursor INTO @DeleteFiles
>
>
> WHILE @@FETCH_STATUS = 0
> BEGIN
> EXEC (@DeleteFiles)
> FETCH NEXT FROM file_cursor INTO @DeleteFiles
> END
>
>
> CLOSE file_cursor
> DEALLOCATE file_cursor
> --
> KPoku

 >> Stay informed about: script to delete old backup jobs 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Sp and Jobs - Hi when I put this update statement <font color=purple> ; UPDATE PContracts</font> <font color=purple> ; SET Status = 'Expire'</font> <font color=purple> ; WHERE DATEDIFF(day, EndDate, GETDATE()) &...

ssis update or delete - hello all, i know ssis allows to insert from a csv file into sqlserver (2005) tables but is it possible to either update and/or delete records with an ssis package based on a csv file? thanks for you help.

tool to script data - Is there a tool available to script data ? e.g. Table1 has 10 rows , so the tool would script 10 insert into statements

An error has occured in a script on this page... - Hi Running SQL Server 2000, SP4 Every time I start up Enterprise Manager, choose server, click on a database - what should be displayed is some overview of the size of the database and other statistics. But instead I get a dialog box from Internet..

Running Large SQL Script on a Schedule - Using SQL 2000, SP4. My ERP Vendor has provided me with a large T-SQL script that reindexes all tables in the ERP database. I need to run it weekly. It is too large to copy/paste into a new DB Maintenace Plan or a new Job. Is there a way to "call...
   Database Help (Home) -> Tools All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]