sql server - how to pivot result based upon physical column name -
i have 3 tables 1. master table m_mastertable 2. detail table of m_mastertable 3. actual physical table
how write query give me desire output, matching physical columns fieldname(2) in pivoting result?
id primary key detailed foreign key.
1:
have @ this:
--quick , dirty sample data declare @master table ( id int, tablename nvarchar(100) ) declare @detail table ( detailid int, tableid int, fieldname nvarchar(100), excelmappingcolumn nvarchar(100) ) insert @master values (1,n'a') insert @detail values (1,1,n'code',n'virtualcode') insert @detail values (2,1,n'value',n'value of virtual exam') -- getting query declare @stmt nvarchar(max) = '' declare @columns nvarchar(max) select @columns = coalesce(@columns + ',[','',@columns + ',[') + fieldname + '] [' + excelmappingcolumn + ']' @detail tableid = 1 select @stmt = 'select ' + @columns + ' ' + tablename @master m m.id = 1 print @stmt --execute exec sp_executesql @stmt
Comments
Post a Comment