January 17, 2012 at 10:36 am
Hi,
I'm trying to explore about sp_exectesql and the below query is showing the syntax error at '+mytable+'.
Please advice..
DECLARE @TblName varchar(50)
DECLARE @ColValue int
set @ColValue=30
set @tblName='MyTable'
execute sp_executesql N'Select * from '+ @Mytable + ' where somecol = @ColVal' , '@ColVal int', @ColValue;
January 17, 2012 at 11:10 am
DECLARE @TblName varchar(50)
DECLARE @ColValue int
set @ColValue=30
set @tblName='MyTable'
execute sp_executesql N'Select * from '+ QUOTENAME(@tblName) + ' where somecol = @ColVal' , '@ColVal int', @ColValue;
January 17, 2012 at 7:19 pm
You have to write the job like this:
DECLARE @tblName nvarchar(50)
DECLARE @sql nvarchar(200)
set @tblName='T1'
SET @sql = N'Select * from '+quotename(@tblName)
execute sp_executesql @sql
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply