Solved

Stored Procedure Bug

Posted on 2006-07-20
10
642 Views
Last Modified: 2008-02-07
I have a table tblBackupFiles in the Master database that stores the creationDate and BackupFileName of my files.  sp_DeleteOldBackupFiles gets files that are older than 7 days and deletes them.  When I try to compile sp_DeleteOldBackupFiles, I get:

Server: Msg 170, Level 15, State 1, Procedure sp_DeleteOldBackupFiles, Line 17
Line 17: Incorrect syntax near '+'.

Code:
Create Procedure sp_DeleteOldBackupFiles
AS
Declare @FileName as varchar(2000)


DECLARE CurDBNames CURSOR FOR Select BackupFileName From Master.dbo.tblBackupFiles Where CreationDate < getdate()-7


   OPEN CurDBNames
   FETCH NEXT FROM CurDBNames INTO @FileName
 
   WHILE @@FETCH_STATUS = 0
   BEGIN
            
            EXEC master.xp_shellcmd 'Del "' + @FileName +'"'
            FETCH NEXT FROM CurDBNames INTO @FileName
   END

   CLOSE CurDBNames
   DEALLOCATE CurDBNames


GO
0
Comment
Question by:benc007
10 Comments
 
LVL 7

Expert Comment

by:FDzjuba
ID: 17150143
you can only pass variables or completed string to SP

Create Procedure sp_DeleteOldBackupFiles
AS
Declare @FileName as varchar(2000)


DECLARE CurDBNames CURSOR FOR Select BackupFileName From Master.dbo.tblBackupFiles Where CreationDate < getdate()-7


   OPEN CurDBNames
   FETCH NEXT FROM CurDBNames INTO @FileName
 
DECLARE @passString nvarchar(600);

   WHILE @@FETCH_STATUS = 0
   BEGIN
          SET @passString  = 'Del "' + @FileName +'"';
          EXEC master.xp_shellcmd @passString ;
          FETCH NEXT FROM CurDBNames INTO @FileName
   END

   CLOSE CurDBNames
   DEALLOCATE CurDBNames

0
 

Author Comment

by:benc007
ID: 17150495
The stored procedure gets created but I get an error:

Cannot add rows to sysdepends for the current stored procedure because it depends on the missing object 'master.xp_shellcmd'. The stored procedure will still be created.
0
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 17150520
it should be


master.dbo.xp_cmdshell
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:benc007
ID: 17150655
Will master.dbo.xp_cmdshell work on both Windows XP and Windows 2000 Server?
0
 

Author Comment

by:benc007
ID: 17150789
I am trying to call sp_DeleteOldBackupFiles from another stored procedure sp_TEST tjhat is in the master database using:

exec sp_DeleteOldBackupFiles

but it's not working and I don't get an error when I compile sp_TEST.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17151295
benc007,
> Will master.dbo.xp_cmdshell work on both Windows XP and Windows 2000 Server?

Yes, if you have access to this

> but it's not working and I don't get an error when I compile sp_TEST

can you post the code
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17151297
Also check whether, by itself the sp is running ?
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 17151912
have you checked this...


declare @rc int
WHILE @@FETCH_STATUS = 0
   BEGIN
          SET @passString  = 'Del "' + @FileName +'"';
          print @passstring   -- for debug use certainly ... but i'd want to know what had been deleted...
          EXEC @rc = master.dbo.xp_shellcmd @passString ;
          print @rc
          FETCH NEXT FROM CurDBNames INTO @FileName
   END

 
0
 

Author Comment

by:benc007
ID: 17152396
LowFatSpread,

I get an error:

Dropping Procedure sp_DeleteOldBackupFiles
Creating Procedure sp_DeleteOldBackupFiles

Cannot add rows to sysdepends for the current stored procedure because it depends on the missing object 'master.dbo.xp_shellcmd'. The stored procedure will still be created.
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 50 total points
ID: 17152532
>'master.dbo.xp_shellcmd'. The stored procedure will still be created.

this should be    

  MASTER.DBO.XP_CMDSHELL
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
multiple application databases same MSSQL instance 5 53
MS SQL Server Management Studio, Save query 2 18
Attaching Database Failed ? 3 38
insert wont work in SQL 14 19
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question