WITH AS(SELECT)语句的CREATE VIEW语法

时间:2018-07-11 20:10:18

标签: sql sql-server view syntax

SQL Server:我有以下有效的SQL语句:

WITH required_skills AS
(
    SELECT DISTINCT 
        e.empl_ID, e.job_code, j.skill_ID, j.skill_desc
    FROM 
        emplskills e
    JOIN 
        jobskills j ON e.job_code = j.job_code
)
SELECT 
    r.empl_ID, e.emplName, r.job_code, r.skill_ID, r.skill_desc
FROM
    required_skills r
FULL OUTER JOIN 
    emplskills e ON r.empl_ID = e.empl_ID
                 AND r.skill_ID = e.skill_ID;

我想将此语句的输出转换为视图。我以为我可以做到这一点:

CREATE VIEW MY_VIEW 
AS
    (WITH required_skills AS
     (
         SELECT DISTINCT 
             e.empl_ID, e.job_code, j.skill_ID, j.skill_desc
         FROM 
             emplskills e
         JOIN 
             jobskills j ON e.job_code = j.job_code
     )
     SELECT 
         r.empl_ID, e.emplName, r.job_code, r.skill_ID, r.skill_desc
     FROM 
         required_skills r
     FULL OUTER JOIN 
         emplskills e ON r.empl_ID = e.empl_ID AND r.skill_ID = e.skill_ID
);

但是它会出错:

  

“ WITH”附近的语法不正确

有人可以帮忙吗?

TIA, 约翰

1 个答案:

答案 0 :(得分:5)

在AS后删除括号。

/scripts/