Dynamic SQL query with Columns

Trying to run this code not sure why i am getting an error

Declare @sql nvarchar(4000), @Table varchar(25), @BatchNo varchar(25)
, @BatchCol varchar(25),@IDColumn varchar(25), @Max int, @Loopcount int
,@returnValue int, @Statement nvarchar(4000)

set @Statement = N'
SELECT @result = Count(1) 
from [CKOLTP_Staging].dbo.[@TableValue] tbl (nolock)
where tbl.[@BatchColValue] = Convert(varchar(10),@BatchNoValue)'
exec sp_executesql @Statement, 
    N'@TableValue varchar(25)
	,@BatchColValue varchar(25) 
	,@BatchNoValue varchar(25)
	,@result INT OUTPUT', 
    @TableValue = @Table
	,@BatchColValue = @BatchCol
	,@BatchNoValue = @BatchNo 
	,@result=@returnValue OUTPUT    

Open in new window

Msg 208, Level 16, State 1, Line 5
Invalid object name 'CKOLTP_Staging.dbo.@TableValue'.

Open in new window

Can we not have the column as a variable?
Leo TorresSQL DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Brian CroweDatabase AdministratorCommented:
set @Statement = N'
SELECT @result = Count(1)
from [CKOLTP_Staging].dbo.[' + @TableValue + '] tbl (nolock)
where tbl.[' + @BatchColValue + '] = Convert(varchar(10),' + @BatchNoValue + ')'
Scott PletcherSenior DBACommented:
No.  No part of any object name in SQL can be a variable.

You need to do replaces in the statement before executing it:

set @Statement_template = N'
SELECT @result = Count(1)
from [CKOLTP_Staging].dbo.[@TableValue] tbl (nolock)
where tbl.[@BatchColValue] = Convert(varchar(10),@BatchNoValue)'

set @statement = replace(replace(@statement_template,
    '@TableValue', @TableValue),
    '@BatchColValue', @BatchColValue)

exec sp_executesql @Statement,
    N',@BatchNoValue varchar(25)
      ,@result INT OUTPUT',
      ,@returnValue OUTPUT

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
Leo TorresSQL DeveloperAuthor Commented:
Yes, I ended up doing that. I didnt know you cant do this with objects
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
Query Syntax

From novice to tech pro — start learning today.