【问题标题】:How To Dynamically Insert To Table From SQL View Using PL/SQL如何使用 PL/SQL 从 SQL 视图动态插入表
【发布时间】:2018-09-18 02:07:39
【问题描述】:

如何使用 PL/SQL 从 SQL 视图动态插入到表中:

  • 当 sql 视图已更改时,我想使用 PLSQL Cursor 从 sql 视图动态插入表!!!

  • vi_customer 是一个 sqlview(主数据)

  • customers_1 是一张表

  • IDENTITY_CUSTOMERS 是一个标识列。

  • v_cur_cust_view cur_customer_view%rowtype

代码:

CREATE OR REPLACE PROCEDURE p_customer_1
IS
  CURSOR cur_customer_view
  IS
    SELECT *
    FROM vi_customer
    WHERE IDENTITY_CUSTOMERS IN
      (SELECT vi_cus.IDENTITY_CUSTOMERS FROM vı_customer vi_cus
    MINUS
    SELECT vı_customer.IDENTITY_CUSTOMERS
    FROM vı_customer
    INNER JOIN customers_1
    ON customers_1.IDENTITY_CUSTOMERS=vı_customer.IDENTITY_CUSTOMERS
      );
    v_cur_cust_view cur_customer_view%rowtype;
  BEGIN
    FOR v_cur_cust_view IN cur_customer_view
    LOOP
      INSERT
      INTO customers_1
        (
          cust_last_name,
          cust_emaıl,
          phone_number1,
          phone_number2,
          order_ıd,
          order_tımestamp,
          order_total,
          quantıty,
          unıt_prıce,
          category,
          fılename,
          ımage_last_update,
          product_ımage,
          product_name,
          lıst_prıce,
          mımetype,
          admın_user,
          created_on,
          expıres_on,
          password,
          products,
          user_ıd,
          user_name,
          st,
          state_name,
          customer_ıd,
          cust_fırst_name,
          IDENTITY_CUSTOMERS
        )
        VALUES
        (
          v_cur_cust_view.cust_last_name,
          v_cur_cust_view.cust_emaıl,
          v_cur_cust_view.phone_number1,
          v_cur_cust_view.phone_number2,
          v_cur_cust_view.order_ıd,
          v_cur_cust_view.order_tımestamp,
          v_cur_cust_view.order_total,
          v_cur_cust_view.quantıty,
          v_cur_cust_view.unıt_prıce,
          v_cur_cust_view.category,
          v_cur_cust_view.fılename,
          v_cur_cust_view.ımage_last_update,
          v_cur_cust_view.product_ımage,
          v_cur_cust_view.product_name,
          v_cur_cust_view.lıst_prıce,
          v_cur_cust_view.mımetype,
          v_cur_cust_view.admın_user,
          v_cur_cust_view.created_on,
          v_cur_cust_view.expıres_on,
          v_cur_cust_view.password,
          v_cur_cust_view.products,
          v_cur_cust_view.user_ıd,
          v_cur_cust_view.user_name,
          v_cur_cust_view.st,
          v_cur_cust_view.state_name,
          v_cur_cust_view.customer_ıd,
          v_cur_cust_view.cust_fırst_name,
          v_cur_cust_view.IDENTITY_CUSTOMERS
        );
    END LOOP;
    COMMIT;
  END;

错误:

Connecting to the database db_kg_0.
ORA-00917: missing comma
ORA-06512: at "DB_KG_0.P_CUSTOMER_1", line 15
ORA-06512: at line 2
Process exited.
Disconnecting from the database db_kg_0.

【问题讨论】:

  • 请注意,有错误的行不包含在问题中。问题中没有对db_kg_0 的引用。

标签: sql oracle plsql insert cursor


【解决方案1】:

已解决!!!

insert into customers_1

(
                        cust_last_name,
                        cust_emaıl,
                        phone_number1,
                        phone_number2,
                        order_ıd,
                        order_tımestamp,
                        order_total,
                        quantıty,
                        unıt_prıce,
                        category,
                        fılename,
                        ımage_last_update,
                        product_ımage,
                        product_name,
                        lıst_prıce,
                        mımetype,
                        admın_user,
                        created_on,
                        expıres_on,
                        password,
                        products,
                        user_ıd,
                        user_name,
                        st,
                        state_name,
                        customer_ıd,
                        cust_fırst_name,
                        IDENTITY_CUSTOMERS)

select cust_last_name,
                        cust_emaıl,
                        phone_number1,
                        phone_number2,
                        order_ıd,
                        order_tımestamp,
                        order_total,
                        quantıty,
                        unıt_prıce,
                        category,
                        fılename,
                        ımage_last_update,
                        product_ımage,
                        product_name,
                        lıst_prıce,
                        mımetype,
                        admın_user,
                        created_on,
                        expıres_on,
                        password,
                        products,
                        user_ıd,
                        user_name,
                        st,
                        state_name,
                        customer_ıd,
                        cust_fırst_name,
                        IDENTITY_CUSTOMERS from vi_customer where IDENTITY_CUSTOMERS in (SELECT vi_cus.IDENTITY_CUSTOMERS
FROM vı_customer vi_cus
      MINUS
        SELECT
          vı_customer.IDENTITY_CUSTOMERS
        FROM
            vı_customer  INNER JOIN customers_1 on customers_1.IDENTITY_CUSTOMERS=vı_customer.IDENTITY_CUSTOMERS);

【讨论】:

    【解决方案2】:

    这个逻辑的正确写法是使用insert . . . select,而不是游标和循环:

    INSERT INTO customers_1 ( . . . )
        SELECT . . .
        FROM vi_customer 
        WHERE IDENTITY_CUSTOMERS IN (SELECT c.IDENTITY_CUSTOMERS
                                     FROM vı_customer c
                                     MINUS
                                     SELECT c.IDENTITY_CUSTOMERS
                                     FROM vı_customer c INNER JOIN 
                                          customers_1 c1
                                          ON c1.IDENTITY_CUSTOMERS = c.IDENTITY_CUSTOMERS
                                    );
    

    【讨论】:

    • 如何用循环和光标写?目前逻辑并不重要。
    • @KeremGUL 。 . .相对很少需要光标。当您可以使用基于集合的操作时,您不应该使用它们。了解何时使用光标以及何时不使用光标与学习使用它们一样重要。
    • 插入到选择中通常是最好的...取决于插入集的大小...您可能希望一次插入 x 数量(例如 10000)...如您不希望交易变得太大......但绝对不鼓励问题一次记录一个记录
    • 好的,谢谢@Gordon Linoff
    猜你喜欢
    • 2017-01-31
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    相关资源
    最近更新 更多