just to play arround - not fully tested, no exception handling etc. maybe more index types to consider but a SP might become handy for this...
REPLACE PROCEDURE test_db_uli.p_create_tb_copy
(IN source_db VARCHAR(30),
IN source_tb VARCHAR(30),
IN target_db VARCHAR(30),
IN target_tb VARCHAR(30),
IN with_data CHAR(1),
IN WITH_STATS CHAR(1),
IN WITH_NPI CHAR(1),
OUT status VARCHAR(2000 )
)
BEGIN
DECLARE CNT SMALLINT;
DECLARE SQL_TEXT VARCHAR(10000);
SELECT COUNT(*) INTO :cnt
FROM dbc.indices
WHERE databasename = :source_db
AND tablename = :source_tb
AND columnposition = 1
AND indextype = 'S'
;
IF ( cnt = 0 )
THEN
BEGIN
SET SQL_TEXT = 'Create table ' !! target_db !! '.' !! target_tb !! ' as ' !! source_db !! '.' !! source_tb !! ' WITH ' !! CASE WHEN with_data = 'N' THEN ' NO ' ELSE ' ' END !! 'DATA ' !! CASE WHEN WITH_STATS = 'Y' THEN 'AND STATISTICS' ELSE '' END !! CASE WHEN WITH_NPI = 'Y' THEN 'NO PRIMARY INDEX' ELSE '' END ;
CALL DBC.SYSEXECSQL( :SQL_TEXT);
SET status = 'NO INDEX' !! SQL_TEXT;
END;
ELSE
BEGIN
SET SQL_TEXT = 'Create table ' !! target_db !! '.' !! target_tb !! ' as ' !! source_db !! '.' !! source_tb !! ' WITH NO DATA ' !! CASE WHEN WITH_STATS = 'Y' THEN 'AND STATISTICS' ELSE '' END !! CASE WHEN WITH_NPI = 'Y' THEN 'NO PRIMARY INDEX' ELSE '' END ;
CALL DBC.SYSEXECSQL( :SQL_TEXT);
SET status = 'NO INDEX' !! SQL_TEXT;
SET SQL_TEXT = 'delete from test_db_uli.gt_drop_index_stmt ';
CALL DBC.SYSEXECSQL( :SQL_TEXT);
SET SQL_TEXT = '
insert into test_db_uli.gt_drop_index_stmt
WITH RECURSIVE base (databasename, tablename, indexnumber, columnname, columnposition)
AS
(
SELECT databasename, tablename, indexnumber, CAST(columnname AS VARCHAR(1000)), columnposition
FROM DBC.Indices
WHERE columnposition = 1
AND INDEXTYPE = ''s''
UNION ALL
SELECT i.databasename,
i.tablename,
i.indexnumber,
b.columnname !! '','' !! TRIM(i.columnname),
i.columnposition
FROM DBC.Indices i
JOIN
base b
ON b.databasename = i.databasename
AND b.tablename = i.tablename
AND b.indexnumber = i.indexnumber
AND b.columnposition + 1 = i.columnposition
)
SELECT ''DROP INDEX ('' !! columnname !! '') ON '!! target_db !! '.' !! target_tb !! ';'' AS txt
FROM base
WHERE databasename = ''' !! source_db !! '''
AND tablename = ''' !! source_tb !! '''
QUALIFY columnposition = MAX(columnposition) OVER (PARTITION BY databasename, tablename, indexnumber)
';
CALL DBC.SYSEXECSQL( :SQL_TEXT);
FOR STCURSOR AS RESULTSET CURSOR FOR
SELECT TXT FROM test_db_uli.gt_drop_index_stmt
DO
BEGIN
SET SQL_TEXT = STCURSOR.txt;
CALL DBC.SYSEXECSQL( :SQL_TEXT);
END;
END FOR;
IF ( with_data = 'Y' )
THEN
BEGIN
SET SQL_TEXT = 'INSERT INTO '!! target_db !! '.' !! target_tb !! ' select * from ' !! source_db !! '.' !! source_tb;
CALL DBC.SYSEXECSQL( :SQL_TEXT);
END;
END IF;
SET status = 'with index ' !! SQL_TEXT;
END;
END IF;
END;
In case secondary indies exists the table will be created with no data option.
then the indices are droped and if with data was specified the table gets populated - durch die brust ins auge
↧