【问题标题】:SQL - How to declare date range in a temp table?SQL - 如何在临时表中声明日期范围?
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 这段代码对我有用,是的,我记得使用变量!起初它给了我与下面的代码相同的错误(列名不明确),但是当我更改为 sDate 和 eDate 时,代码运行良好。感谢您的帮助,对于造成的混乱,我深表歉意。
【解决方案2】:

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

【讨论】:

  • 谢谢,这对我有用。我以为我必须先声明它,我没有想过将它们用作常量。
  • 对不起 - 我忘记将硬编码日期更改为“startDate”和“endDate”。这对我不起作用。我刚刚尝试使用该代码,但它给了我一个错误:> [错误] 脚本行:5-31 ------------------------- 不明确列名“开始日期”。如果我尝试使用其他名称,例如 monthStartDate,它会告诉我这是一个无效的列名。消息:209,级别:16,状态:1,过程:,行:21 [执行时间:2015 年 12 月 31 日上午 10:29:59] [执行时间:62 毫秒]
  • @Karen 你能发布产生这个错误的完整查询吗
  • 是的,这是给出无效列名错误的查询。难道我做错了什么?如果我把它贴在这里 - 它说得太长了。我把代码放在哪里?对不起!
  • @Karen - 添加问题
猜你喜欢
  • 2021-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-06
  • 2018-06-02
  • 2018-03-07
  • 1970-01-01
  • 2011-03-20
相关资源
最近更新 更多