在函数PL / SQL中调用过程

时间:2015-06-06 20:27:41

标签: oracle stored-procedures plsql

我试图找出如何从使用Oracle 11g 11.2.0.2.0的函数调用以下过程。有没有人有想法? :

-- CREATES or REPLACES insert_into_table_a Procedure. 
CREATE OR REPLACE PROCEDURE insert_into_table_a
-- Declares variable to be inserted into tables. 
    (test_insert_a   VARCHAR2)
IS
-- Begins Procedure. 
BEGIN
-- Creates a savepoint. 
SAVEPOINT all_or_none; 

-- Inserts test_insert_a into the CONTACT_ID column of the CONTACT table. 
INSERT INTO CONTACT (CONTACT_ID)
VALUES (test_insert_a);

-- Inserts test_insert_a into the ADDRESS_ID column of the ADDRESS table. 
INSERT INTO ADDRESS (ADDRESS_ID)
VALUES (test_insert_a);

-- Inserts test_insert_a int the TELEPHONE_ID column of the TELEPHONE table. 
INSERT INTO TELEPHONE (TELEPHONE_ID)
VALUES (test_insert_a);

--Commits inserts. 
COMMIT; 
-- Creates exception, incase any errors occur, all changes are rolled back. 
EXCEPTION 
   WHEN OTHERS THEN
    ROLLBACK TO all_or_none; 
-- Ends procedure.
END;
/
-- Shows any errors created. 
SHOW ERRORS

1 个答案:

答案 0 :(得分:2)

您可以像调用任何其他PL / SQL块一样从函数调用该过程

CREATE OR REPLACE FUNCTION my_function
  RETURN integer
IS
  l_parameter VARCHAR2(100) := 'foo';
BEGIN
  insert_into_table_a( l_parameter );
  RETURN 1
END;

话虽如此,从函数调用此过程并没有意义。程序应该操纵数据。函数应返回值。如果调用一个操作函数数据的过程,则不能再在SQL语句中使用该函数,这是首先创建函数的主要原因之一。您还可以使管理安全性变得更加复杂 - 如果您正确使用函数和过程,DBA可以为函数提供只读用户execute权限,而无需担心它们将赋予它们操作数据的能力。

程序本身似乎也很可疑。 address_id表中名为address的列应该是主键,名称表示它是一个数字。这同样适用于contact_id表中的contact列和telephone_id表中的telephone列。您插入字符串而不是数字的事实以及您在三个表中插入相同值的事实意味着这些含义都不是真的。对于将来必须与您的系统合作的人来说,这将会非常混乱。