An oracle stored procedure to truncate table with constraints

create or replace PROCEDURE sp_truncate_table (target_table_name VARCHAR2)
IS
sql_string VARCHAR2(5000);
CURSOR str_cursor IS
select
‘alter table ‘ || child_tname || ‘ drop constraint ‘ || child_cons_name || ‘;’ as sql_str
from
( select a.table_name child_tname, a.constraint_name child_cons_name,
b.r_constraint_name parent_cons_name,
max(decode(position, 1, ‘”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 2,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 3,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 4,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 5,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 6,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 7,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 8,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 9,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,10,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,11,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,12,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,13,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,14,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,15,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,16,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL))
child_columns
from user_cons_columns a, user_constraints b
where a.constraint_name = b.constraint_name
and b.constraint_type = ‘R’
group by a.table_name, a.constraint_name, b.r_constraint_name
) child,
( select a.constraint_name parent_cons_name, a.table_name parent_tname,
max(decode(position, 1, ‘”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 2,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 3,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 4,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 5,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 6,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 7,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 8,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 9,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,10,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,11,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,12,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,13,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,14,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,15,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,16,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL))
parent_columns
from user_cons_columns a, user_constraints b
where a.constraint_name = b.constraint_name
and b.constraint_type in ( ‘P’, ‘U’ )
group by a.table_name, a.constraint_name ) parent
where child.parent_cons_name = parent.parent_cons_name
and parent.parent_tname = upper(target_table_name)

union all

select ‘truncate table ‘ || target_table_name || ‘;’ as sql_str from dual

union all

select
‘alter table ‘ || child_tname || ‘ add constraint ‘ || child_cons_name ||
‘ foreign key ( ‘ || child_columns || ‘ ) ‘ || ‘ references ‘ || parent_tname || ‘ ( ‘ || parent_columns || ‘ );’ as sql_str
from
( select a.table_name child_tname, a.constraint_name child_cons_name,
b.r_constraint_name parent_cons_name,
max(decode(position, 1, ‘”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 2,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 3,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 4,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 5,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 6,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 7,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 8,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 9,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,10,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,11,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,12,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,13,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,14,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,15,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,16,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL))
child_columns
from user_cons_columns a, user_constraints b
where a.constraint_name = b.constraint_name
and b.constraint_type = ‘R’
group by a.table_name, a.constraint_name, b.r_constraint_name
) child,
( select a.constraint_name parent_cons_name, a.table_name parent_tname,
max(decode(position, 1, ‘”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 2,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 3,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 4,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 5,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 6,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 7,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 8,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 9,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,10,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,11,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,12,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,13,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,14,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,15,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,16,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL))
parent_columns
from user_cons_columns a, user_constraints b
where a.constraint_name = b.constraint_name
and b.constraint_type in ( ‘P’, ‘U’ )
group by a.table_name, a.constraint_name ) parent
where child.parent_cons_name = parent.parent_cons_name
and parent.parent_tname = upper(target_table_name)
;
BEGIN

FOR str IN str_cursor LOOP
dbms_output.put_line(str.sql_str);
–EXECUTE IMMEDIATE str.sql_str;
END LOOP;

END;

sql to truncate table with constraints

select
‘alter table ‘ || child_tname || ” || chr(10) ||
‘ drop constraint ‘ || child_cons_name || ‘;’
from
( select a.table_name child_tname, a.constraint_name child_cons_name,
b.r_constraint_name parent_cons_name,
max(decode(position, 1, ‘”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 2,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 3,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 4,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 5,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 6,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 7,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 8,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 9,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,10,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,11,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,12,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,13,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,14,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,15,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,16,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL))
child_columns
from user_cons_columns a, user_constraints b
where a.constraint_name = b.constraint_name
and b.constraint_type = ‘R’
group by a.table_name, a.constraint_name, b.r_constraint_name
) child,
( select a.constraint_name parent_cons_name, a.table_name parent_tname,
max(decode(position, 1, ‘”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 2,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 3,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 4,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 5,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 6,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 7,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 8,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 9,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,10,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,11,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,12,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,13,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,14,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,15,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,16,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL))
parent_columns
from user_cons_columns a, user_constraints b
where a.constraint_name = b.constraint_name
and b.constraint_type in ( ‘P’, ‘U’ )
group by a.table_name, a.constraint_name ) parent
where child.parent_cons_name = parent.parent_cons_name
and parent.parent_tname = upper(‘trades_master’)

union all

select
‘alter table ‘ || child_tname || ” || chr(10) ||
‘ add constraint ‘ || child_cons_name || ” || chr(10) ||
‘ foreign key ( ‘ || child_columns || ‘ ) ‘ || chr(10) ||
‘ references ‘ || parent_tname || ‘ ( ‘ || parent_columns || ‘ )
;’ fkey
from
( select a.table_name child_tname, a.constraint_name child_cons_name,
b.r_constraint_name parent_cons_name,
max(decode(position, 1, ‘”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 2,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 3,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 4,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 5,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 6,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 7,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 8,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 9,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,10,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,11,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,12,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,13,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,14,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,15,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,16,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL))
child_columns
from user_cons_columns a, user_constraints b
where a.constraint_name = b.constraint_name
and b.constraint_type = ‘R’
group by a.table_name, a.constraint_name, b.r_constraint_name
) child,
( select a.constraint_name parent_cons_name, a.table_name parent_tname,
max(decode(position, 1, ‘”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 2,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 3,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 4,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 5,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 6,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 7,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 8,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position, 9,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,10,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,11,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,12,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,13,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,14,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,15,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL)) ||
max(decode(position,16,’, ‘||'”‘||
substr(column_name,1,30)||'”‘,NULL))
parent_columns
from user_cons_columns a, user_constraints b
where a.constraint_name = b.constraint_name
and b.constraint_type in ( ‘P’, ‘U’ )
group by a.table_name, a.constraint_name ) parent
where child.parent_cons_name = parent.parent_cons_name
and parent.parent_tname = upper(‘trades_master’)
;

Example of drop table if exists in SQL Server

IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = ‘Lesson36ProductCategorySource’ AND TABLE_SCHEMA = ‘Production’)
DROP TABLE [Production].[Lesson36ProductCategorySource];
GO

Create JMS resources in Tibco Studio

1. Connection Factory needs to be created in Tibco EMS. No jndi name is needed for connection factory. Just make sure the factory jndi name in BPM is the same as the connection factory name created in Tibco EMS.

2. Queue destination needs to be created in Tibco EMS. A jndi name must also be created in EMS admin. Make sure the queue jndi name is the same as the jndi name specified in BPM.

2. request and response can share the same connecition factory