How to insert into temp table when looping through a string - Oracle - PL/SQL










1














CREATE GLOBAL TEMPORARY TABLE tt_temptable(
RowNums NUMBER(3,0),
procNums NUMBER(18,0)
) ON COMMIT PRESERVE ROWS;

inputString VARCHAR2 ;

inputString := '12,13,14,15'


SELECT REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) ProcNums
FROM dual CONNECT BY REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) IS NOT NULL;

INSERT INTO tt_temptable(
SELECT identity(3) RowNums,procNums
FROM

);


Want to insert 12 , 13, 14 , 15 and identity of 3 length in the temptable so total 4 rows in temptable










share|improve this question




























    1














    CREATE GLOBAL TEMPORARY TABLE tt_temptable(
    RowNums NUMBER(3,0),
    procNums NUMBER(18,0)
    ) ON COMMIT PRESERVE ROWS;

    inputString VARCHAR2 ;

    inputString := '12,13,14,15'


    SELECT REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) ProcNums
    FROM dual CONNECT BY REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) IS NOT NULL;

    INSERT INTO tt_temptable(
    SELECT identity(3) RowNums,procNums
    FROM

    );


    Want to insert 12 , 13, 14 , 15 and identity of 3 length in the temptable so total 4 rows in temptable










    share|improve this question


























      1












      1








      1







      CREATE GLOBAL TEMPORARY TABLE tt_temptable(
      RowNums NUMBER(3,0),
      procNums NUMBER(18,0)
      ) ON COMMIT PRESERVE ROWS;

      inputString VARCHAR2 ;

      inputString := '12,13,14,15'


      SELECT REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) ProcNums
      FROM dual CONNECT BY REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) IS NOT NULL;

      INSERT INTO tt_temptable(
      SELECT identity(3) RowNums,procNums
      FROM

      );


      Want to insert 12 , 13, 14 , 15 and identity of 3 length in the temptable so total 4 rows in temptable










      share|improve this question















      CREATE GLOBAL TEMPORARY TABLE tt_temptable(
      RowNums NUMBER(3,0),
      procNums NUMBER(18,0)
      ) ON COMMIT PRESERVE ROWS;

      inputString VARCHAR2 ;

      inputString := '12,13,14,15'


      SELECT REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) ProcNums
      FROM dual CONNECT BY REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) IS NOT NULL;

      INSERT INTO tt_temptable(
      SELECT identity(3) RowNums,procNums
      FROM

      );


      Want to insert 12 , 13, 14 , 15 and identity of 3 length in the temptable so total 4 rows in temptable







      sql oracle plsql oracle12c






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 16:01









      Barbaros Özhan

      12.1k71530




      12.1k71530










      asked Nov 12 at 14:41









      Shaswata

      6911




      6911






















          2 Answers
          2






          active

          oldest

          votes


















          2














          If you use Oracle 12c, then you may define an IDENTITY column through GENERATED ALWAYS AS IDENTITY in your table definition and follow the way below :



          SQL> CREATE GLOBAL TEMPORARY TABLE tt_temptable(
          2 RowNums NUMBER(3,0) GENERATED ALWAYS AS IDENTITY,
          3 procNums NUMBER(18,0)
          4 ) ON COMMIT PRESERVE ROWS;

          Table created

          SQL>
          SQL> DECLARE
          2 inputString VARCHAR2(50) := '12,13,14,15';
          3 BEGIN
          4 INSERT INTO tt_temptable(procNums)
          5 SELECT REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) ProcNums
          6 FROM dual
          7 CONNECT BY REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) IS NOT NULL;
          8 END;
          9 /

          PL/SQL procedure successfully completed

          SQL> SELECT * FROM tt_temptable;

          ROWNUMS PROCNUMS
          ------- -------------------
          1 12
          2 13
          3 14
          4 15


          To reset the IDENTITY column (RowNums), use :



          SQL> ALTER TABLE tt_temptable MODIFY( RowNums Generated as Identity (START WITH 1));


          whenever the shared locks on the table are released.






          share|improve this answer






















          • any idea how to reset the identties of this temp table
            – Shaswata
            Nov 12 at 15:24


















          1














          insert 
          into tt_temptable
          select NVL((select max(a.rownums)
          from tt_temptable a
          ),100)+rownum
          ,procNums
          from (SELECT REGEXP_SUBSTR ('10,20,30','[^,]+',1,LEVEL) ProcNums,level as lvl
          FROM dual
          CONNECT BY REGEXP_SUBSTR ('10,20,30','[^,]+',1,LEVEL) IS NOT NULL
          )x





          share|improve this answer




















          • thanks a lot , any idea how to reset the identities on temp table
            – Shaswata
            Nov 12 at 15:22










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264509%2fhow-to-insert-into-temp-table-when-looping-through-a-string-oracle-pl-sql%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          If you use Oracle 12c, then you may define an IDENTITY column through GENERATED ALWAYS AS IDENTITY in your table definition and follow the way below :



          SQL> CREATE GLOBAL TEMPORARY TABLE tt_temptable(
          2 RowNums NUMBER(3,0) GENERATED ALWAYS AS IDENTITY,
          3 procNums NUMBER(18,0)
          4 ) ON COMMIT PRESERVE ROWS;

          Table created

          SQL>
          SQL> DECLARE
          2 inputString VARCHAR2(50) := '12,13,14,15';
          3 BEGIN
          4 INSERT INTO tt_temptable(procNums)
          5 SELECT REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) ProcNums
          6 FROM dual
          7 CONNECT BY REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) IS NOT NULL;
          8 END;
          9 /

          PL/SQL procedure successfully completed

          SQL> SELECT * FROM tt_temptable;

          ROWNUMS PROCNUMS
          ------- -------------------
          1 12
          2 13
          3 14
          4 15


          To reset the IDENTITY column (RowNums), use :



          SQL> ALTER TABLE tt_temptable MODIFY( RowNums Generated as Identity (START WITH 1));


          whenever the shared locks on the table are released.






          share|improve this answer






















          • any idea how to reset the identties of this temp table
            – Shaswata
            Nov 12 at 15:24















          2














          If you use Oracle 12c, then you may define an IDENTITY column through GENERATED ALWAYS AS IDENTITY in your table definition and follow the way below :



          SQL> CREATE GLOBAL TEMPORARY TABLE tt_temptable(
          2 RowNums NUMBER(3,0) GENERATED ALWAYS AS IDENTITY,
          3 procNums NUMBER(18,0)
          4 ) ON COMMIT PRESERVE ROWS;

          Table created

          SQL>
          SQL> DECLARE
          2 inputString VARCHAR2(50) := '12,13,14,15';
          3 BEGIN
          4 INSERT INTO tt_temptable(procNums)
          5 SELECT REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) ProcNums
          6 FROM dual
          7 CONNECT BY REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) IS NOT NULL;
          8 END;
          9 /

          PL/SQL procedure successfully completed

          SQL> SELECT * FROM tt_temptable;

          ROWNUMS PROCNUMS
          ------- -------------------
          1 12
          2 13
          3 14
          4 15


          To reset the IDENTITY column (RowNums), use :



          SQL> ALTER TABLE tt_temptable MODIFY( RowNums Generated as Identity (START WITH 1));


          whenever the shared locks on the table are released.






          share|improve this answer






















          • any idea how to reset the identties of this temp table
            – Shaswata
            Nov 12 at 15:24













          2












          2








          2






          If you use Oracle 12c, then you may define an IDENTITY column through GENERATED ALWAYS AS IDENTITY in your table definition and follow the way below :



          SQL> CREATE GLOBAL TEMPORARY TABLE tt_temptable(
          2 RowNums NUMBER(3,0) GENERATED ALWAYS AS IDENTITY,
          3 procNums NUMBER(18,0)
          4 ) ON COMMIT PRESERVE ROWS;

          Table created

          SQL>
          SQL> DECLARE
          2 inputString VARCHAR2(50) := '12,13,14,15';
          3 BEGIN
          4 INSERT INTO tt_temptable(procNums)
          5 SELECT REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) ProcNums
          6 FROM dual
          7 CONNECT BY REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) IS NOT NULL;
          8 END;
          9 /

          PL/SQL procedure successfully completed

          SQL> SELECT * FROM tt_temptable;

          ROWNUMS PROCNUMS
          ------- -------------------
          1 12
          2 13
          3 14
          4 15


          To reset the IDENTITY column (RowNums), use :



          SQL> ALTER TABLE tt_temptable MODIFY( RowNums Generated as Identity (START WITH 1));


          whenever the shared locks on the table are released.






          share|improve this answer














          If you use Oracle 12c, then you may define an IDENTITY column through GENERATED ALWAYS AS IDENTITY in your table definition and follow the way below :



          SQL> CREATE GLOBAL TEMPORARY TABLE tt_temptable(
          2 RowNums NUMBER(3,0) GENERATED ALWAYS AS IDENTITY,
          3 procNums NUMBER(18,0)
          4 ) ON COMMIT PRESERVE ROWS;

          Table created

          SQL>
          SQL> DECLARE
          2 inputString VARCHAR2(50) := '12,13,14,15';
          3 BEGIN
          4 INSERT INTO tt_temptable(procNums)
          5 SELECT REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) ProcNums
          6 FROM dual
          7 CONNECT BY REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) IS NOT NULL;
          8 END;
          9 /

          PL/SQL procedure successfully completed

          SQL> SELECT * FROM tt_temptable;

          ROWNUMS PROCNUMS
          ------- -------------------
          1 12
          2 13
          3 14
          4 15


          To reset the IDENTITY column (RowNums), use :



          SQL> ALTER TABLE tt_temptable MODIFY( RowNums Generated as Identity (START WITH 1));


          whenever the shared locks on the table are released.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 15:34

























          answered Nov 12 at 15:20









          Barbaros Özhan

          12.1k71530




          12.1k71530











          • any idea how to reset the identties of this temp table
            – Shaswata
            Nov 12 at 15:24
















          • any idea how to reset the identties of this temp table
            – Shaswata
            Nov 12 at 15:24















          any idea how to reset the identties of this temp table
          – Shaswata
          Nov 12 at 15:24




          any idea how to reset the identties of this temp table
          – Shaswata
          Nov 12 at 15:24













          1














          insert 
          into tt_temptable
          select NVL((select max(a.rownums)
          from tt_temptable a
          ),100)+rownum
          ,procNums
          from (SELECT REGEXP_SUBSTR ('10,20,30','[^,]+',1,LEVEL) ProcNums,level as lvl
          FROM dual
          CONNECT BY REGEXP_SUBSTR ('10,20,30','[^,]+',1,LEVEL) IS NOT NULL
          )x





          share|improve this answer




















          • thanks a lot , any idea how to reset the identities on temp table
            – Shaswata
            Nov 12 at 15:22















          1














          insert 
          into tt_temptable
          select NVL((select max(a.rownums)
          from tt_temptable a
          ),100)+rownum
          ,procNums
          from (SELECT REGEXP_SUBSTR ('10,20,30','[^,]+',1,LEVEL) ProcNums,level as lvl
          FROM dual
          CONNECT BY REGEXP_SUBSTR ('10,20,30','[^,]+',1,LEVEL) IS NOT NULL
          )x





          share|improve this answer




















          • thanks a lot , any idea how to reset the identities on temp table
            – Shaswata
            Nov 12 at 15:22













          1












          1








          1






          insert 
          into tt_temptable
          select NVL((select max(a.rownums)
          from tt_temptable a
          ),100)+rownum
          ,procNums
          from (SELECT REGEXP_SUBSTR ('10,20,30','[^,]+',1,LEVEL) ProcNums,level as lvl
          FROM dual
          CONNECT BY REGEXP_SUBSTR ('10,20,30','[^,]+',1,LEVEL) IS NOT NULL
          )x





          share|improve this answer












          insert 
          into tt_temptable
          select NVL((select max(a.rownums)
          from tt_temptable a
          ),100)+rownum
          ,procNums
          from (SELECT REGEXP_SUBSTR ('10,20,30','[^,]+',1,LEVEL) ProcNums,level as lvl
          FROM dual
          CONNECT BY REGEXP_SUBSTR ('10,20,30','[^,]+',1,LEVEL) IS NOT NULL
          )x






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 15:17









          George Joseph

          1,30249




          1,30249











          • thanks a lot , any idea how to reset the identities on temp table
            – Shaswata
            Nov 12 at 15:22
















          • thanks a lot , any idea how to reset the identities on temp table
            – Shaswata
            Nov 12 at 15:22















          thanks a lot , any idea how to reset the identities on temp table
          – Shaswata
          Nov 12 at 15:22




          thanks a lot , any idea how to reset the identities on temp table
          – Shaswata
          Nov 12 at 15:22

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264509%2fhow-to-insert-into-temp-table-when-looping-through-a-string-oracle-pl-sql%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線