Announcement

Collapse
No announcement yet.

automatisches Einfügen von Werten in Tabelle generisch

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • automatisches Einfügen von Werten in Tabelle generisch

    Hallo zusammen,

    ich habe folgendes Insert-Statement:

    INSERT INTO [DBName].[Tabellenname] (USER_NAME, USER_GRP, PERS_ID, LANG_CD) VALUES ('user01 ', 'lgcusers ', 'user01 ', 'DE ');

    Ich möchte jetzt z.B. Einträge erstellen von 01 - 99. Geht das irgendwie generisch, so daß dann 'user01' durch 'user02' ersetzt wird, usw.... ???
    Irgendwie in einer Prozedur mit einer Schleife möglich?


    Gruss
    Jan

  • #2
    Da brauchst keine Schleife:

    Code:
    INSERT INTO tabelle (USER_NAME, USER_GRP, PERS_ID, LANG_CD) 
      SELECT 'user'||TRIM(TO_CHAR(rownum,'00')),
             'lgcusers ',
             'user'||TRIM(TO_CHAR(rownum,'00')),
             'DE' 
      FROM all_objects 
      WHERE rownum<=99
    Dim
    Zitat Tom Kyte:
    I have a simple philosophy when it comes to the Oracle Database: you can treat it as a black box and just stick data into it, or you can understand how it works and exploit it as a powerful computing environment.

    Comment


    • #3
      Ok, danke.

      Für eine leere Tabelle würde das gehen. Unter Umständen sind aber in der Tabelle schon Werte drin, also kann ich die rownum nicht verwenden. Da kommt dann eben "unique constraint violated".

      Die Einträge sollen aber user[01 - 99] heissen.


      Gruss
      Jan

      Comment


      • #4
        In diesem Fall verwendest Du einen MERGE (sofern Du mindestens 9i hast):

        Code:
        MERGE INTO deine_tabelle dest
        USING (SELECT 'user'||TRIM(TO_CHAR(rownum,'00'))USER_NAME,
                      'lgcusers' USER_GRP,
                      'user'||TRIM(TO_CHAR(rownum,'00'))PERS_ID,
                      'DE' LANG
                 FROM all_objects 
                WHERE rownum<=99) src
        ON (dest.user_name=src.user_name)
        WHEN NOT MATCHED THEN
         INSERT(USER_NAME, USER_GRP, PERS_ID, LANG_CD)
         VALUES(src.user_name,src.user_grp,src.pers_id,src.lang_cd)
        Ungetestet, aber der Link für die Doku ist ja dabei.

        Dim
        Zitat Tom Kyte:
        I have a simple philosophy when it comes to the Oracle Database: you can treat it as a black box and just stick data into it, or you can understand how it works and exploit it as a powerful computing environment.

        Comment


        • #5
          In der Version 10 ist die Verwendung von DBMS_ERRLOG eine Alternative, das INSERT geht so für alle DS, die noch nicht in der Tabelle enthalten sind:
          Code:
          CREATE TABLE usertab (USER_NAME VARCHAR2(64), USER_GRP VARCHAR2(64), PERS_ID VARCHAR2(64), LANG_CD VARCHAR2(3));
          
          ALTER TABLE usertab ADD CONSTRAINT UNIQ_USER UNIQUE (PERS_ID) ENABLE;
          
          EXECUTE DBMS_ERRLOG.CREATE_ERROR_LOG('usertab', 'errlog');
          
          TRUNCATE TABLE errlog;
          
          SELECT * FROM errlog;
          
          INSERT INTO usertab VALUES ('user13','abcusers','user13','DE'); 
          
          INSERT INTO usertab (USER_NAME, USER_GRP, PERS_ID, LANG_CD) 
            SELECT 'user'||TRIM(TO_CHAR(rownum,'00')),
                   'lgcusers ',
                   'user'||TRIM(TO_CHAR(rownum,'00')),
                   'DE' 
                from dual
                connect by level <= 99
            LOG ERRORS INTO errlog ('user_doppelt') REJECT LIMIT 101;
            
          SELECT * FROM errlog;

          Comment


          • #6
            @dimitri:

            bei dem verwenden von merge bekomme ich folgenden Fehler:

            Error starting at line 1 in command:
            MERGE INTO tabelle dest
            USING (SELECT 'user'||TRIM(TO_CHAR(rownum,'00'))USER_NAME,
            'lgcusers ',
            'user'||TRIM(TO_CHAR(rownum,'00'))PERS_ID,
            'DE',
            '20080813000000',
            '20080813000000',
            '1',
            'xxx'
            FROM all_objects
            WHERE rownum<=99) src
            ON (dest.user_name=src.user_name)
            WHEN NOT MATCHED THEN
            INSERT(USER_NAME, USER_GRP, PERS_ID, LANG_CD, ins_dt_zt, upd_dt_zt, upd_proc_nr, upd_personal_id)
            VALUES(src.user_name,src.user_grp,src.pers_id,src. lang_cd,src.ins_dt_zt,src.upd_dt_zt,src.upd_proc_n r,src.upd_personal_id)
            Error at Command Line:12 Column:33
            Error report:
            SQL Error: ORA-00918: column ambiguously defined
            00918. 00000 - "column ambiguously defined"
            *Cause:
            *Action:


            @jum:

            funktioniert einwandfrei ... danke.
            Zuletzt editiert von jan21; 13.08.2008, 12:31.

            Comment


            • #7
              Du musst für alle Spalten die Du angiebst auch Aliase definieren.

              Code:
              user'||TRIM(TO_CHAR(rownum,'00'))PERS_ID,
              'DE',
              '20080813000000',
              '20080813000000',
              So wie bei der PERS_ID muss das auch für die anderen 3 Spalten gemacht werden.

              Ansonsten weißt der Fehler darauf hin, dass Du irgendwo den Qualifier src oder dest vergessen hat und Oracle nicht weiß zu welcher Tabelle die Spalte gehört.

              Dim
              Zitat Tom Kyte:
              I have a simple philosophy when it comes to the Oracle Database: you can treat it as a black box and just stick data into it, or you can understand how it works and exploit it as a powerful computing environment.

              Comment


              • #8
                Ah, ok alles klar.

                Vielen Dank euch beiden.

                Gruss
                Jan

                Comment

                Working...
                X