Below sample showing dynamically get field from 2 table and join them for its similar fields and then join the field names into string after that execute to get its value.
declare @columns VARCHAR(max)
declare @sql VARCHAR(max)
select @columns=coalesce(@columns + ', ', '') + cast(t1.column_name AS varchar(100)) from (
select * from table_field_name
where table_name='table1'
) as t1
inner join
(select table_name, column_name from (
select * from table_field_name
where table_name='table2'
) as ta) t2
on t2.column_name=t1.column_name
set @sql = 'select ' + @columns + ' from ' + 'table1'
print @sql
exec (@sql)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment