Solved

Stored Procedure Bug

Posted on 2006-07-20
10
650 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

624 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