create table tesst_table (name varchar(10),yuwen int,shuxue int,yingyu int)insert into tesst_table select '李大三',50,23,40 union all select '王小二',50,88,50 select name, case c when 'yuwen' then '语文' when 'shuxue' then '数学' when 'yingyu' then '英语' else '' end 'subject', v 'score' from tesst_table a unpivot(v for c in([yuwen],[shuxue],[yingyu])) u/* name subject score ---------- ------- ----------- 李大三 语文 50 李大三 数学 23 李大三 英语 40 王小二 语文 50 王小二 数学 88 王小二 英语 50(6 row(s) affected) */
运行查询语句是提示: Incorrect syntax near 'pivot'. You may need to set the compatibility level of the current database to a higher value to enable this feature. See help for the SET COMPATIBILITY_LEVEL option of ALTER DATABASE. 兼容级别不够执行了下: EXEC sp_dbcmptlevel databaseName,90 OK 了。非常感谢您的帮助!!!
create table tesst_table
(name varchar(10),yuwen int,shuxue int,yingyu int)insert into tesst_table
select '李大三',50,23,40 union all
select '王小二',50,88,50
select name,
case c when 'yuwen' then '语文'
when 'shuxue' then '数学'
when 'yingyu' then '英语' else '' end 'subject',
v 'score'
from tesst_table a
unpivot(v for c in([yuwen],[shuxue],[yingyu])) u/*
name subject score
---------- ------- -----------
李大三 语文 50
李大三 数学 23
李大三 英语 40
王小二 语文 50
王小二 数学 88
王小二 英语 50(6 row(s) affected)
*/
Incorrect syntax near 'pivot'. You may need to set the compatibility level of the current database to a higher value to enable this feature. See help for the SET COMPATIBILITY_LEVEL option of ALTER DATABASE.
兼容级别不够执行了下:
EXEC sp_dbcmptlevel databaseName,90 OK 了。非常感谢您的帮助!!!