I am writing a query which need to append the database name dynamically.
The query gets values from a table (in another database) , and I would interested to know if there is a better approach/way.
So my query looks like following :
DECLARE @TagNames AS VARCHAR(MAX) --Probably this is not a Global variable
DECLARE @QUERY AS VARCHAR(MAX)
SET @QUERY ='SELECT @TagNames = coalesce( @TagNames + '','','''') +
fldTagName FROM '+ dbo.fnGetZiConfigValue('KEYNAME')+'.dbo.tblTags Where
fldInterpolate = 1 AND fldUnitID = 13'
EXEC (@QUERY)
When I execute this , I get an error,
Must declare the scalar variable "@TagNames".