tencent cloud

Feedback

SQL Tag statement

Last updated: 2024-11-01 16:35:04
    The development and orchestration space of WeData supports the SQL Tag statement feature. Users can use if-else, for loops, and other logical judgments in SQL to achieve more complex business logic with simpler code.

    Special identifier at the beginning of the script

    --SQL/VMSCRIPT
    #set( $cnt = 10 )
    select $cnt;

    Assignment statements and operators

    --SQL/VMSCRIPT
    #set( $cnt = 10 )
    #set( $cnt = $cnt + 1 )
    select $cnt;

    Operator

    Operator
    Operators
    Sample code
    Object Equivalence
    ==
    #if( $foo == $bar )
    Not Equals
    !=
    #if( $foo != $bar )
    Less Than or Equal To
    <=
    #if( $foo <= 42 )
    Less Than
    <
    #if( $foo < 42 )
    Greater Than or Equal To
    >=
    #if( $foo >= 42 )
    Greater Than
    >
    #if( $foo > 42 )
    Equals String
    ==
    #if( $foo == "bar" )
    Equals Number
    ==
    #if( $foo == 42 )
    Boolean NOT
    !
    #if( !$foo )

    for-each statement

    Integer format

    --SQL/VMSCRIPT
    #set( $cnt = 10 )
    #set( $cnt = $cnt + 1 )
    #foreach($i in $_range.of(1, $cnt, 1))
    select $cnt,$i;
    #end

    List mode

    --SQL/VMSCRIPT
    #foreach($table in ["test_user_info_table", "test_issue_info_table"])
    select '$table' table_name, count(1) cnt from $table where substring(create_time, 1, 7) = '2022-11'
    #if($foreach.last != true)
    union all
    #end
    #end

    Condition judgment statement

    Using assigned variables

    --SQL/VMSCRIPT
    --This statement will execute the branch where age>2
    #set( $flag_1 = 0 )
    #set( $flag_2 = 0 )
    select * from default.demo_hive
    where
    #if($flag_1 == 1)
    id = 1;
    #elseif( $flag_2 == 1)
    id = 2;
    #else
    id >2;
    #end
    Using system parameters, system parameters must be used in the ${variable_name} format.
    Here, the variables flag_1 and flag_2 are both set to 0 in Workflow Task Scheduling.
    --SQL/VMSCRIPT
    --This statement will execute the branch where age>2
    select * from default.demo_hive
    where
    #if(${flag_1} == 1)
    age = 1;
    #elseif(${flag_2} == 1)
    age = 2;
    #else
    age >2;
    #end
    Using system variables and time variables, where dt, dt_pre_7d, and dt_pre_1d are created as time parameter variables.
    --SQL/VMSCRIPT
    --This statement will execute the else branch
    select * from default.user_info
    where
    #if(${is_reload} == "1")
    create_time <= ${dt};
    #elseif(${is_deltareload} == "1")
    create_time between ${dt_pre_7d} and ${dt};
    #else
    create_time between ${dt_pre_1d} and ${dt};
    #end

    Scope of application description

    SQL Tag can be used in the Development Space and Orchestration Space of WeData.

    Condition judgment statement

    Implementations use velocity, and relevant documentation can be referred to in Apache Velocity Engine VTL Reference.
    Note:
    The SQL Tag statements will be automatically effective for newly purchased Schedule Resource Groups. For older Schedule Resource Groups, assistance from the Product Research and Development Team is required to restart and upgrade. It is recommended to purchase new Schedule Resource Groups and then bind them to the project.
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support