sql server - how to pivot result based upon physical column name -


pivot result based upon physical table's column

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: enter image description here

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

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -