sql - 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.
解决方案
该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);
推荐阅读
- huawei-mobile-services - 华为 Push Kit Flutter:Push.getToken() 返回 ResultCode -5
- c# - 如果不存在值,如何在枚举中获取默认值
- regex - Unix:查找连续逗号并将其替换为连续管道
- nattable - NatTable - 使用单元格内的行分隔符复制和粘贴单元格数据
- c# - BLE GATT 在连接到 xamarin 表单中的异常“GattCallback 错误:133”后有时会断开连接
- javascript - Uncaught (in promise) 错误:Axios 中的请求失败,状态码为 404
- python - 尽管 2 年前使用相同的代码,但在训练模型时损失极高且准确率极低
- javascript - discord.js 嵌入:UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body embed.fields[0].value: 长度必须为 1024 或更少
- c# - 发生异常时,Postgres 连接状态保持活动状态
- c++ - 添加到 boost::accumulator 的值的数量是否有限制?