oracle - 是否可以在过程中将数组作为参数传递?
问题描述
我正在尝试在我的过程中将数组作为参数传递,但我不断收到命令未知错误
代码
SET SERVEROUTPUT ON;
TYPE pourcentage_remise IS TABLE OF NUMBER INDEX BY commandeproduit.ref_produit%type;
CREATE OR REPLACE PROCEDURE remise_produit( pourcent IN pourcentage_remise,
ref_comm IN commande.ref_commande%type,
c_ht OUT commandeproduit.prix_ht%type,
c_ttc OUT commandeproduit.prix_ttc%type)
IS
CURSOR p_curs IS
SELECT ref_produit, prix_ttc, prix_ht FROM commandeproduit WHERE concerne = ref_comm ;
ref commandeproduit.ref_produit%type;
ttc commandeproduit.prix_ttc%type;
ht commandeproduit.prix_ht%type;
BEGIN
open p_curs;
LOOP
FETCH p_curs into ref, ttc, ht;
EXIT WHEN p_curs%notfound;
dbms_output.put_line(ref, ' ',ht, ' ', ttc);
IF pourcent(ref) THEN
ttc := ttc - ttc * pourcent(ref);
ht := ht - ttc * pourcent(ref);
INSERT INTO commandeproduit(prix_ht, prix_ttc) VALUES(ht, ttc) WHERE concerne = ref_comm AND ref_produit = ref;
END IF;
dbms_output.put_line(ref, ' ',ht, ' ', ttc);
END LOOP;
close p_curs;
END remise_produit;
/
过程调用
DECLARE
pourcentage pourcentage_remise;
reference commande.ref_commande%type :=1;
BEGIN
pourcentage('A01') :=0.15;
pourcentage('B15') :=0.2;
remise_produit(pourcentage, reference);
END;
/
桌子
法语错误,表示命令未知
请帮忙
解决方案
您的语法错误出现在您的类型声明中,因此您的其余代码并不是真正需要的。
TYPE pourcentage_remise IS TABLE OF NUMBER INDEX BY commandeproduit.ref_produit%type;
几个问题
- 如果你试图在 SQL 中声明一个类型,你需要使用 a
CREATE TYPE
所以你错过了CREATE
. - 如果您尝试在 SQL 中声明表类型,则不能使用关联数组。您实际上需要一个嵌套表。
- 如果您尝试声明 PL/SQL 类型,则您的语句需要位于 PL/SQL 块中。您可以声明一个包含关联数组类型的包。
如果要在 SQL 中声明嵌套表类型
CREATE TYPE pourcentage_remise IS TABLE OF NUMBER;
如果要在 PL/SQL 包中声明关联数组
CREATE OR REPLACE PACKAGE my_collection_pkg
AS
TYPE pourcentage_remise IS TABLE OF NUMBER INDEX BY commandeproduit.ref_produit%type;
END;
如果您想使用嵌套表类型,这会改变您需要初始化关联数组的方式。它应该改变你引用该数组元素的方式,但我对你的代码感到困惑。您的过程似乎正在使用数字索引来访问关联数组的元素,如果关联数组使用字符串作为索引,这将没有意义。
推荐阅读
- c++ - 使用两个 while 循环的简单 C++ 代码说明
- r - 如何调整和删除交叉表图中的图例标签?
- typescript - VSCode typescript intellisense 不建议嵌套命名空间中的接口
- html - 波兰语字符有不同的权重
- php - 巧妙地解析 PHP 日期时间
- javascript - 用相似的子对象解构 JS 对象
- loops - 在 Ada 中的第一个索引之后开始的数组循环?
- php - 数组值自动增加一个设定值(例如,如果第一个是 echo $myarray[5];下一个是 echo $myarray[5+5];所以真的是 $myarray[10];)
- sql - 无法在 phpmyadmin 中显示空值,对我做错了什么有任何解释吗?
- python - Flask/Dash CSV/Excel 文件下载/输出对于不同实例中的每个用户不是唯一的