Oracle SQL用户定义的功能

发布于 2025-02-10 19:13:08 字数 939 浏览 1 评论 0原文

我正在尝试编写Oracle SQL功能。应将国家代码,最低年度和最高年份作为输入,并应退还包含该国家 /地区的信息的表。这是我尝试编写的内容,但我是SQL功能的新手。这就是数据的外观,我会为任何帮助而高兴。

create or replace type african_crisis_row as object(
country_abv varchar(4),
year number(5),
banking_crisis varchar(10)
);

create or replace type t_african_crisis_table as table of african_crisis_row;

create or replace function african_crisis (   
    country_abv in varchar,
    year_min in number,
    year_max in number
)
return t_african_crisis_table as v_ret table t_african_crisis_table;

begin
    select 
       african_crisis_row(country_abv, year)
    bulk collect into
       v_ret
    from
       africancrisisdata
    where
        country_abv = country_abv and year between year_min and year_max;
    return v_ret
end african_crisis

I am trying to write Oracle SQL function. The should take country code, min year and max year as inputs and should return table which contains information for that country in the specified years. This is what I tried to write, but I am new to SQL functions. This is how the data looks and I will be glad for any help.
enter image description here

create or replace type african_crisis_row as object(
country_abv varchar(4),
year number(5),
banking_crisis varchar(10)
);

create or replace type t_african_crisis_table as table of african_crisis_row;

create or replace function african_crisis (   
    country_abv in varchar,
    year_min in number,
    year_max in number
)
return t_african_crisis_table as v_ret table t_african_crisis_table;

begin
    select 
       african_crisis_row(country_abv, year)
    bulk collect into
       v_ret
    from
       africancrisisdata
    where
        country_abv = country_abv and year between year_min and year_max;
    return v_ret
end african_crisis

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

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

发布评论

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

评论(1

终难愈 2025-02-17 19:13:08

您需要:

  • v_ret声明之后删除
  • 在调用african_crisis_row对象构造器的呼叫中,包括第三banking_crisis值。
  • 返回和最终结束语句之后,请包括;语句终结者。
  • 不要将其名称与列值相同的函数参数命名。

(Oracle使用varchar2varchar是与varchar2的别名。)

类似的东西:

create or replace function african_crisis (   
  i_country_abv in varchar2,
  i_year_min    in number,
  i_year_max    in number
) return t_african_crisis_table
as
  v_ret t_african_crisis_table;
begin
  select african_crisis_row(country_abv, year, banking_crisis)
  bulk collect into v_ret
  from  africancrisisdata
  where country_abv = i_country_abv
  and   year between i_year_min and i_year_max;

  return v_ret;
end african_crisis;
/

db< > fiddle

You need to:

  • remove table after the v_ret declaration.
  • Include the 3rd banking_crisis value in the call to the african_crisis_row object constructor.
  • Include ; statement terminators after the return and final end statements.
  • Don't name the function parameters with the same name as the column values.

(Oracle uses VARCHAR2 and VARCHAR is an alias to VARCHAR2.)

Something like this:

create or replace function african_crisis (   
  i_country_abv in varchar2,
  i_year_min    in number,
  i_year_max    in number
) return t_african_crisis_table
as
  v_ret t_african_crisis_table;
begin
  select african_crisis_row(country_abv, year, banking_crisis)
  bulk collect into v_ret
  from  africancrisisdata
  where country_abv = i_country_abv
  and   year between i_year_min and i_year_max;

  return v_ret;
end african_crisis;
/

db<>fiddle here

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