将月份传递给 pl/sql 函数?
我想编写一个函数来返回给定月份带来的产品。
我尝试了这个
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
传入日期 ('01-MAY-2003') 并使用日期函数使其工作:
在输入月份上使用
TRUNC(x, 'MM')
意味着您可以传入 查询中的任何日期都将从该月的第一天开始运行。或者,您可以使用
LAST_DAY()
函数返回输入月份最后一天的开始时间。您必须注意时间值,因为 Oracle DATE 数据类型可以同时包含日期和时间。Pass in a date ('01-MAY-2003') and use date functions to make it work:
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.我认为您需要的只是将购买日期列中的月份与函数参数上的月份进行比较...
我会这样做
如果您还想包括年份,您可以使用“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
If you want to include also the year, you could use 'RRRRMM' as a param of TO_CHAR, instead of 'MM'