未创建视图
View isn't getting created
你们能解释一下为什么我在尝试创建此视图时收到错误消息吗?
我得到的错误是:
Msg 156, Level 15, State 1, Procedure ComplianceView, Line 2
Incorrect syntax near the keyword 'declare'.
我使用的脚本是:
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
当我在没有视图语法的情况下查看脚本时,该脚本有效。
如评论中所述,您只能在视图中进行查询。您可以使用 CTE(通用 Table 表达式)而不是使用 table 变量来执行此操作。
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
,而不是在视图中。
你们能解释一下为什么我在尝试创建此视图时收到错误消息吗?
我得到的错误是:
Msg 156, Level 15, State 1, Procedure ComplianceView, Line 2
Incorrect syntax near the keyword 'declare'.
我使用的脚本是:
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
当我在没有视图语法的情况下查看脚本时,该脚本有效。
如评论中所述,您只能在视图中进行查询。您可以使用 CTE(通用 Table 表达式)而不是使用 table 变量来执行此操作。
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
,而不是在视图中。