I have one table #table with columns columnname,value
having contents say...
Columnname value
XYZ NUll
ABC null
another table I have say #datatable with Columns XYZ, ABC, LMN and having data
XYZ ABC LMN
1 2 3
I need to update first #table the result of the sql query as,
ColumnName Value
XYZ 1
ABC 2
Please help
here is what I tried
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(columnname)
from #table
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
print @cols
set @query = N'SELECT ' + @cols + N' from
#datatable
unpivot
(
value
for columnname in (' + @cols + N')
) p '
but error
Msg 207, Level 16, State 1, Line 1
Invalid column name 'XYZ'.
Msg 207, Level 16, State 1, Line 1
Invalid column name 'ABC'.