SAP HANA 存储过程中的IF While使用案例

-- run 1_datatype_data.sql before running this script

ALTER SESSION SET CURRENT_SCHEMA = SqlScriptDocumentation;

drop procedure while_proc; 
CREATE PROCEDURE while_proc LANGUAGE SQLSCRIPT AS 
    v_index1 INT := 0; 
    v_index2 INT := 0; 
    v_msg    VARCHAR(200) := ''; 
BEGIN 
    init_proc();

    WHILE :v_index1 < 5 DO 
        v_msg := 'Here is ' || :v_index1 || '.'; 
        ins_msg_proc(:v_msg); 
        v_index1 := :v_index1 + 1; 
    END WHILE;

    v_index1 := 0; 
    WHILE :v_index1 < 5 DO 
        v_index2 := 0; 
        WHILE :v_index2 < 5 DO 
            v_msg := 'Here is '|| :v_index1 || '-' || :v_index2 || '.'; 
            ins_msg_proc(:v_msg); 
            v_index2 := :v_index2 + 1; 
        END WHILE; 
        v_index1 := :v_index1 + 1; 
    END WHILE; 
END;

CALL while_proc(); 
SELECT message FROM message_box;

-----------------------------------------------------------

drop procedure upsert_proc; 
CREATE PROCEDURE upsert_proc (IN v_isbn VARCHAR(20)) LANGUAGE SQLSCRIPT 
AS 
    found INT := 1; 
BEGIN 
    init_proc(); 
    WHILE :found <> 0 DO 
        SELECT count(*) INTO found FROM books WHERE isbn = :v_isbn; 
        IF :found IS NULL THEN 
            ins_msg_proc('result of count(*) cannot be NULL'); 
        ELSE 
            ins_msg_proc('result of count(*) not NULL - as expected'); 
        END IF; 
            
        IF :found = 0 THEN 
            INSERT INTO books VALUES (:v_isbn, 'In-Memory Data Management', 1, 1, '2011', 42.75, 'EUR');    
        END IF; 
    END WHILE; 
END;

call upsert_proc('''978-3-642-19362-0'''); 
SELECT * FROM books; 
SELECT message FROM message_box;

-------------------------------------------------------------

drop procedure for_proc; 
CREATE PROCEDURE for_proc LANGUAGE SQLSCRIPT AS 
    v_index1 INT; 
    v_index2 DECIMAL(5,2); 
    v_msg    VARCHAR(200); 
BEGIN 
    init_proc();

    FOR v_index1 IN -2 .. 2 DO 
        FOR v_index2 IN REVERSE 0.5 .. 5.5 DO 
            v_msg := 'Here is '|| :v_index1 || '-' || :v_index2 || '.'; 
            ins_msg_proc(:v_msg); 
        END FOR; 
    END FOR; 
END;

CALL for_proc(); 
SELECT message FROM message_box;

专注于企业信息化,最近对股票数据分析较为感兴趣,可免费分享股票个股主力资金实时变化趋势分析工具,股票交流QQ群:457394862
分类: SAP HANA

本文转自沧海-重庆博客园博客,原文链接:http://www.cnblogs.com/omygod/archive/2013/04/25/3043781.html,如需转载请自行联系原作者
上一篇:Windows 8实现所有微软网站免登录


下一篇:区块链每日一问 | 区块链的主链和侧链有什么关系?最长的区块链就是正确的吗?