DB2 存储过程:为游标动态构建 Select 语句 [英] DB2 Stored Procedure: Dynamically Building a Select Statement for cursor

查看:22
本文介绍了DB2 存储过程:为游标动态构建 Select 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对存储过程相当陌生.我天真地认为我可以建立一个如下的选择语句.我做不到,你们中的一些人会对我的想法嗤之以鼻.

I am fairly new to stored procedures. I naively thought that I could build up a select statement as follows. I cannot, and some of you will grin with what I came up with.

如何做我想做的事?

提前致谢.

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@

推荐答案

我解决了这个问题.很丑,但是已经解决了.

I solved this. It's ugly but it's solved.

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@

这篇关于DB2 存储过程:为游标动态构建 Select 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆