Stored procedures DB2存储过程:为游标动态构建Select语句

Stored procedures DB2存储过程:为游标动态构建Select语句,stored-procedures,db2,Stored Procedures,Db2,我对存储过程相当陌生。我天真地认为我可以建立一个select语句,如下所示。我不能,你们中的一些人会因为我的想法而笑 一个人如何做我想做的事 提前谢谢 CREATE PROCEDURE GET_CUSTOMER_FOR_BORROWER_LETTER ( IN APPLICATION_ID INTEGER, IN GET_GUARANTOR INTEGER, IN GET_PREFERRED_CONTACT INTEGER ) DYNAMIC RESULT

我对存储过程相当陌生。我天真地认为我可以建立一个select语句,如下所示。我不能,你们中的一些人会因为我的想法而笑

一个人如何做我想做的事

提前谢谢

CREATE PROCEDURE GET_CUSTOMER_FOR_BORROWER_LETTER (


    IN APPLICATION_ID INTEGER,
    IN GET_GUARANTOR INTEGER,
    IN GET_PREFERRED_CONTACT INTEGER
    )

DYNAMIC RESULT SETS 1
READS SQL DATA

P1:BEGIN
    DECLARE selectStmt VARCHAR(800);
    DECLARE selectStmtPreferred VARCHAR(400);
    DECLARE selectStmtApplicants VARCHAR(400);
    DECLARE selectStmtGuarantor VARCHAR(400);


    DECLARE cursor1 CURSOR WITH RETURN FOR
    selectStmt -- will define this later, conditionally (babe in the woods :) )
    OPEN cursor1;


    set selectStmtPreferred = 'select "preferred applicant" as recipient_type, app.APPLICATION_ID, cust.KEY from application app, customer cust, application_detail appd where app.application_id = 407634 and app.APPLICATION_ID = appd.APPLICATION_ID  and appd.PREFERRED_CONTACT_ID = cust.KEY';

    set selectStmtApplicants = 'select "applicant" as recipient_type, app.APPLICATION_ID, cust.KEY from application app, applicant applc, customer cust where app.application_id = 407634 and applc.APPLICATION_ID = app.APPLICATION_ID and applc.CUST_ID = cust.CUST_ID';

    set selectStmtGuarantor = ' union select "guarantor" as recipient_type ,app.APPLICATION_ID, cust.KEY from application app, application_guarantor appg, customer cust where app.application_id = 407634 and appg.APPLICATION_ID = app.APPLICATION_ID and appg.CUST_ID = cust.CUST_ID';

    IF GET_PREFERRED_CONTACT = 1 THEN

        IF GET_GUARANTOR = 1 THEN 
            SET selectStmt = concat (selectStmtPreferred,selectStmtGuarantor);
        ELSE 
            SET selectStmt = selectStmtPreferred;
        END IF;
    ELSE 
        IF GET_GUARANTOR = 1 THEN 
            SET selectStmt = concat (selectStmtApplicants,selectStmtGuarantor);
        ELSE 
            SET selectStmt = selectStmtApplicants;
        END IF;
    END IF;
selectStmt = concat (selectStmtPreferred,";");


END P1@

我解决了这个问题。这很难看,但已经解决了

P1:BEGIN

    DECLARE preferredWithGuarantor CURSOR WITH RETURN FOR
            select 'preferred applicant' as recipient_type, app.APPLICATION_ID, cust.KEY from application app, customer cust, application_detail appd where app.application_id = 407634 and app.APPLICATION_ID = appd.APPLICATION_ID  and appd.PREFERRED_CONTACT_ID = cust.KEY union select 'guarantor' as recipient_type ,app.APPLICATION_ID, cust.KEY from application app, application_guarantor appg, customer cust where app.application_id = 407634 and appg.APPLICATION_ID = app.APPLICATION_ID and appg.CUST_ID = cust.CUST_ID;

    DECLARE preferred CURSOR WITH RETURN FOR
            select 'preferred applicant' as recipient_type, app.APPLICATION_ID, cust.KEY from application app, customer cust, application_detail appd where app.application_id = 407634 and app.APPLICATION_ID = appd.APPLICATION_ID  and appd.PREFERRED_CONTACT_ID = cust.KEY;

    DECLARE applicantWithGuarantor CURSOR WITH RETURN FOR
            select 'applicant' as recipient_type, app.APPLICATION_ID, cust.KEY from application app, applicant applc, customer cust where app.application_id = 407634 and applc.APPLICATION_ID = app.APPLICATION_ID and applc.CUST_ID = cust.CUST_ID union select 'guarantor' as recipient_type ,app.APPLICATION_ID, cust.KEY from application app, application_guarantor appg, customer cust where app.application_id = 407634 and appg.APPLICATION_ID = app.APPLICATION_ID and appg.CUST_ID = cust.CUST_ID;

    DECLARE applicant CURSOR WITH RETURN FOR
            select 'applicant' as recipient_type, app.APPLICATION_ID, cust.KEY from application app, applicant applc, customer cust where app.application_id = 407634 and applc.APPLICATION_ID = app.APPLICATION_ID and applc.CUST_ID = cust.CUST_ID;


    IF GET_PREFERRED_CONTACT = 1 THEN
        IF GET_GUARANTOR = 1 THEN 
            open preferredWithGuarantor;
        ELSE 
            open preferred;
        END IF;
    ELSE 
        IF GET_GUARANTOR = 1 THEN 
            open applicantWithGuarantor;
        ELSE
            open applicant;
        END IF;
    END IF;

END P1@

下次需要构建一些动态SQL语句时,请尝试此方法

DECLARE SELECT_STATEMENT VARCHAR(8000);

DECLARE cursor1 CURSOR WITH RETURN FOR SQL_STATEMENT;

...build dynamic sql here...

PREPARE SQL_STATEMENT FROM SELECT_STATEMENT;

OPEN cursor1;

明确地说,SQL_语句是您想要的任何名称。只需确保游标声明和PREPARE语句中的内容相同。

嗯,通常您必须知道
SELECT
语句是什么,才能声明游标(如果它不是动态字符串,通常意味着编译时)。并研究如何使用
PREPARE
EXECUTE
,这将允许您连接字符串。如果s,您应该能够取消嵌套
。不要使用隐式连接语法,它已被弃用;写下你的
JOIN
s。这可能很难看,但它肯定比在代码中动态构建SQL语句更容易阅读和维护。做得好,尽管这很紧急,但你已经将SQL注入的风险降到了最低。