Following is the query that I am executing. I am getting XML input from c# in @XMLdata parameter.
CREATE TABLE #TablesList
(
TableName VARCHAR(500),
RefTable VARCHAR(500),
RefTableIDColumn VARCHAR(500)
)
SET @Query = @Query + ' INSERT INTO #TablesList SELECT ref.value(''tablename[1]'',''nvarchar(500)'') AS tablename,'
SET @Query = @Query + ' ref.value(''refTable[1]'',''nvarchar(500)'') AS refTable, ref.value(''refTableIDColumn[1]'',''nvarchar(500)'') AS refTableIDColumn FROM '
SET @Query = @Query + @XMLdata+'.nodes(''//Table[@name="'+@DataItem+'"]'') AS R(ref)'
EXEC(@Query)
When I execute the query, I get the following error. Error is for the 2nd last line
The data types varchar(max) and xml are incompatible in the add operator.
@Queryand@XMLdata?XMLdata type implicitly into avarcharwhich isn't defined. If you need to access theXMLas anXMLdata type in the constructed query, you need to pass it, select it or otherwise construct it within the query you're trying to create.SELECT @Queryinstead ofEXEC(@Query)and you will see, that the query is not the way you expect it to be... So once again: Please tell us how you call this from C# and how you handle this in SQL.