使用cte的SQL视图将无法保存

时间:2013-10-05 13:39:48

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

我正在使用Microsoft SQL Server Management Studio 2008,但在创建View时遇到了问题。我有以下代码:

;WITH
    cteSample
    AS (
                    SELECT
                            vw_JobCosting_WIPMasterSub.SJob
                          , vw_JobCosting_WIPMasterSub.MJob
                          , vw_JobCosting_WIPMasterSub.MJob DisplayMJob
                          , vw_JobCosting_WIPMasterSub.GroupF
                          , 0 AS LVL
                    FROM vw_JobCosting_WIPMasterSub
                    WHERE vw_JobCosting_WIPMasterSub.GroupF = 'True'
                    UNION ALL
                            SELECT
                                    vw_JobCosting_WIPMasterSub.SJob
                                  , vw_JobCosting_WIPMasterSub.MJob
                                  , CASE S.LVL
                                            WHEN 0 THEN S.SJob
                                            ELSE S.DisplayMJob
                                            END AS DisplayMJob
                                  , vw_JobCosting_WIPMasterSub.GroupF
                                  , S.LVL + 1
                            FROM cteSample S
                            INNER JOIN vw_JobCosting_WIPMasterSub
                                    ON S.SJob = vw_JobCosting_WIPMasterSub.MJob
                            WHERE vw_JobCosting_WIPMasterSub.GroupF = 'False'
            )
SELECT
    cteSample.SJob
  , cteSample.DisplayMJob MJob
  , cteSample.GroupF
FROM cteSample
ORDER BY
    cteSample.LVL
  , cteSample.MJob
  , cteSample.SJob 

此代码作为查询执行正常。当我作为视图执行时,我收到消息:

“无法解析查询文本”

当我确定代码执行正常时。当我尝试保存时,我收到以下错误,它将无法保存:

“''附近的语法不正确;'”

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您应该可以在视图的定义中使用WITH创建视图。只需删除分隔符;(您无论如何都不需要它,因为WITH是您的第一个也是唯一的声明)和ORDER BY(您应该在使用视图时订购):

CREATE VIEW yourView AS
WITH
    cteSample
    AS (
                    SELECT
                            vw_JobCosting_WIPMasterSub.SJob
                          , vw_JobCosting_WIPMasterSub.MJob
                          , vw_JobCosting_WIPMasterSub.MJob DisplayMJob
                          , vw_JobCosting_WIPMasterSub.GroupF
                          , 0 AS LVL
                    FROM vw_JobCosting_WIPMasterSub
                    WHERE vw_JobCosting_WIPMasterSub.GroupF = 'True'
                    UNION ALL
                            SELECT
                                    vw_JobCosting_WIPMasterSub.SJob
                                  , vw_JobCosting_WIPMasterSub.MJob
                                  , CASE S.LVL
                                            WHEN 0 THEN S.SJob
                                            ELSE S.DisplayMJob
                                            END AS DisplayMJob
                                  , vw_JobCosting_WIPMasterSub.GroupF
                                  , S.LVL + 1
                            FROM cteSample S
                            INNER JOIN vw_JobCosting_WIPMasterSub
                                    ON S.SJob = vw_JobCosting_WIPMasterSub.MJob
                            WHERE vw_JobCosting_WIPMasterSub.GroupF = 'False'
            )
SELECT
    cteSample.SJob
  , cteSample.DisplayMJob MJob
  , cteSample.GroupF
FROM cteSample