求最简洁.通用性强的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
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
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28