Thursday, April 20, 2017

Creating Unique Index By Using Table Name and Column Serial Number.

1. Create procedure for generating scripts and creating Unique Constraints.

CREATE OR REPLACE PROCEDURE SP_TABLE_CONSTRAINT
(P_TABLE_NAME IN VARCHAR2,
 P_COLUMN_SL IN VARCHAR2
 )
 IS
 V_SQL_SCRIPTS VARCHAR2(4000);
 V_COLUMN_LIST VARCHAR2(1000);
 BEGIN
    SELECT TO_CHAR(WM_CONCAT(COLUMN_NAME)) COLUMN_LIST
    INTO V_COLUMN_LIST
    FROM USER_TAB_COLUMNS C, (SELECT REGEXP_SUBSTR(P_COLUMN_SL,'[^,]+', 1, LEVEL) COLUMN_SL FROM DUAL
     CONNECT BY REGEXP_SUBSTR(P_COLUMN_SL, '[^,]+', 1, LEVEL) IS NOT NULL) S
    WHERE TABLE_NAME=P_TABLE_NAME
    AND C.COLUMN_ID=S.COLUMN_SL;
   
    V_SQL_SCRIPTS:='CREATE UNIQUE INDEX IDX_'||P_TABLE_NAME||'_UNQ_'||REPLACE(P_COLUMN_SL,',')||' ON '||P_TABLE_NAME||'('||V_COLUMN_LIST||')';
   
    EXECUTE IMMEDIATE V_SQL_SCRIPTS;
   
    DBMS_OUTPUT.PUT_LINE(V_SQL_SCRIPTS);
 
  EXCEPTION
 
     WHEN OTHERS THEN
     RAISE_APPLICATION_ERROR(-20100,SQLERRM);
   
 END;

2. Call Procedure for Creating Unique Constraints.

DECLARE
  P_TABLE_NAME VARCHAR2(32767);
  P_COLUMN_SL VARCHAR2(32767);

BEGIN
  P_TABLE_NAME := 'CONNECTIONS';
  P_COLUMN_SL := '1,2';

  SP_TABLE_CONSTRAINT ( P_TABLE_NAME, P_COLUMN_SL );

END;

7 comments:

  1. Thanks for sharing the very useful info about Oracle and please keep updating........

    ReplyDelete
  2. Attendance Management Systems. Check out School Management System Software at sharadtechnologies.com. Visit our News section and updates on attendance management systems.

    Attendance Management Systems

    ReplyDelete
  3. This is a nice post thanks for providing such useful information.Keep updating this kind of articles

    ReplyDelete
  4. I read your program ,thanks for sharing. Please visit our link.
    Oracle Fusion SCM Training in Hyderabad

    ReplyDelete
  5. THANKS FOR SHARING YOUR INFORMATION PLEASE KEEP UP DATING THE INFORMATION TIME WENT ON READING THE ARTICLEBEST ORACLE ONLNE COURSE

    ReplyDelete
  6. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well.Thanks for sharing your informative post on development.
    Salesforce Training in Chennai

    Salesforce Online Training in Chennai

    Salesforce Training in Bangalore

    Salesforce Training in Hyderabad

    Salesforce training in ameerpet

    Salesforce Training in Pune

    Salesforce Online Training

    Salesforce Training

    ReplyDelete