我有一个Oracle PLSQL过程,它有一个父游标和2个子游标(在body中创建),引用了父游标值。
当我运行SQL语句direclt时,它会立即返回结果。但是,当我把它们放在PLSQL程序中时,它无休止地运行。
CREATE OR REPLACE PROCEDURE SENDTASKREMINDER
AS
l_html VARCHAR2(32767);
r_html VARCHAR2(32767);
l_exst NUMBER;
ownerId NUMBER;
CURSOR OwnerCursor IS SELECT DISTINCT TASK_OWNER_ID FROM TASKS
WHERE to_char(TASK_START_DATE, 'DD-MON-YY') >= to_char(SYSDATE, 'DD-MON-YY') AND to_char(TASK_DUE_DATE, 'DD-MON-YY') <= to_char(SYSDATE, 'DD-MON-YY')
UNION
SELECT DISTINCT TASK_OWNER_ID FROM TASKS
WHERE to_char(TASK_START_DATE, 'DD-MON-YY') > to_char(SYSDATE, 'DD-MON-YY');
BEGIN
l_html := '<html>
<head>
<title>Task Reminder</title>
</head>
<body>';
r_html := l_html;
/* Get Pending Tasks */
OPEN OwnerCursor;
LOOP
FETCH OwnerCursor INTO ownerId;
r_html := l_html;
SELECT COUNT(*) INTO l_exst FROM TASKS
WHERE TASK_OWNER_ID = ownerId AND to_char(TASK_START_DATE, 'DD-MON-YY') >= to_char(SYSDATE, 'DD-MON-YY') AND to_char(TASK_DUE_DATE, 'DD-MON-YY') <= to_char(SYSDATE, 'DD-MON-YY');
IF l_exst > 0
THEN
r_html := r_html || '<h1>Your current pending tasks</h1><table><tr><th>Task Name</th><th>Description</th><th>Due Date</th></tr>';
FOR PendingCursor IN (SELECT PK_TASK_ID, TASK_NAME, TASK_DESCRIPTION, TASK_START_DATE, TASK_DUE_DATE FROM TASKS
WHERE TASK_OWNER_ID = ownerId AND to_char(TASK_START_DATE, 'DD-MON-YY') >= to_char(SYSDATE, 'DD-MON-YY') AND to_char(TASK_DUE_DATE, 'DD-MON-YY') <= to_char(SYSDATE, 'DD-MON-YY'))
LOOP
r_html := r_html || '<tr><td>' || PendingCursor.TASK_NAME || '</td><td>' || PendingCursor.TASK_DESCRIPTION || '</td><td>' || PendingCursor.TASK_DUE_DATE ||'</td></tr>';
END LOOP;
r_html := r_html || '</table>';
END IF;
SELECT COUNT(*) INTO l_exst FROM TASKS
WHERE TASK_OWNER_ID = ownerId AND to_char(TASK_START_DATE, 'DD-MON-YY') > to_char(SYSDATE, 'DD-MON-YY') ;
IF l_exst > 0
THEN
r_html := r_html || '<h1>Your future tasks</h1><table><tr><th>Task Name</th><th>Description</th><th>Due Date</th></tr>';
FOR FutureCursor IN (SELECT PK_TASK_ID, TASK_NAME, TASK_DESCRIPTION, TASK_START_DATE, TASK_DUE_DATE FROM TASKS
WHERE TASK_OWNER_ID = ownerId AND to_char(TASK_START_DATE, 'DD-MON-YY') > to_char(SYSDATE, 'DD-MON-YY') )
LOOP
r_html := r_html || '<tr><td>' || FutureCursor.TASK_NAME || '</td><td>' || FutureCursor.TASK_DESCRIPTION || '</td><td>' || FutureCursor.TASK_DUE_DATE ||'</td></tr>';
END LOOP;
r_html := r_html || '</table>';
END IF;
r_html := r_html || '</body></html>';
send_mail(p_to => 'XXX.XXX@xxx.com',
p_from => 'fromemail@xxx.com',
p_subject => 'Your Pending and Future tasks for OwnerId' || ownerId,
p_text_msg => 'text msg is not yet created',
p_html_msg => r_html,
p_smtp_host => 'localhost');
END LOOP;
CLOSE OwnerCursor;
END;
/
这里是否有任何无限循环。 PLSQL中没有错误。我在粘贴之前编辑了这个文件。因此,如果找到编译错误,那么这是由于我的编辑。实际过程没有任何编译错误。
提前感谢您的帮助。
答案 0 :(得分:2)
在获取后忘记了EXIT
:
FETCH Ownercursor into ownerId;
EXIT WHEN Ownercursor%NOTFOUND;
答案 1 :(得分:1)
我没有看到任何无休止的循环。您是否在独立程序中测试了对send_mail的调用?它可能挂在那里。
你的where子句中还有另一个逻辑缺陷。您正在使用TO_CHAR函数错误。这样:
AND to_char(TASK_START_DATE, 'DD-MON-YY') >= to_char(SYSDATE, 'DD-MON-YY') AND to_char(TASK_DUE_DATE, 'DD-MON-YY') <= to_char(SYSDATE, 'DD-MON-YY'))
在逻辑上与此不相同:
TASK_START_DATE >= SYSDATE AND TASK_DUE_DATE <= SYSDATE))
例如,如果TASK_START_DATE为01-APR-15且TASK_DUE_DATE为30-MAY-15且今天为01-JUN-15,那么您的表达式将评估为TRUE,因为“JUN”按字母顺序在“APR”和“之间” MAY”。
答案 2 :(得分:0)
哇。从哪儿开始。 Diederikh和Scott报道了一些明显的问题,但仔细观察代码,有很多事情没有意义。
首先,是一个“当前”任务,具有过去或现在的开始日期以及未来或现在的截止日期?是一个未来开始日期的“未来”任务吗?如果是这样,你的比较都是错误的。根据您的逻辑,“当前”任务是指具有未来或现在的开始日期以及过去或现在的截止日期的任务。
其次,有五个(!)位置有查询要出去并点击数据库。没有理由无法使用初始光标读入您需要的所有内容。然后根据需要进行分析和处理。
还有很多其他地方我会提出建议。我不想列出所有这些,所以我只是包含代码。您可以接受我的小建议,但一定要验证您的日期逻辑,并且不要经常访问数据库。在执行一次磁盘访问所需的时间内,可以执行数千行代码。它仍然没有我想要的生产代码那么精致,但它足以让你开始。而且,虽然我无法访问您的桌子,但它应该可以使用。
create or replace PROCEDURE SENDTASKREMINDER AS
l_html VARCHAR2(100);
r_html VARCHAR2(32767);
C_header varchar2( 40 ) := '<h1>Your current pending tasks</h1>';
F_header varchar2( 32 ) := '<h1>Your future tasks</h1>';
EndHeader varchar2( 80 ) := '<table><tr><th>Task Name</th><th>Description</th><th>Due Date</th></tr>';
EndFooter varchar2( 32 ) := '</table></body></html>';
PrevOwnerId int := -1; -- Must not be a valid OwnerID
C_count int := 0;
F_count int := 0;
l_today date := Trunc( SysDate );
CURSOR OwnerCursor IS
SELECT TASK_OWNER_ID, PK_TASK_ID, TASK_NAME, TASK_DESCRIPTION, TASK_START_DATE, TASK_DUE_DATE
FROM TASKS
WHERE (TASK_START_DATE <= l_today and TASK_DUE_DATE >= l_today)
or TASK_START_DATE > l_today
order by TASK_OWNER_ID, TASK_START_DATE; -- So first the Current then Future tasks
BEGIN
l_html := '<html><head><title>Task Reminder</title></head><body>';
/* Get all Tasks */
for OwnerRec in OwnerCursor LOOP
if OwnerRec.TASK_OWNER_ID != PrevOwnerId then
-- This is the first record for this owner. Unless it's also the first record of the loop,
-- close the html and send the email.
if C_count > 0 or F_count > 0 then
r_html := r_html || EndFooter;
end if;
send_mail(p_to => 'XXX.XXX@xxx.com',
p_from => 'fromemail@xxx.com',
p_subject => 'Your Pending and Future tasks for OwnerId' || ownerId,
p_text_msg => 'text msg is not yet created',
p_html_msg => r_html,
p_smtp_host => 'localhost');
r_html := l_html;
PrevOwnerId := OwnerRec.TASK_OWNER_ID;
C_count := 0;
F_count := 0;
end if;
if OwnerRec.TASK_START_DATE <= l_today then
if C_count = 0 then
-- This is the first Current entry, write the header
r_html := r_html || C_header || EndHeader;
end if;
C_count := c_count + 1;
else
if F_count = 0 then
-- This is the first Future entry. See if there were any Current entries
if C_count > 0 then
-- There was, so terminate the Current table
r_html := r_html || '</table>';
C_count := 0;
end if;
-- Write the header
r_html := r_html || F_header || EndHeader;
end if;
F_count := F_count + 1;
end if;
-- Whether Current or Future, write the task particulars
r_html := r_html || '<tr><td>' || OwnerRec.TASK_NAME || '</td><td>' || OwnerRec.TASK_DESCRIPTION || '</td><td>' || OwnerRec.TASK_DUE_DATE ||'</td></tr>';
END LOOP;
-- The last owner has not completed processing. But maybe there were no tasks at all...
if C_count > 0 or F_count > 0 then
r_html := r_html || EndFooter;
send_mail(p_to => 'XXX.XXX@xxx.com',
p_from => 'fromemail@xxx.com',
p_subject => 'Your Pending and Future tasks for OwnerId' || ownerId,
p_text_msg => 'text msg is not yet created',
p_html_msg => r_html,
p_smtp_host => 'localhost');
dbms_output.put_line( r_html );
end if;
END;