【发布时间】:2015-12-31 14:47:29
【问题描述】:
我想为我正在使用的日期分配一个值,这样我就不必在每次想要查看不同月份时都通过代码进行更改。当我提交以下代码时,它工作正常:
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]
【问题讨论】:
-
您使用的是 SQL Server 还是 MySQL?您的标签显示 mysql,但语法建议使用 SQL Server。
-
老实说,我不确定,但我希望我知道。在我发帖之前,我打电话给 DB 公司的支持人员,因为总是有人问我这个问题。我与之交谈的人也不认识,今天也没有“程序员”在工作。这是一个 Infinite Campus 数据库,我在 Excel 中使用 ODBC,我还使用 AquaData Studio 或 Microsoft SQL Server 2012。感谢您在下面的回复 - 我想在有机会查看更多内容时尝试一下.
-
如果您在
SELECT * INTO #Attendance之前而不是在括号内使用DECLARE语句,您的第一次尝试可能会奏效。 -
你是对的 EvilBob22。它做了!有很多东西要学,方法也很多。谢谢。
标签: sql-server tsql variables temp