【问题标题】:Inserting records to a table by looping through another column and using second table the ID as one of the fields通过循环遍历另一列并将第二个表的 ID 作为其中一个字段来将记录插入到表中
【发布时间】:2023-01-10 12:35:26
【问题描述】:
What I am trying is to loop through the following records from the subquery and assign the ID from the ARINVT table as the first column value for the Inert query. The other two values will be consistently hard keyed. I think the problem is that my subquery returns all the rows from the ARINVT but I need to loop through each one of those rows one at a time when inserting rows to the FGMULTI table. I don't know how to do that although I suspect somehow, I could have a variable be assigned the ID value of the current row I am on in the loop? Just not sure how to proceed. 


Begin

For v in (

Select
  ID
From ARINVT
Left Join ARINVT inv on inv.ID = br.ARINVT_ID
  and inv.CLASS = 'WP')

Loop

Insert Into FGMULTI (ARINVT_ID, LOC_ID, AUTO_DISPO_DEFAULT_LOC)

VALUES
((Select ID FROM ARINVT WHERE CLASS='WP'),27052, 'Y')


End Loop ;

End ;

完整脚本返回以下错误: 开始

对于 v 在 (

选择 ID 来自 ARINVT 在 inv.ID = br.ARINVT_ID 上左加入 ARINVT inv 和 inv.CLASS = 'WP')

环形

插入 FGMULTI(ARINVT_ID、LOC_ID、AUTO_DISPO_DEFAULT_LOC)

价值观 ((Select ID FROM ARINVT WHERE CLASS='WP'),27052, 'Y')

结束循环;

结尾 ; - 好的。 [ 0.0080 秒] 0 行受影响

- 失败的: [FireDAC][Phys][Ora] ORA-06550:第 18 行,第 3 列: PL/SQL: ORA-00933: SQL 命令未正确结束 ORA-06550:第 13 行,第 1 列: PL/SQL:SQL 语句被忽略 ORA-06550:第 21 行,第 5 列: PLS-00103: 遇到符号“;”当期望出现以下情况之一时:

环形

手动 SQL 语句 -> 已完成,出现 1 个错误。

下标返回以下错误:

插入 FGMULTI(ARINVT_ID、LOC_ID、AUTO_DISPO_DEFAULT_LOC)

价值观 ((Select ID FROM ARINVT WHERE CLASS='WP'),27052,'Y') - 好的。 [ 0.0190 秒] 0 行受影响

- 失败的: [FireDAC][Phys][Ora] ORA-01427:单行子查询返回多行

手动 SQL 语句 -> 已完成,但有 1 个错误。

【问题讨论】:

    标签: sql sql-insert


    【解决方案1】:

    目前尚不清楚您要做什么。这是您的代码(为了便于阅读而有点结构化)以及关于错误的 cmets。主要的困境是你的光标应该是什么样子。

    Begin
        For v in (Select ID     -- ID from first ARINVT or self joined one (aliased inv) --> I assumed the first one and put it as br.ID
                  From ARINVT   -- there is no alias though you use it in Left Join's ON clause
                  Left Join ARINVT inv on inv.ID = br.ARINVT_ID and inv.CLASS = 'WP')   -- ON clause should be surrounded with brackets ()
        Loop
            Insert Into FGMULTI (ARINVT_ID, LOC_ID, AUTO_DISPO_DEFAULT_LOC)
                        VALUES((Select ID FROM ARINVT WHERE CLASS='WP'),27052, 'Y') -- if you were using the cursor for loop to get ID what is this Select for
                                -- Here it looks like you need IDs where CLASS is 'WP' - is it this or like in the above cursor
                                -- The cursor would be as it is here - just - Select ID From ARINVT Where CLASS = 'WP'
        End Loop ;
    End;
    

    ...这就是完成工作的方式(可能)...

    Declare
        c   CURSOR IS
                Select      br.ID
                From        ARINVT br
                Left Join   ARINVT inv on (inv.ID = br.ARINVT_ID And inv.CLASS = 'WP');
        -- OR MAYBE JUST LIKE --->  c CURSOR IS Select ID From ARINVT Where CLASS = 'WP';
        m_ID    ARINVT.ID%TYPE;
    Begin
        OPEN c;
        LOOP
            FETCH  c Into m_ID;
            EXIT WHEN c%NOTFOUND;
            Insert Into FGMULTI (ARINVT_ID, LOC_ID, AUTO_DISPO_DEFAULT_LOC) 
                        VALUES(m_ID, 27052, 'Y');
        END  LOOP;
        CLOSE c;
    End;
    

    通过这种方式,您可以声明游标和一个变量以将 ID 提取到其中并循环逐行插入 ID ...

    【讨论】:

      猜你喜欢
      • 2019-07-27
      • 1970-01-01
      • 2022-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多