CREATE OR REPLACE PROCEDURE DROP_OBJECT_IF_EXISTS(
CHARACTER VARYING(ANY),
CHARACTER VARYING(ANY)
)
RETURNS BOOLEAN
LANGUAGE NZPLSQL
execute as caller
AS
BEGIN_PROC
DECLARE
v_count INTEGER;
v_sql VARCHAR(999 );
BEGIN
/*
* Check if the object exists
*/
IF ($ 2 = 'TABLE') THEN SELECT count (* ) INTO v_count FROM _v_table WHERE UPPER (TABLENAME) = UPPER($1);
ELSIF ($2 = 'VIEW') THEN SELECT count (* ) INTO v_count FROM _v_view WHERE UPPER (VIEWNAME) = UPPER($1);
ELSIF ($2 = 'SEQUENCE') THEN SELECT count (* ) INTO v_count FROM _v_sequence WHERE UPPER (SEQNAME) = UPPER($1);
END IF;
/*
* Return if the object does not exist
*/
IF v_count = 0 THEN
RETURN FALSE ;
END IF;
/*
* If indeed the object exists, then drop it.
*/
IF ($ 2 = 'TABLE') THEN v_sql:= 'DROP TABLE ' || $ 1;
ELSIF ($2 = 'VIEW') THEN v_sql:= 'DROP VIEW ' || $1 ;
ELSIF ($2 = 'SEQUENCE') THEN v_sql:= 'DROP SEQUENCE ' || $1 ;
END IF;
EXECUTE IMMEDIATE v_sql;
/*
* End of procedure
*/
RETURN TRUE;
/*
* Handle exceptions and exit gracefully
*/
EXCEPTION
WHEN OTHERS THEN
RAISE NOTICE 'Dropping object failed. Error msg: %' , SQLERRM ;
RETURN FALSE ;
END;
END_PROC;
No comments:
Post a Comment