Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Stored Procedure Bug

Posted on 2006-07-20
10
Medium Priority
?
651 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 Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 

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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

721 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