日期:2014-05-17 浏览次数:20727 次
use tempdb
if object_id('tb1') is not null drop table tb1
go
create table tb1
(id int ,username nvarchar(20))
go
insert into tb1
select 1,'公司1'
union all
select 2,'公司2'
union all
select 3,'公司3'
union all
select 4,'公司4'
union all
select 5,'公司5'
if object_id('tb2') is not null drop table tb2
go
create table tb2
(id int ,proName nvarchar(20))
go
insert into tb2
select 1,'产品1'
union all
select 2,'产品2'
union all
select 3,'产品3'
union all
select 4,'产品4'
union all
select 5,'产品5'
declare @sql nvarchar(500)
set @sql=''
select @sql=@sql +', 0 as '+ proName from tb2
select @sql='select username '+@sql + ' from tb1 '
print(@sql)
exec (@sql)
/***
select username , 0 as 产品1, 0 as 产品2, 0 as 产品3, 0 as 产品4, 0 as 产品5 from tb1
username 产品1 产品2 产品3 产品4 产品5
-------------------- ----------- ----------- ----------- ----------- -----------
公司1 0 0 0 0 0
公司2 0 0 0 0 0
公司3 0 0 0 0 0
公司4 0 0 0 0 0
公司5 0 0 0 0 0
(5 行受影响)
***/