首页 > 解决方案 > Passing a set of numbers

问题描述

I am trying to compute some mathematical formulas using SQL, but I've stumbled upon the problem of holding k numbers. How could I hold them in a simple way, so I can access them directly, without actually declaring each one of them as individual variables? I've thought about using arrays, but it would be a bit too complicated, is there anything simpler?

The code I've tried with distinct variables to hold the numbers: https://pastebin.com/mPvKCiNP

v_sum := v_first + v_second + v_third;

Instead of this line, have something like a while and iterate through all the values. I don't necessarily need to know the number of values, since I can always pass it through parameters.

标签: sqloracleplsql

解决方案


table类型表示一个无界数组(不要与关系数据表混淆)。

create or replace type number_table_type is table of number;

create or replace procedure mean (number_table_type nt) as
v_sum number := 0;
begin
    for i IN nt.first..nt.last loop
        v_sum := v_sum + i;
    end loop;
    [...]
end mean;

您可以声明表类型的实例:

nt number_table_type := number_table__type(1, 2, 3, 4);

推荐阅读