+ -
当前位置:首页 → 问答吧 → 求最简洁.通用性强的SQL语句

求最简洁.通用性强的SQL语句

时间:2011-11-04

来源:互联网

SQL code
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[t1]') and OBJECTPROPERTY(id,N'IsUserTable') = 1) drop table [dbo].[t1]
create table t1(cid varchar(10),cname nvarchar(10))
insert into t1 
select 'col_1','名字' union all
select 'col_2','年龄' union all
select 'col_3','性別' union all
select 'col_4','电话' union all
select 'col_5','地址' 
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[t2]') and OBJECTPROPERTY(id,N'IsUserTable') = 1) drop table [dbo].[t2]
create table t2(id int,col_1 varchar(10),col_2 tinyint,col_3 char(2),col_4 varchar(15),col_5 varchar(150))
insert into t2 
select 1,'张三',20,'M','123456789','test' union all
select 2,'李四',18,'W','123456789','test' union all
select 3,'王五',21,'M','123456789','test' union all
select 4,'朱六',25,'W','123456789','test' union all
select 5,'刘七',22,'M','123456789','test'
go
所需結果如下: 
名字      年龄  性別  电话            地址
---------- ----- ----- --------------- -------------
张三         20    M     123456789       test
李四         18    W     123456789       test
王五         21    M     123456789       test
朱六         25    W     123456789       test
刘七         22    M     123456789       test


作者: dovei   发布时间: 2011-11-04

有人曾经设计过这中表结构,无比的纠结

作者: ssp2009   发布时间: 2011-11-04

楼上的知道怎么写么?

作者: dovei   发布时间: 2011-11-04

SQL code

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[t1]') and OBJECTPROPERTY(id,N'IsUserTable') = 1) drop table [dbo].[t1]
create table t1(cid varchar(10),cname nvarchar(10))
insert into t1 
select 'col_1','名字' union all
select 'col_2','年龄' union all
select 'col_3','性別' union all
select 'col_4','电话' union all
select 'col_5','地址' 
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[t2]') and OBJECTPROPERTY(id,N'IsUserTable') = 1) drop table [dbo].[t2]
create table t2(id int,col_1 varchar(10),col_2 tinyint,col_3 char(2),col_4 varchar(15),col_5 varchar(150))
insert into t2 
select 1,'张三',20,'M','123456789','test' union all
select 2,'李四',18,'W','123456789','test' union all
select 3,'王五',21,'M','123456789','test' union all
select 4,'朱六',25,'W','123456789','test' union all
select 5,'刘七',22,'M','123456789','test'

declare @sql varchar(2000) set @sql=''
select @sql=coalesce(@sql+',','')+[Name]+' as '+isnull(b.cname,a.name)  
from syscolumns a left join t1 b on a.name=b.cid where object_id('t2')=id
set @sql='select '+right(@sql,len(@sql)-1)+' from t2'
exec(@sql)
/*
id          名字         年龄   性別   电话              地址
----------- ---------- ---- ---- --------------- -------------------
1           张三         20   M    123456789       test
2           李四         18   W    123456789       test
3           王五         21   M    123456789       test
4           朱六         25   W    123456789       test
5           刘七         22   M    123456789       test
*/

作者: maco_wang   发布时间: 2011-11-04

把T1列转行再与T2 join

作者: fredrickhu   发布时间: 2011-11-04

这样用
SQL code
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[t1]') and OBJECTPROPERTY(id,N'IsUserTable') = 1) drop table [dbo].[t1]
create table t1(cid varchar(10),cname nvarchar(10))
insert into t1 
select 'col_1','名字' union all
select 'col_2','年龄' union all
select 'col_3','性別' union all
select 'col_4','电话' union all
select 'col_5','地址' 
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[t2]') and OBJECTPROPERTY(id,N'IsUserTable') = 1) drop table [dbo].[t2]
create table t2(id int,col_1 varchar(10),col_2 tinyint,col_3 char(2),col_4 varchar(15),col_5 varchar(150))
insert into t2 
select 1,'张三',20,'M','123456789','test' union all
select 2,'李四',18,'W','123456789','test' union all
select 3,'王五',21,'M','123456789','test' union all
select 4,'朱六',25,'W','123456789','test' union all
select 5,'刘七',22,'M','123456789','test'
go

declare @s nvarchar(4000)
select @s=isnull(@s+',','select ')+QUOTENAME(cname)+'='+cid from t1
exec(@s+' from t2')

/*
名字    年龄    性別    电话    地址
张三    20    M     123456789    test
李四    18    W     123456789    test
王五    21    M     123456789    test
朱六    25    W     123456789    test
刘七    22    M     123456789    test
*/

作者: roy_88   发布时间: 2011-11-04