SQL - 如何在临时表中声明日期范围?

时间:2015-12-31 14:47:30

标签: sql-server tsql variables temp

我想为我使用的日期指定一个值,因此每次我想查看其他月份时,我都不必通过代码进行更改。当我提交以下代码时,它可以正常工作:

Declare @startDate datetime = '9/1/2015'
Declare @endDate datetime = '9/30/2015'
SELECT a.attendanceID
, p.stateID
, p.personID
, i.lastName
, i.firstName
, en.grade
, a.date
, ae.status
, ae.excuse
, ae.description
, prd.name
FROM enrollment en
LEFT JOIN enrollmentME em on en.enrollmentID = em.enrollmentID
LEFT JOIN person p on p.personID = en.personID
LEFT JOIN [Identity] i on p.currentIdentityID = i.identityID and p.personID = i.personID
LEFT JOIN Calendar c on en.calendarID = c.calendarID
LEFT JOIN SchoolYear sy ON sy.endYear = c.endYear AND sy.active = 1
LEFT JOIN School sc ON sc.schoolID = c.schoolID
LEFT JOIN Attendance a on a.personID = p.personID and a.date between @startDate and @endDate
LEFT JOIN AttendanceExcuse ae on a.excuseID = ae.excuseID AND a.calendarID = c.calendarID
LEFT JOIN period prd on prd.periodID = a.periodID
WHERE (en.endDate IS NULL or en.endDate > @endDate)
    AND a.date is not null
    AND ae.excuse is not null
    AND sc.name = 'Elm Street School'

但是 - 当我尝试用它制作临时表时,我得到错误:

SELECT *
INTO #Attendance
FROM
(Declare @startDate datetime = '9/1/2015'
Declare @endDate datetime = '9/30/2015'
SELECT a.attendanceID
, p.stateID
, p.personID
, i.lastName
, i.firstName
, en.grade
, a.date
, ae.status
, ae.excuse
, ae.description
, prd.name
FROM enrollment en
LEFT JOIN enrollmentME em on en.enrollmentID = em.enrollmentID
LEFT JOIN person p on p.personID = en.personID
LEFT JOIN [Identity] i on p.currentIdentityID = i.identityID and p.personID = i.personID
LEFT JOIN Calendar c on en.calendarID = c.calendarID
LEFT JOIN SchoolYear sy ON sy.endYear = c.endYear AND sy.active = 1
LEFT JOIN School sc ON sc.schoolID = c.schoolID
LEFT JOIN Attendance a on a.personID = p.personID and a.date between @startDate and @endDate
LEFT JOIN AttendanceExcuse ae on a.excuseID = ae.excuseID AND a.calendarID = c.calendarID
LEFT JOIN period prd on prd.periodID = a.periodID
WHERE (en.endDate IS NULL or en.endDate > @endDate)
    AND a.date is not null
    AND ae.excuse is not null
    AND sc.name = 'Elm Street School') x

是否有正确的'实现这一目标的方法?这是我得到的错误结果:

>[Error] Script lines: 9-39 -------------------------
 Incorrect syntax near the keyword 'Declare'.
 Msg: 156, Level: 15, State: 1, Procedure: , Line: 4 

>[Error] Script lines: 9-39 -------------------------
 Incorrect syntax near ')'.
 Msg: 102, Level: 15, State: 1, Procedure: , Line: 30 

 [Executed: 12/31/2015 9:45:40 AM] [Execution: 47ms] 

谢谢!

这是给我一个无效列名错误的代码:

SELECT *
INTO #Attendance
FROM
(SELECT cast('9/1/2015' as date) as sDate
,cast('9/30/2015' as date) as eDate 
,a.attendanceID
, p.stateID
, p.personID
, i.lastName
, i.firstName
, en.grade
, a.date
, ae.status
, ae.excuse
, ae.description
, prd.name
FROM enrollment en
LEFT JOIN enrollmentME em on en.enrollmentID = em.enrollmentID
LEFT JOIN person p on p.personID = en.personID
LEFT JOIN [Identity] i on p.currentIdentityID = i.identityID and p.personID = i.personID
LEFT JOIN Calendar c on en.calendarID = c.calendarID
LEFT JOIN SchoolYear sy ON sy.endYear = c.endYear AND sy.active = 1
LEFT JOIN School sc ON sc.schoolID = c.schoolID
LEFT JOIN Attendance a on a.personID = p.personID and a.date between sDate and eDate
LEFT JOIN AttendanceExcuse ae on a.excuseID = ae.excuseID AND a.calendarID = c.calendarID
LEFT JOIN period prd on prd.periodID = a.periodID
WHERE (en.endDate IS NULL or en.endDate > eDate)
    AND a.date is not null
    AND ae.excuse is not null
    AND sc.name = 'Elm Street School') x

错误:

>[Error] Script lines: 2-31 -------------------------
 Invalid column name 'sDate'.
 Msg: 207, Level: 16, State: 1, Procedure: , Line: 24 

>[Error] Script lines: 2-31 -------------------------
 Invalid column name 'eDate'.
 Msg: 207, Level: 16, State: 1, Procedure: , Line: 24 

>[Error] Script lines: 2-31 -------------------------
 Invalid column name 'eDate'.
 Msg: 207, Level: 16, State: 1, Procedure: , Line: 27 

 [Executed: 12/31/2015 10:58:07 AM] [Execution: 62ms] 

2 个答案:

答案 0 :(得分:0)

我建议将值放在子查询中,然后使用这些值。在MySQL中:

CREATE TEMPORARY TABLE Atendance as
    SELECT . . .
    FROM (SELECT date('2015-09-01') as startDate, date('2015-09-30') as endDate 
         ) params CROSS JOIN
         enrollment en
    LEFT JOIN enrollmentME em on en.enrollmentID = em.enrollmentID
    LEFT JOIN person p on p.personID = en.personID
    LEFT JOIN [Identity] i on p.currentIdentityID = i.identityID and p.personID = i.personID
    LEFT JOIN Calendar c on en.calendarID = c.calendarID
    LEFT JOIN SchoolYear sy ON sy.endYear = c.endYear AND sy.active = 1
    LEFT JOIN School sc ON sc.schoolID = c.schoolID
    LEFT JOIN Attendance a on a.personID = p.personID and a.date between params.startDate and params.endDate
    LEFT JOIN AttendanceExcuse ae on a.excuseID = ae.excuseID AND a.calendarID = c.calendarID
    LEFT JOIN period prd on prd.periodID = a.periodID
    WHERE (en.endDate IS NULL or en.endDate > params.endDate)

答案 1 :(得分:0)

而不是使用declare将其用作常量值

SELECT *
INTO #Attendance
FROM
(
SELECT 
cast('9/1/2015' as date) as startDate
,cast('9/30/2015' as date) as  endDate 
,a.attendanceID
, p.stateID
, p.personID
, i.lastName
, i.firstName
...