如何将日期值传递给 plsql 中的游标?

发布于 2024-11-02 21:06:35 字数 539 浏览 0 评论 0原文

基本上我想将日期值传递给游标,并在每次找到后打印出整个行/记录。我遇到了麻烦,因为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落成白 2024-11-09 21:06:35

不幸的是,您无法通过单个 DBMS_OUTPUT 调用打印出整行;您需要单独打印光标返回的每一列。 PUT_LINE 需要 < code>VARCHAR2 argument 或可以隐式转换的东西。您可以将多个值连接到一个调用中。然而,良好的格式化并不容易。

日期转换几乎没问题,但您应该在游标调用中包含 TO_DATE,因为游标参数需要 DATE;并且您应该在日期掩码中使用 RR 而不是 YY,或者最好使用 4 位数字的年份和掩码 YYYY

SET SERVEROUTPUT ON
DEFINE B_HIREDATE = 11-OCT-1988

DECLARE
  cursor DATE_CUR (the_date DATE) is
    select * from employees
      where hire_date > the_date
      order by hire_date;    
BEGIN
  for r_emp IN DATE_CUR(TO_DATE('&B_HIREDATE','DD-MON-YYYY')) LOOP
     dbms_output.put_line(r_emp.hire_date);
  end LOOP;
END;

您不需要使用此游标语法将 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 a VARCHAR2 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 a DATE; and you should use RR instead of YY in your date mask, or preferably use 4-digit years and mask YYYY.

SET SERVEROUTPUT ON
DEFINE B_HIREDATE = 11-OCT-1988

DECLARE
  cursor DATE_CUR (the_date DATE) is
    select * from employees
      where hire_date > the_date
      order by hire_date;    
BEGIN
  for r_emp IN DATE_CUR(TO_DATE('&B_HIREDATE','DD-MON-YYYY')) LOOP
     dbms_output.put_line(r_emp.hire_date);
  end LOOP;
END;

You don't need to explicitly declare the r_emp as a variable with this cursor syntax (but you would with the OPEN/FETCH/CLOSE version).

If you're running this in SQL*Plus, you need to add SET SERVEROUTPUT ON at the start to allow the DBMS_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文