0

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

;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 

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

“无法解析查询文本”

当我确定时,代码执行良好。当我尝试保存时,我收到以下错误并且它不会保存:

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

任何帮助将不胜感激。

4

1 回答 1

1

您应该能够在视图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
于 2013-10-05T20:38:43.027 回答