Dynamic query runs directly but not through variable, what could be the reason?
Posted
by waheed
on Stack Overflow
See other posts from Stack Overflow
or by waheed
Published on 2010-05-08T00:46:01Z
Indexed on
2010/05/08
2:08 UTC
Read the original article
Hit count: 481
Here is my scenario, I'm creating a dynamic query using a select statement which uses functions to generate the query. I am storing it into a variable and running it using exec. i.e.
declare @dsql nvarchar(max)
set @dsql = ''
select @dsql = @dsql + dbo.getDynmicQuery(column1, column2)
from Table1
exec(@dsql)
Now it produces the many errors in this scenario, like 'Incorrect syntax near ','' and 'Case expressions may only be nested to level 10.'
But if i take the text from @dsql and assign it a variable manually like:
declare @dsql nvarchar(max)
set @dsql = ''
set @dsql = N'<Dynamic query text>'
exec(@dsql)
it runs and generates the result, what could be the reason for that ??
Thanks..
© Stack Overflow or respective owner