User defined Oracle types can be a very powerful tool for experienced developers. As with any other object type, it is critical to know if an object (i.e. TYPE here) exists before you create it during execution of the deployment script. The following Oracle data dictionaries have the required information whether you need type or type attribute details:
user_types - lists all user owned custom types user_type_attrs - lists all attributes for the owned custom types user_type_methods - lists all methods found in the owned custom types
Below you can find a few functions that check if a required entity exists or not and return 1 (exists) or 0 (does not exist). Note that these functions can check types in other schemas given that respective privileges have been granted.
CREATE FUNCTION type_exists(p_schema_name VARCHAR2, p_type_name VARCHAR2) RETURN INTEGER AS v_cnt INTEGER; BEGIN SELECT COUNT(*) INTO v_cnt FROM all_types WHERE owner=UPPER(p_schema_name) AND type_name=UPPER(p_type_name); RETURN SIGN(v_cnt); END type_exists;
CREATE FUNCTION type_attribute_exists(p_schema_name VARCHAR2, p_type_name VARCHAR2, p_attr_name VARCHAR2) RETURN INTEGER AS v_cnt INTEGER; BEGIN SELECT COUNT(*) INTO v_cnt FROM all_type_attrs WHERE owner=UPPER(p_schema_name) AND type_name=UPPER(p_type_name) AND attr_name=UPPER(p_attr_name); RETURN SIGN(v_cnt); END type_attribute_exists;
CREATE FUNCTION type_method_exists(p_schema_name VARCHAR2, p_type_name VARCHAR2, p_method_name VARCHAR2) RETURN INTEGER AS v_cnt INTEGER; BEGIN SELECT COUNT(*) INTO v_cnt FROM all_type_methods WHERE owner=UPPER(p_schema_name) AND type_name=UPPER(p_type_name) AND method_name=UPPER(p_method_name); RETURN SIGN(v_cnt); END type_method_exists;
My Oracle Group on Facebook:
If you like this post, you may want to join my new Oracle group on Facebook: https://www.facebook.com/groups/sqlpatterns/
Would you like to read about many more tricks and puzzles?
For more tricks and cool techniques check my book “Oracle SQL Tricks and Workarounds”.