SQL Server 2008 R2 - Dynamic Stored Procedure Problem

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
Amour22015Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
@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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Amour22015Author Commented:
Hi,

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

Never mind I found the problem..  Thanks
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Vitor MontalvãoMSSQL Senior EngineerCommented:
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
Amour22015Author Commented:
Thanks for all your help
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
You are very welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.