Oracle PL / SQL – ALTER関数の例
ALTER FUNCTION
ステートメントは、スタンドアロン関数を明示的に再コンパイルします。 関数で使用されているテーブルのALTER TABLE
により、関数が無効になる場合があります。再度有効にするには、再コンパイル(関数の変更)する必要があります。
1. ALTER関数の例
まず、テーブルtest_alter
、関数get_max_amount
を作成します。 この関数では、test_alter
の「amount」列を使用しています。 ここで、test_alter
テーブルから「amount」列を削除すると、関数のステータスは自動的に無効になります。
「amount」列をtest_alter
テーブルに追加し直しても、関数のステータスは無効のままです。 関数を再度有効にするには、ALTER FUNCTION
ステートメントを使用して関数を再コンパイルする必要があります。
1.1 Create table and function.
--creating table test_alter CREATE TABLE test_alter ( ID number(5), AMOUNT number(5) );
CREATE OR REPLACE FUNCTION get_max_amount RETURN NUMBER IS maxAmount NUMBER(5); BEGIN select MAX(amount) into maxAmount from test_alter; RETURN maxAmount; END get_max_amount; /
1.2 Check the status of function.
select object_name, status from user_objects where object_name = 'GET_MAX_AMOUNT'; -- output : VALID
1.3 Drop column and add it back to table, check the function status.
-- drop column ALTER TABLE test_alter DROP column amount; select object_name, status from user_objects where object_name = 'GET_MAX_AMOUNT'; -- output : INVALID -- add column ALTER TABLE test_alter ADD amount number(5); select object_name, status from user_objects where object_name = 'GET_MAX_AMOUNT'; -- output : INVALID
1.4 Recompile the function with ALTER FUNCTION
ALTER FUNCTION GET_MAX_AMOUNT COMPILE; -- Output : function GET_MAX_AMOUNT altered. -- Check the function status again! select object_name, status from user_objects where object_name = 'GET_MAX_AMOUNT'; -- output : VALID