Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Chay Weei Jye

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

DB2 enable/disable constraints

-- All FKs will be enabled/disabled, PKs not affected
-- usage: 
--        enforce -> db2 call SET_CONSTRAINTS('YES')
--        not enforced -> db2 call SET_CONSTRAINTS('NO')

-- To check if referential integrity check is enforced:
-- select * from syscat.tabconst where tabschema='DB2ADMIN' and TYPE='F'

drop procedure SET_CONSTRAINTS!
create procedure SET_CONSTRAINTS (ENFORCE VARCHAR(3))
LANGUAGE SQL
BEGIN
declare v_tabname varchar(128);
declare v_constname varchar(128);
declare v_rows integer;
declare v_alter_table_sql varchar(256);
declare tmp_cursor cursor for SELECT tabname, constname FROM syscat.references WHERE TABSCHEMA = CURRENT SCHEMA ORDER BY CONSTNAME,TABNAME, REFTABNAME;
select count(*) into v_rows from syscat.references WHERE TABSCHEMA = CURRENT SCHEMA;
open tmp_cursor;
while (v_rows > 0) DO
FETCH tmp_cursor INTO v_tabname, v_constname;
SET v_alter_table_sql = 'alter table ' || current SCHEMA || '.' || v_tabname ||  ' alter foreign key ' || v_constname;
IF (ENFORCE = 'NO') THEN 
    SET v_alter_table_sql = v_alter_table_sql || ' NOT ENFORCED';
ELSE
    SET v_alter_table_sql = v_alter_table_sql || ' ENFORCED';
END IF;
execute immediate v_alter_table_sql;
SET v_rows = v_rows - 1;
end while;
close tmp_cursor;
END!
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS