创建一个包含exec的视图

时间:2016-03-23 13:04:56

标签: sql sql-server sql-server-2008 sql-server-2012

我想创建以下语句的视图:

declare @tab1 varchar(20);

declare @tab2 varchar(20);

declare @tab3 varchar(20);

select @tab1 = tablet from result where RANK = 1;

select @tab2 = tablet from result where RANK = 2;

select @tab3 = tablet from result where RANK = 3;


exec('
SELECT [1] AS '+@tab1+', [2] AS '+@tab2+', [3] AS '+@tab3+'
FROM (SELECT RANK, time, row_number() OVER (partition BY koy
ORDER BY time ASC) AS ranking
FROM result) s 
PIVOT (max(time) FOR rank IN ([1], [2], [3])) p');

1 个答案:

答案 0 :(得分:2)

您无法使用动态列名创建视图,它的定义必须是静态的。

不幸的是,你想要做的事情甚至不可能(允许)在表值多语句函数中,因为它们也不允许动态SQL执行。