Skip to content

Commit

Permalink
initial push, need to add audit tables
Browse files Browse the repository at this point in the history
  • Loading branch information
zoyafuso-NOAA committed Sep 5, 2024
1 parent 30b7ff7 commit 2e6ff58
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions code/sql_other_views/update_field_names.SQL
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
-- Set up the Procedure to loop through every column in GAP_PRODUCTS and
-- update the column comment with what is in GAP_PRODUCTS.METADATA_COLUMN

CREATE OR REPLACE PROCEDURE update_field_comments AS

PRAGMA AUTONOMOUS_TRANSACTION; -- Declare an autonomous transaction

-- Query all column names from every table in GAP_PRODUCTS EXCEPT VOUCHERS
CURSOR COLUMN_NAMES IS
SELECT OWNER, TABLE_NAME, COLUMN_NAME
FROM all_tab_cols
WHERE OWNER = 'GAP_PRODUCTS'
AND TABLE_NAME NOT IN ('VOUCHERS');

v_field_description VARCHAR2(4000); -- temporary string variable

BEGIN
DBMS_OUTPUT.PUT_LINE('Updating Field Comments');
FOR rec IN COLUMN_NAMES LOOP
BEGIN
-- Attempt to find the field description from GAP_PRODUCTS.METADATA_COLUMN
SELECT METADATA_COLNAME_DESC
INTO v_field_description
FROM GAP_PRODUCTS.METADATA_COLUMN
WHERE METADATA_COLNAME = rec.COLUMN_NAME
AND ROWNUM = 1; -- Ensure we only get one record

-- Update the comment on the column if a description is found
IF v_field_description IS NOT NULL THEN
EXECUTE IMMEDIATE 'COMMENT ON COLUMN ' || rec.OWNER || '.' || rec.TABLE_NAME || '.' || rec.COLUMN_NAME || ' IS ''' || v_field_description || '''';
END IF;

EXCEPTION
WHEN NO_DATA_FOUND THEN
-- No matching metadata column, continue to the next
NULL;
WHEN OTHERS THEN
-- Log unexpected errors and continue
DBMS_OUTPUT.PUT_LINE('An error occurred for column ' || rec.COLUMN_NAME || ': ' || SQLERRM);
END;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Finished Updating Field Comments');
END;

-- Set up the Trigger
CREATE OR REPLACE TRIGGER trg_update_field_comments
AFTER INSERT OR UPDATE OR DELETE ON GAP_PRODUCTS.METADATA_COLUMN
BEGIN
-- Call procedure to update field comments across all GAP_PRODUCTS tables
update_field_comments;
END;

0 comments on commit 2e6ff58

Please sign in to comment.