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

SQL - How to declare date range in a temp table?

我想为我正在使用的日期分配一个值,这样我就不必每次查看不同的月份时都通过代码进行更改。当我提交以下代码时,它工作正常:

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'

但是 - 当我尝试用它制作临时 table 时,我收到错误消息:

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

有没有 'correct' 方法来实现这一点?这是我得到的错误结果:

>[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] 

我建议将这些值放在子查询中,然后使用这些值。在 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)

而不是使用 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
...