我正在使用SQLPLUS并且确实需要能够使用IF/THEN/ELSE
,但我无法找到有关如何使其工作的任何信息。 (消息人士都说不能这样做),所以我试图用CASE
来做同样的事情。我需要查看sysdate,如果它在日期A和日期B之间,则运行Select语句。如果它没有转到下一个CASE
语句。以下是我正在尝试做的事情:
CASE
WHEN sysdate BETWEEN TO_DATE('28/08/2016', 'DD/MM/YYYY') AND TO_DATE('20/11/2016', 'DD/MM/YYYY')
THEN
(SELECT
schools.abbreviation,
students.entrydate,
students.lastfirst as Student,
students.student_number,
students.grade_level,
courses.course_name,
teachers.lastfirst as Teacher,
pgfinalgrades.grade,
round(pgfinalgrades.percent) as Percent,
(CASE
WHEN ps_customfields.getcf('Students', students.ID, 'zz_B_Soccer') = '1'
THEN 'Boys_Soccer'
ELSE NULL
END) AS "Athletic Sport - Fall"
FROM pgfinalgrades
JOIN sections on pgfinalgrades.sectionid = sections.id
JOIN courses on upper(sections.course_number) = upper(courses.course_number)
JOIN cc on pgfinalgrades.studentid = cc.studentid and sections.id = cc.sectionid and sysdate between cc.dateenrolled and cc.dateleft
JOIN students on pgfinalgrades.studentid = students.id
JOIN prefs on prefs.name = 'curfgname-S' || sections.schoolid and pgfinalgrades.finalgradename = to_char(substr(prefs.value, 1,2))
JOIN terms on terms.schoolid = sections.schoolid and sysdate between terms.firstday and terms.lastday and terms.isyearrec = 1
JOIN schools on sections.schoolid = schools.school_number
JOIN teachers on sections.teacher = teachers.id
WHERE students.enroll_status = 0
AND sysdate between cc.dateenrolled and cc.dateleft
AND (pgfinalgrades.grade = 'C-'
OR pgfinalgrades.grade = 'D'
OR pgfinalgrades.grade = 'D-'
OR pgfinalgrades.grade = 'E'
OR pgfinalgrades.grade = 'F'
)
AND --Fall Sports
( ps_customfields.getcf('Students', students.ID, 'zz_B_Soccer') IS NOT NULL
OR ps_customfields.getcf('Students', students.ID, 'zz_B_Cross_Country') IS NOT NULL
OR ps_customfields.getcf('Students', students.ID, 'zz_Golf') IS NOT NULL
));
接下来是冬季运动,然后是春季运动。 我在sysdate行上收到错误“unknown command”,然后运行每个case。我不知道如何让它抓住今天的日期,确定它属于哪种情况,然后运行那个让我输出。我打算将其自动化,在整个学年中每周运行一次,并且不希望每年多次触摸它。 如果有人能想到更好的方法来做到这一点,我全都听见了。 :)
答案 0 :(得分:0)
我正在使用SQLPLUS,确实需要能够使用 IF / THEN / ELSE,但我无法找到有关如何的任何信息 我可以让它工作。 (
Sources all say it can't be done
)
我想说这在PLSQL匿名块中可以很好地完成,我们可以使用IF/ELSE
语句。
请参阅breif演示:
begin
if sysdate between to_date ('28/08/2016', 'DD/MM/YYYY')
and to_date ('20/11/2016', 'DD/MM/YYYY')
then
dbms_output.put_line ('Sysdate is beteeween the date range');
---You can run your sql query here
else
dbms_output.put_line ('Not in date range');
---You can run your sql query here
end if;
end;
输出
Not in date range
PL/SQL procedure successfully completed.