如何从这个查询或CTE查看?

时间:2018-05-24 18:21:19

标签: tsql

以下是我正在尝试使用以下代码创建的代码:

DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);

SELECT @cols = STUFF((SELECT distinct
',' +
QUOTENAME( stsd.TRM_CDE + stsd.YR_CDE) as termyear
FROM STUD_TERM_SUM_DIV stsd
WHERE stsd.TRANSACTION_STS IN ('C','H','R','P') AND stsd.YR_CDE IN ('1415','1516','1617','1718') AND stsd.TRM_CDE IN ('FA','LF','SP','LS','SU','LU') and stsd.DIV_CDE = 'GR'
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)') 
,1,1,'');`

1 个答案:

答案 0 :(得分:2)

Transact-SQL(Microsoft SQL Server)不允许在视图定义中进行变量声明。

抛弃变量声明和@cols =

CREATE VIEW myviewname (mycolname) AS

之前添加SELECT

我们可以为外部查询返回的teh表达式指定一个别名(列名)。这将允许我们省略CREATE VIEW中的列名列表。

CREATE VIEW myviewname (foo)
AS
SELECT STUFF(
          ...
       ) AS foo