2016-10-22 33 views
0

我编写了函数,并调用了以下过程。两种编码都可以执行,但是当我执行ProdDetails(10010)时,它显示错误。任何人都可以知道有什么问题吗?通过过程调用函数的错误

 create or replace function ProdCheck(
    f_product_id in number) 
    return varchar IS 
    f_product_name varchar(30); 
    f_product_price number; 
    begin 
    select product_name, price into f_product_name, f_product_price 
    from product 
    where product_id = f_product_id; 
    return f_product_name; 
    end; 

create or replace procedure ProdDetails(
sp_product_id in number 
)IS 
f_product_id number; 
sp_name varchar(30); 
sp_price number; 

begin 
f_product_id := ProdCheck(sp_product_id); 
if f_product_id > 0 then 
dbms_output.put_line('Product Name : '||sp_name); 
dbms_output.put_line('Product Price : '||sp_price); 
else 
dbms_output.put_line('Product not in the database!'); 
end if; 
end; 

enter image description here

+0

也许提及什么错误是? –

+0

http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557 –

回答

1

你的功能prodcheck需要product_id并返回product_name。然后在调用该函数的过程中,您提供函数a product_id(迄今为止的所有内容),但是将该函数的返回值(产品名称)分配给您声明为number的变量f_product_id。显然这是行不通的。事实上,功能和程序在句法上都是正确的;只有将它们放在一起才会失败,并且只有在运行时才会执行,因为Oracle不会严格执行数据类型(如果产品名称为'1000',则函数可能会执行OK并生成垃圾结果,因为它会解释此产品名称改为产品ID)。

您在函数中查询您的表以检查产品ID是否有效,并返回名称。在该过程中,您可以将该函数的返回值分配给sp_name。该函数并不返回价格(它不能 - 一个函数不能返回多个值),所以你不能在过程中显示价格。你可以在过程中再次查询表格,但这似乎很无意义;将所有内容组合成单一程序会更好。 (您可能根本不需要检查 - 如果表中不存在产品ID,您将得到一个“无行”异常,您可以使用它来代替prodcheck。)