如何将日期值传递给 plsql 中的游标?
基本上我想将日期值传递给游标,并在每次找到后打印出整个行/记录。我遇到了麻烦,因为 a) 我不知道我的日期在 BEGIN 部分是否正确转换,b) 在打印每一行时,我收到“调用‘PUT_LINE’时参数的数量或类型错误”。
这就是我到目前为止所得到的:
DEFINE B_HIREDATE = 11-OCT-88
DECLARE
cursor DATE_CUR (the_date DATE) is
select * from employees
where hire_date > to_date(the_date, 'dd-mon-yy')
order by hire_date;
r_emp DATE_CUR%ROWTYPE;
BEGIN
for r_emp IN DATE_CUR('&B_HIREDATE') LOOP
dbms_output.put_line(r_emp);
end LOOP;
END;
/
即使我将 select 语句更改为已知的单个字段名称,我也没有得到任何输出值。
Basically I would like to pass a date value to a cursor, and print out the entire row/record after for each found. I am having trouble because a) I don't know if my date is being converted properly in the BEGIN section, and b) I am getting "wrong number or types of arguments in call to 'PUT_LINE'" when printing each row.
This is what I have so far:
DEFINE B_HIREDATE = 11-OCT-88
DECLARE
cursor DATE_CUR (the_date DATE) is
select * from employees
where hire_date > to_date(the_date, 'dd-mon-yy')
order by hire_date;
r_emp DATE_CUR%ROWTYPE;
BEGIN
for r_emp IN DATE_CUR('&B_HIREDATE') LOOP
dbms_output.put_line(r_emp);
end LOOP;
END;
/
I am getting no output values, even if I change my select statement to a known single field name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,您无法通过单个
DBMS_OUTPUT
调用打印出整行;您需要单独打印光标返回的每一列。PUT_LINE
需要 < code>VARCHAR2 argument 或可以隐式转换的东西。您可以将多个值连接到一个调用中。然而,良好的格式化并不容易。日期转换几乎没问题,但您应该在游标调用中包含
TO_DATE
,因为游标参数需要DATE
;并且您应该在日期掩码中使用RR
而不是YY
,或者最好使用 4 位数字的年份和掩码YYYY
。您不需要使用此游标语法将
r_emp
显式声明为变量(但您可以使用OPEN
/FETCH
/关闭
版本)。如果您在 SQL*Plus 中运行此程序,则需要在开头添加
SET SERVEROUTPUT ON
以允许显示DBMS_OUTPUT
调用。您也可以在 SQL Developer 中执行此操作,或者有一个单独的窗格用于查看输出,您需要为工作表启用该窗格。You can't print out the whole row from a single
DBMS_OUTPUT
call, unfortunately; you'll need to print each column returned by the cursor individually.PUT_LINE
expects aVARCHAR2
argument or something that can be implicitly converted. You can concatenate several values into one call. Nice formatting isn't easy though.The date conversion is almost OK, but you should have the
TO_DATE
in the cursor call, as the cursor parameter is expecting aDATE
; and you should useRR
instead ofYY
in your date mask, or preferably use 4-digit years and maskYYYY
.You don't need to explicitly declare the
r_emp
as a variable with this cursor syntax (but you would with theOPEN
/FETCH
/CLOSE
version).If you're running this in SQL*Plus, you need to add
SET SERVEROUTPUT ON
at the start to allow theDBMS_OUTPUT
calls to be displayed. You can do that in SQL Developer too, or there's a separate pane for viewing the output, which you need to enable for the worksheet.