用于从具有多个条件的多个表中的多个列中获取数据的存储过程

Posted

技术标签:

【中文标题】用于从具有多个条件的多个表中的多个列中获取数据的存储过程【英文标题】:stored procedure to fetch data from multiple columns in multiple tables with multiple criteria's 【发布时间】:2017-10-18 00:50:07 【问题描述】:

我在寻找什么?

创建一个存储过程来将结果写入一个表,或者最坏的情况是简化这个查询。

查询

SELECT CONCAT(ba.BRANCH_NUMBER,ba.CLIENT_ID) AS BANK_ACCOUNT_NUMBER,
bp.ORIGINAL_REGISTERED_ID,
bap.ANOTHER_COLUMN_1,
bap.ANOTHER_COLUMN_2,
bap.ANOTHER_COLUMN_3,
bap.ANOTHER_COLUMN_4,
bd.ANOTHER_COLUMN_1,
bd.REGISTERED_ID
FROM BANK_ACCOUNT_PARTNER bap,
BANK_PARTNER bp,
BANK_ACCOUNT ba,
BANKING_DATA bd
WHERE bap.PARTNER_ID=bp.PARTNER_ID
AND bap.ACCOUNT_ID=ba.ACCOUNT_ID
AND ba.REGISTERED_ID=bd.REGISTERED_ID
AND bap.ACCOUNT_ID IN
    (SELECT ACCOUNT_ID FROM(SELECT ba.ACCOUNT_ID,
    ROW_NUMBER() OVER (PARTITION BY ba.BRANCH_NUMBER, ba.CLIENT_ID ORDER BY bd.ACCOUNT_OPENED_DATE DESC) ROW_NUMBER
    FROM BANK_ACCOUNT ba,
    BANKING_DATA bd,
    ANOTHER_TABLE at
    WHERE da.REGISTERED_ID = bd.REGISTERED_ID
    AND at.REGISTERED_ID=bd.REGISTERED_ID
    AND ANOTHER_CRITERIA_COLUMN = 'someValue'
    AND ANOTHER_CRITERIA_COLUMN IN ('someValue1','someValue2','someValue3')
    AND ba.ANOTHER_CRITERIA_COLUMN='someValue'
    AND ba.ACCOUNT_MATURITY_DATE IS NULL) WHERE ROW_NUMBER='1');

提前致谢。

【问题讨论】:

问题是什么? @BaSsGaz - 我如何通过创建存储过程来简化这一点 它应该怎么做?只执行这个查询? 如问题所述,我需要将这个查询的结果写入一个表中 【参考方案1】:

对于标量查询,您可以使用CURSOR 或简单的SELECT INTO 从查询中检索结果,如下所示:

CREATE PROCEDURE myProcedure IS
        BANK_ACCOUNT_NUMBER VARCHAR(30);
        ORIGINAL_REGISTERED_ID BANK_PARTNER.ORIGINAL_REGISTERED_ID%TYPE;
        ANOTHER_COLUMN_BAP1 BANK_ACCOUNT_PARTNER.ANOTHER_COLUMN_1%TYPE;
        ANOTHER_COLUMN_2 BANK_ACCOUNT_PARTNER.ANOTHER_COLUMN_2%TYPE;
        ANOTHER_COLUMN_3 BANK_ACCOUNT_PARTNER.ANOTHER_COLUMN_3%TYPE;
        ANOTHER_COLUMN_4 BANK_ACCOUNT_PARTNER.ANOTHER_COLUMN_4%TYPE;
        ANOTHER_COLUMN_BD1 BANKING_DATA.ANOTHER_COLUMN_1%TYPE;
        REGISTERED_ID BANKING_DATA.REGISTERED_ID%TYPE;
BEGIN
    SELECT CONCAT(ba.BRANCH_NUMBER,ba.CLIENT_ID) AS BANK_ACCOUNT_NUMBER,
    bp.ORIGINAL_REGISTERED_ID,
    bap.ANOTHER_COLUMN_1,
    bap.ANOTHER_COLUMN_2,
    bap.ANOTHER_COLUMN_3,
    bap.ANOTHER_COLUMN_4,
    bd.ANOTHER_COLUMN_1,
    bd.REGISTERED_ID INTO BANK_ACCOUNT_NUMBER, ORIGINAL_REGISTERED_ID, ANOTHER_COLUMN_BAP1, ANOTHER_COLUMN_2, ANOTHER_COLUMN_3, ANOTHER_COLUMN_4,  ANOTHER_COLUMN_BD1, REGISTERED_ID
    FROM BANK_ACCOUNT_PARTNER bap,
    BANK_PARTNER bp,
    BANK_ACCOUNT ba,
    BANKING_DATA bd
    WHERE bap.PARTNER_ID=bp.PARTNER_ID
    AND bap.ACCOUNT_ID=ba.ACCOUNT_ID
    AND ba.REGISTERED_ID=bd.REGISTERED_ID
    AND bap.ACCOUNT_ID IN
        (SELECT ACCOUNT_ID FROM(SELECT ba.ACCOUNT_ID,
        ROW_NUMBER() OVER (PARTITION BY ba.BRANCH_NUMBER, ba.CLIENT_ID ORDER BY bd.ACCOUNT_OPENED_DATE DESC) ROW_NUMBER
        FROM BANK_ACCOUNT ba,
        BANKING_DATA bd,
        ANOTHER_TABLE at
        WHERE da.REGISTERED_ID = bd.REGISTERED_ID
        AND at.REGISTERED_ID=bd.REGISTERED_ID
        AND ANOTHER_CRITERIA_COLUMN = 'someValue'
        AND ANOTHER_CRITERIA_COLUMN IN ('someValue1','someValue2','someValue3')
        AND ba.ANOTHER_CRITERIA_COLUMN='someValue'
        AND ba.ACCOUNT_MATURITY_DATE IS NULL) WHERE ROW_NUMBER='1');

    INSERT INTO myTable VALUES(BANK_ACCOUNT_NUMBER, ORIGINAL_REGISTERED_ID, ANOTHER_COLUMN_BAP1, ANOTHER_COLUMN_2, ANOTHER_COLUMN_3, ANOTHER_COLUMN_4,  ANOTHER_COLUMN_BD1, REGISTERED_ID);
END;
/

或者使用CURSOR 循环:

CREATE PROCEDURE myProcedure IS
    CURSOR query IS 
        SELECT CONCAT(ba.BRANCH_NUMBER,ba.CLIENT_ID) AS BANK_ACCOUNT_NUMBER,
        bp.ORIGINAL_REGISTERED_ID,
        bap.ANOTHER_COLUMN_1 AS ANOTHER_COLUMN_BAP1,
        bap.ANOTHER_COLUMN_2,
        bap.ANOTHER_COLUMN_3,
        bap.ANOTHER_COLUMN_4,
        bd.ANOTHER_COLUMN_1 AS ANOTHER_COLUMN_BD1,
        bd.REGISTERED_ID
        FROM BANK_ACCOUNT_PARTNER bap,
        BANK_PARTNER bp,
        BANK_ACCOUNT ba,
        BANKING_DATA bd
        WHERE bap.PARTNER_ID=bp.PARTNER_ID
        AND bap.ACCOUNT_ID=ba.ACCOUNT_ID
        AND ba.REGISTERED_ID=bd.REGISTERED_ID
        AND bap.ACCOUNT_ID IN
            (SELECT ACCOUNT_ID FROM(SELECT ba.ACCOUNT_ID,
            ROW_NUMBER() OVER (PARTITION BY ba.BRANCH_NUMBER, ba.CLIENT_ID ORDER BY bd.ACCOUNT_OPENED_DATE DESC) ROW_NUMBER
            FROM BANK_ACCOUNT ba,
            BANKING_DATA bd,
            ANOTHER_TABLE at
            WHERE da.REGISTERED_ID = bd.REGISTERED_ID
            AND at.REGISTERED_ID=bd.REGISTERED_ID
            AND ANOTHER_CRITERIA_COLUMN = 'someValue'
            AND ANOTHER_CRITERIA_COLUMN IN ('someValue1','someValue2','someValue3')
            AND ba.ANOTHER_CRITERIA_COLUMN='someValue'
            AND ba.ACCOUNT_MATURITY_DATE IS NULL) WHERE ROW_NUMBER='1');
BEGIN
     FOR line IN query LOOP
        INSERT INTO myTable VALUES(line.BANK_ACCOUNT_NUMBER, line.ORIGINAL_REGISTERED_ID, line.ANOTHER_COLUMN_BAP1, line.ANOTHER_COLUMN_2, line.ANOTHER_COLUMN_3, line.ANOTHER_COLUMN_4,  line.ANOTHER_COLUMN_BD1, line.REGISTERED_ID);
     END LOOP;
END;
/

【讨论】:

感谢@BaSsGaz,关于性能 CURSOR 与 SELECT INTO 的任何建议? @Cmen535 我不知道有任何明显的性能差异!我个人使用SELECT INTO 进行这样的快速查询关联,当我需要循环结果以获得更多控制权时使用CURSOR

以上是关于用于从具有多个条件的多个表中的多个列中获取数据的存储过程的主要内容,如果未能解决你的问题,请参考以下文章

在其他列中计算具有多个条件的 NaN 单列

在多表列中具有多个术语的简单 PHP 过滤器

根据条件从多个表中获取数据

MYSQL - 1列中的多个所有数据在同一列中具有最大值

归一化具有多个条件的数据帧,R

如何根据多个条件从 SQL Server 中删除大量数据