[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL Server 2008 R2 - Dynamic Stored Procedure Problem

Posted on 2014-08-25
6
Medium Priority
?
617 Views
Last Modified: 2016-02-13
Hi experts,

I am executing a Stored Procedure and hard coding the parameter.

When I execute the SP I get an error:
Msg 245, Level 16, State 1, Procedure INBOUND_CLEANUP, Line 39
Conversion failed when converting the varchar value 'DELETE TOP(@inboundCommitCount)
                  FROM INBOUND_ADCAUSECD
                  WHERE INBOUND_ADCAUSECD.[FILE_ID] = ' to data type int.


Here is the SP:
ALTER PROCEDURE [dbo].[INBOUND_CLEANUP] 
      @FileId int = 543184-- File_Id from Database_Tables CLAIM

AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
            
     DECLARE
		    @inboundCommitCount int = 1000,
			@rc int,
            @sql nvarchar(4000),
            @name varchar(100)
            
            -- Processing nonmonthly inbound tables
            DECLARE db_cursor CURSOR FOR  
Select Name
From sys.tables
Where name like 'INBOUND_%' And ISNumeric(Replace(SUBSTRING(Name,len(name)-6,7),'_',''))=0

OPEN db_cursor   
FETCH NEXT FROM db_cursor INTO @name   

WHILE @@FETCH_STATUS = 0   
BEGIN   
--do purge stuff here 
     SET @rc = 1
     SELECT @sql = 'DELETE TOP(@inboundCommitCount)
                  FROM ' + @name + '
                  WHERE ' + @name + '.[FILE_ID] = ' +@fileId
                  
      
   
      WHILE @rc > 0
      BEGIN
            BEGIN TRAN
           exec sp_executesql @sql;          
                  SET @rc = @@ROWCOUNT
                  
      
            COMMIT TRAN
      END;     

       FETCH NEXT FROM db_cursor INTO @name   
END   

CLOSE db_cursor   
DEALLOCATE db_cursor
      CHECKPOINT
END;

Open in new window


Please help and thanks
0
Comment
Question by:Amour22015
  • 3
  • 3
6 Comments
 
LVL 53

Accepted Solution

by:
Vitor Montalvão earned 2000 total points
ID: 40282964
@fileId is INT data type and you are trying to convert it to string. So you should change your statement to:
     SELECT @sql = 'DELETE TOP(@inboundCommitCount)
                  FROM ' + @name + '
                  WHERE ' + @name + '.[FILE_ID] = ' + CONVERT(VARCHAR, @fileId)

Open in new window

0
 

Author Comment

by:Amour22015
ID: 40282978
Hi,

Now I am getting error:
Msg 137, Level 15, State 2, Line 1
Must declare the scalar variable "@inboundCommitCount".
0
 

Author Comment

by:Amour22015
ID: 40283026
Hi,

Never mind I found the problem..  Thanks
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.

 
LVL 53

Expert Comment

by:Vitor Montalvão
ID: 40283031
It's because you are passing the variable to the string but when it runs in another scope and then engine doesn't recognize that variable in that scope. Or you pass it as parameter or you can also convert it to string, like this:
   SELECT @sql = 'DELETE TOP(' + CONVERT(VARCHAR, @inboundCommitCount) + ')
                  FROM ' + @name + '
                  WHERE ' + @name + '.[FILE_ID] = ' + CONVERT(VARCHAR, @fileId)

Open in new window

0
 

Author Closing Comment

by:Amour22015
ID: 40283048
Thanks for all your help
0
 
LVL 53

Expert Comment

by:Vitor Montalvão
ID: 40283052
You are very welcome.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

829 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