视图未创建

时间:2018-04-16 14:23:34

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

当我尝试创建此视图时,您能解释一下我为什么会收到错误消息。

我得到的错误是:

  

Msg 156,Level 15,State 1,Procedure ComplianceView,Line 2
  关键字'声明'。

附近的语法不正确

我使用的脚本是:

CREATE VIEW ComplianceView 
AS
    DECLARE @training AS TABLE (forename NVARCHAR(50), 
                                surname NVARCHAR(50), 
                                module NVARCHAR(100), 
                                date DATETIME)

    INSERT INTO @training (forename, surname, module, date)
        SELECT
            [First Name], surname, module, [completion date]
        FROM
            dbo.[report$]

        UNION ALL

        SELECT
            [First Name], surname, module, [completion date]
        FROM
            [Import Training]

    SELECT DISTINCT 
        l.[employee id],
        l.firstname,
        l.surname,
        l.[job role],
        l.manager,
        m.Mandatory,
        l.location,
        t.module,
        CONVERT(NVARCHAR(35), MAX(t.date), 106) AS LastCompleted,
        CONVERT(NVARCHAR(35), DATEADD(YY, m.[Frequency of training], MAX(t.date)), 106) AS RenewalDate,
        CASE
           WHEN DATEADD(YY, m.[Frequency of training], MAX(t.date)) > GETDATE() 
              THEN 'In Date'
              ELSE 'Out of date' 
        END AS [Compliant]
    FROM
        lookup l 
    JOIN
        @training t ON l.FirstName = t.forename 
                    AND l.Surname = t.surname
    LEFT JOIN
        Mandatory m ON m.[Course Title] = t.module 
                    AND m.[Job Role] = l.[Job Role]
    WHERE
        t.module IN ('Customer Care', 'Equality and Diversity', 'Fire Safety',
                     'Infection Control (Non Clinical)', 'Information Governance', 
                     'Moving and Handling (Non Clinical)', 'Prevent',
                     'Safeguarding Adults (Level 1)', 'Basic Life Support',
                     'Chaperoning', 'Consent', 'On Call Training', 
                     'Safeguarding Children (Level 3)', 'Infection Control (Clinical)',
                     'Moving and Handling (Clinical)')
    GROUP BY
        l.[employee id], l.firstname, l.surname,
        l.[job role], l.manager, 
        t.module,
        l.location,
        m.[Frequency of training], m.mandatory
    ORDER BY
        m.Mandatory DESC

当我在没有视图语法的情况下查看脚本时,脚本可以正常工作。

1 个答案:

答案 0 :(得分:3)

如评论中所述,您只能在视图中查询。您可以使用CTE(公用表表达式)而不是使用表变量来执行此操作。

Create view ComplianceView as
WITH training AS (
    select [First Name], surname, module, [completion date]
    from dbo.[report$]

    union all 

    select [First Name], surname, module, [completion date]
    from [Import Training]
)
select distinct l.[employee id],
l.firstname,
l.surname,
l.[job role],
l.manager,
m.Mandatory,
l.location,
t.module,
Convert(nvarchar(35),MAX(t.date),106) as LastCompleted,
Convert(nvarchar(35),DATEADD(YY,m.[Frequency of training],MAX(t.date)),106) 
as RenewalDate,
Case when DATEADD(YY,m.[Frequency of training],MAX(t.date)) > getdate() then 
'In Date'
else 'Out of date' end as [Compliant]
from lookup l join training t on l.FirstName = t.forename and l.Surname = 
t.surname
left join Mandatory m on m.[Course Title] = t.module and m.[Job Role] = l. 
[Job Role]
where  t.module in ('Customer Care','Equality and Diversity','Fire 
Safety','Infection Control (Non Clinical)','Information Governance','Moving 
and Handling (Non Clinical)',
'Prevent','Safeguarding Adults (Level 1)','Basic Life 
Support','Chaperoning','Consent','On Call Training','Safeguarding Children 
(Level 3)','Infection Control (Clinical)',
'Moving and Handling (Clinical)')
group by l.[employee id],
l.firstname,
l.surname,
l.[job role],
l.manager,
t.module,
l.location,
m.[Frequency of training],
m.mandatory

请注意,您需要在视图中包含ORDER BY,而不是在视图中。