将月份传递给 pl/sql 函数?

发布于 2024-12-15 13:01:29 字数 663 浏览 5 评论 0原文

我想编写一个函数来返回给定月份带来的产品。

我尝试了这个

 create or replace 
 function myfun(mymonth date) 
    return date
 is  
    cursor cur 
    is 
       select * 
         from products 
        where purchase_date=mymonth;
 begin
    for i in cur
    loop
       dbms_output.put_line(i.product_id || ', ' || 
                            i.description || ', ' || 
                            i.product_name || ', ' || 
                            i.quantity || ', ' || 
                            i.price || ', ' || 
                            i.purchase_date);
    end loop;
    return 1;
 end;  

,这给了我错误。 我如何将月份传递给 pl/sql 函数并检索数据。

I want to Write a function that returns the Products brought in a given month.

I tried this

 create or replace 
 function myfun(mymonth date) 
    return date
 is  
    cursor cur 
    is 
       select * 
         from products 
        where purchase_date=mymonth;
 begin
    for i in cur
    loop
       dbms_output.put_line(i.product_id || ', ' || 
                            i.description || ', ' || 
                            i.product_name || ', ' || 
                            i.quantity || ', ' || 
                            i.price || ', ' || 
                            i.purchase_date);
    end loop;
    return 1;
 end;  

this giving me error.
how can i pass month to pl/sql function and retrieve the data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

我偏爱纯白色 2024-12-22 13:01:29

传入日期 ('01-MAY-2003') 并使用日期函数使其工作:

create or replace 
 function myfun(mymonth date) 
    return date
 is  
    cursor cur 
    is 
       select * 
         from products 
        where purchase_date >= TRUNC(mymonth, 'MM')
          and purchase_date < ADD_MONTHS( TRUNC(mymonth,'MM') , 1 );

在输入月份上使用 TRUNC(x, 'MM') 意味着您可以传入 查询中的任何日期都将从该月的第一天开始运行。

或者,您可以使用 LAST_DAY() 函数返回输入月份最后一天的开始时间。您必须注意时间值,因为 Oracle DATE 数据类型可以同时包含日期和时间。

Pass in a date ('01-MAY-2003') and use date functions to make it work:

create or replace 
 function myfun(mymonth date) 
    return date
 is  
    cursor cur 
    is 
       select * 
         from products 
        where purchase_date >= TRUNC(mymonth, 'MM')
          and purchase_date < ADD_MONTHS( TRUNC(mymonth,'MM') , 1 );

Using TRUNC(x, 'MM') on the input month means you can pass in any date at teh query will run from the first day of the month.

Alternatively, you could use the LAST_DAY() function which returns the start of the last day of the input month. You'd have to watch out for time values, since the Oracle DATE datatype can include both date and time.

寄与心 2024-12-22 13:01:29

我认为您需要的只是将购买日期列中的月份与函数参数上的月份进行比较...

我会这样做

cursor cur 
is 
   select * 
     from products 
    where TO_CHAR(purchase_date,'MM') >= TO_CHAR(mymonth, 'MM')

如果您还想包括年份,您可以使用“RRRRMM”作为TO_CHAR 参数,而不是 'MM'

I think that what you need is only to compare the month from the purchase_date column with the month on the param of the function...

I would do it this way

cursor cur 
is 
   select * 
     from products 
    where TO_CHAR(purchase_date,'MM') >= TO_CHAR(mymonth, 'MM')

If you want to include also the year, you could use 'RRRRMM' as a param of TO_CHAR, instead of 'MM'

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