自定义表列

USE tempdb
GO
IF OBJECT_ID('userInfo') IS NOT NULL DROP TABLE userInfo
GO
create table userInfo(
    userId int identity(1,1) primary key,
    userName nvarchar(20) not null,  	--姓名 必填
    birthday datetime not null,         	--生日,避免年龄总是变化
    age as datediff(year, birthday, getdate()),  --自动产生年龄
    gender nchar(1),		        	--性别
    c1 nvarchar(max),	 	--自定义列
    c2 nvarchar(max),	 	--自定义列
    c3 nvarchar(max),	 	--自定义列
    c4 nvarchar(max),	 	--自定义列
    c5 nvarchar(max),	 	--自定义列
    c6 nvarchar(max),	 	--自定义列
    c7 nvarchar(max),	 	--自定义列
    c8 nvarchar(max),	 	--自定义列
    c9 nvarchar(max),	 	--自定义列
    c10 nvarchar(max)	 	--自定义列
)
GO
IF OBJECT_ID('userInfoConfig') IS NOT NULL DROP TABLE userInfoConfig
GO
create table userInfoConfig(
    configId int identity(1,1)  primary key,
    colName varchar(50),
    colCustomeName   nvarchar(50),
    colSeq    int
)
GO
SET NOCOUNT ON
insert into userInfoConfig (colName,colCustomeName,colSeq)
              select 'userName','姓名',2
union all select 'age','年龄',5
union all select 'gender','性别',1
union all select 'c1','身高',3
union all select 'c2','体重',4

DECLARE @sql NVARCHAR(MAX)
SET @sql ='SELECT ' + STUFF(
	(SELECT  ',[' + colName + '] AS [' + colCustomeName+']' 
	FROM userInfoConfig ORDER BY colSeq
	FOR XML PATH(''))
,1,1,'') + ' FROM dbo.userInfo'

PRINT @sql
EXEC (@sql)
/*
SELECT [gender] AS [性别],[userName] AS [姓名],[c1] AS [身高],[c2] AS [体重],[age] AS [年龄] FROM dbo.userInfo
性别   姓名    身高  体重  年龄                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             体重                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               年龄
---- -------- ------ ----- ------

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值