Problems with sp_dropdevice

  • I have a backup script that creates a backup device for each day, and uses it for TLog backups.  Well, the script that I'm having problems with is the one that goes back and deletes any device older than 7 days, including datafiles.  here's the error I get:
     
    Server: Msg 170, Level 15, State 1, Line 29

    Line 29: Incorrect syntax near 'sp_dropdevice'.

    Server: Msg 170, Level 15, State 1, Line 1

    Line 1: Incorrect syntax near ','.

     
    And here's my entire SP script:
     
    -- This will delete the BackupDevice for the last week

    use master

    --Declare Variables

    SET NOCOUNT ON

    DECLARE @DeviceName varchar(255)

    DECLARE @File varchar(255)

    DECLARE @Creation varchar(255)

    DECLARE @OldPlus varchar(255)

     
    -- Declare the cursor

    DECLARE bud CURSOR FOR

      SELECT BackupDeviceName,

             BackupDeviceFile,

           BackupDeviceCreation

      FROM BackupDevice

    -- Open the cursor

    OPEN bud

    -- Loop through the cursor

    FETCH NEXT

       FROM bud

       INTO @DeviceName,

            @File,

            @Creation

    WHILE @@FETCH_STATUS = 0

    BEGIN

     SET @OldPlus = DATEADD(day,7,@Creation)

     IF @OldPlus < @Creation

      BEGIN

       sp_dropdevice @DeviceName,'DELFILE'

       GO

       DELETE BackupDeviceName,

              BackupDeviceFile,

                 BackupDeviceCreation

       FROM BackupDevice

       WHERE BackupDeviceName = @DeviceName AND

       BackupDeviceFile = @File AND

       BackupDeviceCreation = @Creation

      END

    END

    CLOSE bud

    DEALLOCATE bud

     
    Any insight would be very much appreciated.
     
    Thank You,
    tibby
  • Try changing sp_dropdevice @DeviceName,'DELFILE'

    to

    execute sp_dropdevice @DeviceName,'DELFILE'

    /Kenneth

  • Thank you for the help.  I also had another small problem with the fetching, but got it solved.  Here's the final version of the script that is working like a wet dream:
     
    -- This will delete the BackupDevice for the last week

    use master

    --Declare Variables

    DECLARE @DeviceName varchar(255)

    DECLARE @File varchar(255)

    DECLARE @Creation datetime

    DECLARE @OldPlus datetime

    -- Declare the cursor

    DECLARE bud CURSOR FOR

      SELECT BackupDeviceName, BackupDeviceFile, BackupDeviceCreation

      FROM BackupDevice

      ORDER BY BackupDeviceCreation

    -- Open the cursor

    OPEN bud

    -- Loop through the cursor

    FETCH NEXT FROM bud

       INTO @DeviceName, @File, @Creation

    WHILE @@FETCH_STATUS = 0

    BEGIN

     SET @OldPlus = DATEADD(day, 7, @Creation)

     IF convert(varchar(50), @OldPlus, 101) <= convert(varchar(50),getdate(),101)

      BEGIN

       EXEC sp_dropdevice @DeviceName, DELFILE

       DELETE FROM BackupDevice

       WHERE BackupDeviceName = @DeviceName AND BackupDeviceFile = @File AND BackupDeviceCreation = @Creation

      END

     FETCH NEXT FROM bud

        INTO @DeviceName, @File, @Creation

    END

    CLOSE bud

    DEALLOCATE bud

     
    Thank you again, I never would have gotten it to work without your insight.
     
    tibby
  • what about the physical file??? i dont see any action to delete it?? what will happens when the DBbackups fill the HD??

  • To see the answer to that question, please open up BOL and search for 'sp_dropdevice'

    /Kenneth

  • thanks for the reply my bad

  • No problems.

    It's just that many forget how good BOL is, so I use to redirect there rather than just state the answers. In BOL you can most of the times not only read how, but also why stuff is like stuff is.

    /Kenneth

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply