Using Common Table Expressions in Oracle to update tables

From ExxtremeWiki
Jump to: navigation, search

Example: Using a CTE to insert records into the MaxGroups table filling values on the fly

/*
** This repair example adds missing groups back into the maxgroups table defaulting several parameters
*/
 
INSERT INTO maxgroups (GRPNAME,CANGROUPCHANGEPW,USECOMMODITYAUTH,COMMAUTHINCLUDE)
SELECT * FROM (
 WITH cte AS (SELECT grpname, 'Y' AS CANGROUPCHANGEPW, 'N' AS USECOMMODITYAUTH, 'Y' AS COMMAUTHINCLUDE
		FROM maxusergroups
		WHERE grpname NOT IN (SELECT grpname FROM maxgroups)
		GROUP BY grpname ORDER BY grpname
        )
     SELECT GRPNAME,CANGROUPCHANGEPW,USECOMMODITYAUTH,COMMAUTHINCLUDE
     FROM cte
     )
;
commit
;


Example: Using a CTE to insert records into the MaxUserGroups table filling values on the fly

/*
** This repair example is adding back in missing group entries.  Also note that in Oracle the CTE positioning is different than SQLServer so be careful.
*/
 
INSERT INTO maxusergroups (usrname, grpname, inherited, STATUS, wfactive, siteid, startcenterid, showwfinbox) 
SELECT * FROM ( 
 WITH cte AS (SELECT grpname AS usrname, grpname, 'N' AS inherited, 'ACTIVE' AS STATUS, 'N' AS wfactive, 'BARCELON' AS siteid, '00' AS startcenterid, 'N' AS showwfinbox 
               FROM maxgroups WHERE NOT EXISTS (SELECT NULL FROM maxusergroups WHERE usrname = maxgroups.grpname) 
          ) 
     SELECT usrname, grpname, inherited, STATUS, wfactive, siteid, startcenterid, showwfinbox 
     FROM cte 
     ) 
; 
commit 
;


Example: Correcting duplicate EQTransID's in the EQTrans table

/*
** EQtrans Key Index is defined incorrectly
*/
 
SELECT * FROM maxsysindexes WHERE name LIKE 'EQTRANS%'
;
SELECT * FROM maxsyskeys WHERE ixname = 'EQTRANS_NDX'
;
UPDATE maxsysindexes SET uniquerule = 'U' WHERE name = 'EQTRANS_NDX' AND uniquerule = 'D'
;
DROP INDEX EQTRANS_NDX
;
SELECT eqtransid FROM eqtrans GROUP BY eqtransid HAVING COUNT(*) > 1
;
CREATE SEQUENCE eqtransseq1
;
SELECT eqtransseq1.NEXTVAL FROM dual
;
 
UPDATE eqtrans SET eqtransid = eqtransseq1.NEXTVAL WHERE eqtransid IN (
	WITH eqtransfix AS (SELECT eqtransid FROM eqtrans GROUP BY eqtransid HAVING COUNT(*) > 1)
	SELECT * FROM eqtransfix
	)
;
 
DROP SEQUENCE eqtransseq1
;
CREATE UNIQUE INDEX EQTRANS_NDX ON eqtrans (eqtransid)
;
commit
;


Example: Merge / Fix PrimaryKeyColumnSequences from the MaxSysIndex and MaxSysKeys tables

/*
** Merge / Fix PrimaryKeyColumnSequences from the MaxSysIndex and MaxSysKeys tables
*/
MERGE INTO maxsyscolscfg d
   USING
   (
	SELECT indexname, m.tbname, m.name, colname, m.primarykeycolseq, colseq FROM (
	WITH indexes AS
		(SELECT name AS indexname, tbname, colname, colseq
			 FROM maxsyskeys k
			INNER JOIN maxsysindexes i ON i.name = k.ixname AND uniquerule = 'U'
			AND name LIKE '%NDX1'
		)
		SELECT * FROM indexes
		) i
	JOIN maxsyscolscfg m ON m.tbname = i.tbname AND m.name = i.colname
	WHERE (primarykeycolseq != colseq OR (primarykeycolseq IS NULL AND colseq IS NOT NULL))
	) s
   ON (d.tbname = s.tbname AND d.name = s.name)
   WHEN MATCHED THEN UPDATE SET d.primarykeycolseq = s.colseq
   WHEN NOT MATCHED THEN INSERT (d.tbname) VALUES (s.tbname) --where (d.primarykeycolseq != s.primarykeycolseq)
;
 
MERGE INTO maxsyscolumns d
   USING
   (
	SELECT indexname, m.tbname, m.name, colname, m.primarykeycolseq, colseq FROM (
	WITH indexes AS
		(SELECT name AS indexname, tbname, colname, colseq
			 FROM maxsyskeys k
			INNER JOIN maxsysindexes i ON i.name = k.ixname AND uniquerule = 'U'
			AND name LIKE '%NDX1'
		)
		SELECT * FROM indexes
		) i
	JOIN maxsyscolumns m ON m.tbname = i.tbname AND m.name = i.colname
	WHERE (primarykeycolseq != colseq OR (primarykeycolseq IS NULL AND colseq IS NOT NULL))
	) s
   ON (d.tbname = s.tbname AND d.name = s.name)
   WHEN MATCHED THEN UPDATE SET d.primarykeycolseq = s.colseq
   WHEN NOT MATCHED THEN INSERT (d.tbname) VALUES (s.tbname) --where (d.primarykeycolseq != s.primarykeycolseq)
;
 
commit
;

Further reading

References

<references></references>