• HANA SQL Script学习(4):Declarative SQLScript Logic


    4. Declarative SQLScript Logic

    4.1 Table Parameter

    /*

    3.Declarative SQLScript Logic

    3.1Table Parameter

    <table_param> ::= [IN|OUT] <param_name> {<table_type>|<table_type_definition>| <any_table_type>}

    <table_type> ::= <identifier>

    <table_type_definition> ::= TABLE(<column_list_elements>)

    <any_table_type> ::= TABLE(...)

    */

    --Table Parameter
    --使用<table_type_definaition>
    create procedure test_proc6( IN inputtab TABLE(I INT),OUT outputtab TABLE (I INT,J DOUBLE))
    as begin
        outputtab = select i,i*1.2 as j from :inputtab;
    end
    
    --代码块
    do begin
        declare outtab table(I int,J double);
        declare intab table(I int);
        intab = select I from mytab4;
        call test_proc6(:intab,outputtab=>outtab);
        select * from :outtab;
    end
    
    --使用<table_type>
    create type tt_inputtab as table(I int);
    create type tt_outputtab as table(I int,j double);
    create procedure test_proc7(IN inputtab tt_inputtab, OUT outputtab tt_outputtab)
    as begin
        outputtab = select i,i*1.5 as j from :inputtab;
    end
    
    --代码块
    do begin
        declare outtab table(I int,J double);
        declare intab table(I int);
        intab = select I from mytab4;
        call test_proc7(:intab,outputtab=>outtab);
        select * from :outtab;
    end
    
    --使用<any_table_type>
    /*
    使用限制:
    1.the any_table_type parameter cannot be used within anonymous blocks, other languages or outside the scope of SQLScript
    2.any_table_type parameters are supported only as input parameter of table UDFs, but not as return parameters
    3.scalar UDFs do not support any_table_type parameters.
    4.If an output any table type parameter cannot be resolved during procedure creation
    (for example, out_any_table = select * from in_any_table), the procedure cannot be called inside SQLScript.
    */
    create function func_test1
    as begin
        --在function中创建procedure
        create procedure test_proc8(in ott table(...))
        as begin   
         ott = select * from mytab4;
        end;
    end
    --创建不了?
    create procedure test_proc9( IN inputtab table(...),OUT outputtab table(...) )
    as begin
        outputtab = select * from :inputtab;
    end
    
    --代码块
    do begin
        declare intab table(I int,J double);
        intab = select I,I*1.3 AS J from mytab4;
        call test_proc8(:intab,outputtab=>outtab);
        select * from :outtab;
    end

    4.2 Table Variable Type Definition

    /*

    3.2Table Variable Type Definition

    DECLARE <sql_identifier> [{,<sql_identifier> }...] [CONSTANT] {TABLE (<column_list_definition>)|<table_type>} [ <proc_table_default> ]

    <proc_table_default> ::= { DEFAULT | '=' } { <select_statement> | <proc_ce_call> | <proc_apply_filter> | <unnest_function> }

    */

    drop procedure test_proc10;
    --<table_type>,之前已经创建tt_inputtab
    --create type tt_inputtab as table(I int);
    create procedure test_proc10()
    as begin
        --table(<column_list>)
        declare t_test1 table(n int);
        --<table_type>
        declare t_test2 tt_inputtab;
        --default,=,设置默认值
        declare t_test3 tt_inputtab default select I from mytab4;
        declare t_test4 tt_inputtab = select I from mytab4;
        t_test1 = select I as n from mytab4;
        t_test2 = select I from mytab4;
        select * from :t_test1;
        select * from :t_test2;
        select * from :t_test3;
        select * from :t_test4;
    end
    call test_proc10();
    
    --使用constant定义常量table类型,必须指定默认值
    --创建出错
    DECLARE t_test5 CONSTANT TABLE(I INT) DEFAULT SELECT * FROM mytab4;
    --作用域,定义table变量的作用域
    drop procedure test_proc11;
    CREATE PROCEDURE test_proc11 (OUT outTab TABLE(n int))
     LANGUAGE SQLScript READS SQL DATA
    AS BEGIN   
        --Binding Table Variables
        temp = SELECT 1 as n FROM DUMMY ;       
        BEGIN      
         temp = SELECT 2 as n FROM DUMMY ;           
         outTab = Select * from :temp;   
        END;  
    
        --Referencing Variables,使用<:var>
        outTab = Select * from :temp;
    END;
    --输出2
    call test_proc11(?);
    CREATE PROCEDURE test_proc12(OUT outTab TABLE(n int)) LANGUAGE SQLScript READS SQL DATA AS BEGIN DECLARE temp TABLE (n int); temp = SELECT 1 as n FROM DUMMY ; BEGIN --定义temp只在block中有效 DECLARE temp TABLE (n int); temp = SELECT 2 as n FROM DUMMY ; outTab = Select * from :temp; END; outTab = Select * from :temp; END; --输出1 call test_proc12(?);

    4.3 Column View Parameter Binding

    /*

    3.3 Column View Parameter Binding

    SELECT * FROM <column_view> ( <named_parameter_list> );

    <column_view> ::= <identifier>

    <named_parameter_list> ::= <named_parameter> [{,<named_parameter>}…}]

    <named_parameter> ::= <parameter_name> => <expression>

    <parameter_name> ::= {PLACEHOLDER.<identifier> | HINT.<identifier> | <identifier>}

    */

    --创建column view??
    CREATE PROCEDURE my_proc_caller (IN in_client INT, IN in_currency INT, OUT outtab mytab_t)
        LANGUAGE SQLSCRIPT READS SQL DATA AS
    BEGIN   
        outtab = SELECT * FROM CALC_VIEW(PLACEHOLDER."$$client$$" => :in_client , PLACEHOLDER."$$currency$$" => :in_currency );
    END;

    4.4 Map Merge Operator

    /*

    3.4 Map Merge Operator

    <table_variable> = MAP_MERGE(<table_or_table_variable>, <mapper_identifier>

        (<table_or_table_variable>.<column_name> [ {,<table_or_table_variable>.<column_name>} … ] [, <param_list>])

    <param_list> ::= <param> [{, <param>} …]

    <paramter> = <table_or_table_variable> | <string_literal> | <numeric_literal> | <identifier>

    */

    drop function test_mapper;
    --mapperfunction
    CREATE FUNCTION test_mapper (IN a INT)
     RETURNS TABLE (COL_A INT)
    AS BEGIN   
     ot = SELECT :a AS COL_A from dummy;   
     RETURN :ot;
    END;
    
    --使用for循环
    --执行报错?
    DO (OUT ret_tab TABLE(col_a INT)=>?)
    BEGIN     
        DECLARE i int;     
        DECLARE varb int;    
        t = SELECT * FROM mytab4;     
        FOR i IN 1 .. 10 DO     
             varb = :t.i[:i];
            CALL test_mapper(:varb,out_tab);           
            ret_tab = SELECT * FROM :out_tab           
            UNION SELECT * FROM :ret_tab;     
        END FOR;
    END;
    
    --使用map merge
    --?invalid name of map_merge,不支持
    DO (OUT ret_tab TABLE(col_a INT)=>?)
    BEGIN  
        t = SELECT * FROM mytab4;   
        ret_tab = MAP_MERGE(:t, test_mapper(:t.col_a));
    END;

    4.5 Map Reduce Operator

    /*

    3.5 Map Reduce Operator

    MAP_REDUCE(<input table/table variable name>, <mapper specification>, <reducer specification>)

    <mapper spec> ::= <mapper TUDF>(<list of mapper parameters>) group by <list of columns in the TUDF> as <ID>

    <reducer spec> ::= <reduce TUDF>(<list of reducer TUDF parameters>) | <reduce procedure>(<list of reducer procedure parameters>)

    <mapper parameter> ::= <table/table variable name>.<column name> | <other scalar parameter>

    <reducer TUDF parameter> ::= <ID> | <ID>.<key column name> | <other scalar parameter>

    <reducer procedure parameter> ::= <reducer TUDF parameter> | <output table parameter>

    MAP_REDUCE is a programming model introduced by Google that allows easy development of scalable parallel applications

    for processing big data on large clusters of commodity machines.

    */

    4.6 Hints

    /*

    3.6 Hints

    1.NO_INLINE and INLINE Hints

    The SQLScript compiler combines statements to optimize code.

    Hints enable you to block or enforce the inlining of table variables.

    2.ROUTE_TO Hint

    <servicetype> ::= 'indexserver' | 'xsengine' | 'scriptserver' | 'dpserver' | 'computeserver'

    <hint_with_parameters> ::= ROUTE_TO( <volume_id> [{, <volume_id> }] )                          

        | ROUTE_TO( '<servicetype>' [{, '<servicetype>' }] )                          

        | NO_ROUTE_TO( <volume_id> [{, <volume_id> }] )                          

        | NO_ROUTE_TO( '<servicetype>' [{, '<servicetype>' }] )                          

        | ROUTE_BY( <table_name> [{, <table_name>}] )                           

        | ROUTE_BY_CARDINALITY( <table_name> [{, <table_name>}] )

        | DATA_TRANSFER_COST ({0 | 1})

    */

    --SQL 语句
    tab   = select A, B, C from T where A = 1;
    tab2  = select C from :tab where  C = 0;
    
    --执行时,组合成下面语句:
    select C from (select A,B,C from T where A = 1) where C=0;
    
    --使用HINT(NO_INLINE),阻止表变量内联,
    tab = select A, B, C from T where A = 1 WITH HINT(NO_INLINE);
    tab2  = select C from :tab where  C = 0;
    
    --创建procInner,procCaller
    --调用procCaller时,两个表独立执行
    CREATE PROCEDURE procInner (OUT tab2 TABLE(I int))
     LANGUAGE SQLSCRIPT READS SQL DATA
    AS BEGIN    
        tab2 = SELECT I FROM mytab4;
    END;
    CREATE PROCEDURE procCaller (OUT table2 TABLE(I int)) LANGUAGE SQLSCRIPT READS SQL DATA AS BEGIN call procInner (outTable); table2 = select I from :outTable where I > 10; END; --procInner定义为HINT(INLINE) --调用时两个表连接执行 drop procedure procInner; CREATE PROCEDURE procInner (OUT tab2 TABLE(I int)) LANGUAGE SQLSCRIPT READS SQL DATA AS BEGIN tab2 = SELECT I FROM mytab4 WITH HINT (INLINE); END; --procInner参数tab2链接procCaller中outtable中执行 SELECT I FROM (SELECT I FROM T WITH HINT (INLINE)) where I > 10; --invalid hint: incorrect syntax near "indexserver" select * from mytab4 with hint(ROUTE_TO('indexserver')); select * from mytab4 with hint(ROUTE_TO('indexserver','computeserver')); select * from mytab4 with hint(NO_ROUTE_TO('indexserver')); select * from mytab4 with hint(NO_ROUTE_TO('indexserver','computeserver')); --error:insufficient privilege: Not authorized select preferred_routing_volumes, * from sys.m_sql_plan_cache_ where statement_string like '%select * from mytab%';
  • 相关阅读:
    静态构造函数
    js-----Date对象
    JS中Math函数的常用方法
    js 简单数据类型和复杂数据类型的区别
    js new关键字 和 this详解
    JS 对象的三种创建方式
    js 预解析
    JS作用域问题
    js实现斐波那契数列
    JS 三元表达式
  • 原文地址:https://www.cnblogs.com/tangToms/p/14124749.html
Copyright © 2020-2023  润新知