【问题标题】:How to store fetched bulk values into single variable and insert all values it on one variable using pl/sql如何将获取的批量值存储到单个变量中并使用 pl/sql 将所有值插入到一个变量中
【发布时间】:2020-06-05 07:42:36
【问题描述】:

注意

有人告诉我如何将单个列存储的多行值存储到单个变量中,并代表该变量返回的值单击一次存储在表中如何使用 pl/sql 执行此类场景。

喜欢这样的查询返回单列多行我想将它存储到一些var中并在单击过程中插入所有值。它会生成错误的错误图像已附加

    DECLARE
    l_respondent_id   VARCHAR(100);
BEGIN
    FOR c IN (
        SELECT
            s.gr_number
        FROM
            student s
            LEFT JOIN class_time ct ON ct.class_id = s.class_id
                                       AND instr(s.class_time, ct.class_time) > 0
        WHERE
            upper(TRIM(ct.class_id)) = upper(TRIM(:app_user))
            AND s.gr_number IS NOT NULL
            AND is_active_flg = 'Y'
    ) LOOP
        l_respondent_id := c.gr_number;
        BEGIN
            SELECT
                s.gr_number
            INTO l_respondent_id
            FROM
                student s
                LEFT JOIN class_time ct ON ct.class_id = s.class_id
                                           AND instr(s.class_time, ct.class_time) > 0
            WHERE
                upper(TRIM(ct.class_id)) = upper(TRIM(:app_user))
                AND s.gr_number IS NOT NULL
                AND is_active_flg = 'Y'
                AND s.gr_number = c.gr_number;

        EXCEPTION
            WHEN no_data_found THEN
                l_respondent_id := NULL;
        END;

        IF l_respondent_id IS NULL THEN
            INSERT INTO student_class_attend ( gr_number ) VALUES ( c.gr_number ) RETURNING gr_number INTO l_respondent_id;

        END IF;

    END LOOP;
END;

【问题讨论】:

    标签: sql oracle plsql


    【解决方案1】:

    您可能可以为此使用嵌套表,同时使用 BULK COLLECT 和 FORALL。简化示例:

    表格和数据

    create table students( id primary key, fname, lname )
    as
    select 1, 'fname_1', 'lname_1' from dual union all
    select 2, 'fname_2', 'lname_2' from dual union all
    select 3, 'fname_3', 'lname_3' from dual union all
    select 4, 'fname_4', 'lname_4' from dual union all
    select 5, 'fname_5', 'lname_5' from dual union all
    select 6, 'fname_6', 'lname_6' from dual ;
    
    -- empty
    create table attendance( studentid number references students, when_ date ) ;
    

    匿名屏蔽

    declare
    
    -- This will allow you to store several studentIDs:
      type studentid_t is table of students.id%type index by pls_integer ;
    
    -- The current attendance list.  Notice the TYPE.  
      attendancelist studentid_t ;
    
    begin
    
    -- Your query (including all the necessary joins and conditions) here.
    -- In this case, we will pick up 3 of the 6 IDs.
      select id bulk collect into attendancelist
      from students
      where mod( id, 2 ) = 0 ; 
    
    -- Write a _single_ DML statement after FORALL (which is not a loop - see docs) 
      forall i in attendancelist.first .. attendancelist.last
        insert into attendance( studentid, when_ ) 
          values( attendancelist( i ) , sysdate ) ; -- use whatever you need here
    
    end ;
    /
    

    结果(执行匿名块后)

    SQL> select * from attendance ;
    
     STUDENTID WHEN_    
    ---------- ---------
             2 07-JUN-20
             4 07-JUN-20
             6 07-JUN-20
    

    DBfiddle here.

    【讨论】:

    • 谢谢,但是当我的 select 语句为单列返回 11-13 行时,您只获得了限制数据,我想插入所有这些,在这种情况下我该怎么办?
    • 不太清楚你所说的“你只是得到限制数据”是什么意思。如果您的选择返回 11-13 行,请使用相同的原则(定义您自己的 TYPE,使用 BULK COLLECT 和 FORALL。参见另一个示例,我们选择 100 个学生 ID(“单列”),并通过 FORALL 插入它们。 . 插入“考勤表”:dbfiddle.uk/…
    猜你喜欢
    • 2020-09-23
    • 1970-01-01
    • 2022-12-19
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    相关资源
    最近更新 更多