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
性别 姓名 身高 体重 年龄 体重 年龄
---- -------- ------ ----- ------
*/
自定义表列
最新推荐文章于 2020-10-11 19:37:31 发布