在过程中调用函数

时间:2012-12-05 18:26:10

标签: oracle oracle10g

我提前为这个可能基本的问题道歉。我是学生,这是关键时刻!

我正在使用Oracle 10g Express。

我创建了一个函数:

create or replace FUNCTION test_glaccounts_description
(
account_description_param VARCHAR2
) 
RETURN NUMBER
AS
  description_dup_var NUMBER;
BEGIN
  SELECT 1
  INTO description_dup_var
  FROM general_ledger_accounts
  WHERE account_description = account_description_param;

  RETURN description_dup_var;  
  EXCEPTION 
    WHEN NO_DATA_FOUND THEN
    RETURN 0;
END;

并希望在程序中使用该功能。 我试过了:

PROCEDURE insert_gla_with_test
(
  account_number_param  NUMBER,
  account_description_param VARCHAR2
)
 AS
BEGIN 
  IF test_glaccounts_description = 1 THEN
  INSERT INTO  general_ledger_accounts
  VALUES (account_number_param, account_description_param);
  ELSE raise_application_error (-20001, 'Duplicate account description');
  END IF;
END;

但它不喜欢“test_gla_accounts”行.....我做错了什么? 据我了解,该函数返回值1或0,在该过程中,如果函数返回1,我希望将param字段添加到表中。如果函数返回0,我希望程序引发错误。

1 个答案:

答案 0 :(得分:4)

test_glaccounts_description获取参数(account_description_param)并返回NUMBER。因此,为了调用该函数,您需要传入一个参数。假设您要传入传递到account_description_param过程的insert_gla_with_test

CREATE OR REPLACE PROCEDURE insert_gla_with_test
(
  account_number_param  NUMBER,
  account_description_param VARCHAR2
)
 AS
BEGIN 
  IF test_glaccounts_description( account_description_param ) = 1 THEN
    INSERT INTO  general_ledger_accounts
      VALUES (account_number_param, account_description_param);
  ELSE 
    raise_application_error (-20001, 'Duplicate account description');
  END IF;
END;