February 6, 2006 at 5:42 am
In TABLE1 DATA IS LIKE THIS
COL1 COL2 COL3
ENO ENAME DEPTNO (This is DAta)
In TABLE2 DATA IS LIKE THIS
col1 col2 COL3
1 XXXXX 10
2 YYYYY 20
i want to get the output as
ENO ENAME DEPTNO
1 XXXXX 10
2 YYYYY 20
IF we make union we will get i know but my output is colnames have to come from table1 data.Is it possible ?
February 6, 2006 at 6:51 am
Is this fine with you ?
create table #table1
(
colidint identity(1,1),
col1varchar(50),
col2varchar(50),
col3varchar(50)
)
insert into #table1 (col1, col2, col3)
select'ENO', 'ENAME', 'DEPTNO'
create table #table2
(
col1int,
col2varchar(100),
col3int
)
insert into #table2
select1, 'XXXXX', 10union all
select2, 'YYYYY', 20
declare
@sqlnvarchar(1000),
@col1_namevarchar(50),
@col2_namevarchar(50),
@col3_namevarchar(50)
select@col1_name = col1,
@col2_name = col2,
@col3_name = col3
from#table1
wherecolid= 1
select@sql = 'SELECT' + char(13)
select@sql = @sql + N'col1 as ' + @col1_name + ',' + char(13)
select@sql = @sql + N'col2 as ' + @col2_name + ',' + char(13)
select@sql = @sql + N'col3 as ' + @col3_name + ' ' + char(13)
select@sql = @sql + N'FROM #table2'+ char(13)
exec sp_executesql @sql
drop table #table1
drop table #table2
February 6, 2006 at 11:22 pm
Thanks for your reply!!i will work on that and i will get back to u
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply