Skip to Content

Declaring variables in HANA

Dear Experts,

I want to convert my below SQL query to HANA.

Declare @code varchar(20)

set @code = 'C20000'

Select * From OINV T0 Where T0.CardCode = @code

Can anyone please help me for the right statement. I want to use variable in HANA Query and don't want to create procedure.

1-

code varchar(20);

code := 'C20000';

SELECT * FROM "OINV" WHERE "CardCode" = :code;

2-

Declare code varchar(20);

code := 'C20000';

SELECT * FROM "OINV" WHERE "CardCode" = :code;

Error

* Could not execute 'code varchar(20)'

SAP DBTech JDBC: [257]: sql syntax error: incorrect syntax near "code": line 1 col 1 (at pos 1)

* Could not execute 'code := 'C20000''

SAP DBTech JDBC: [257]: sql syntax error: incorrect syntax near "code": line 1 col 1 (at pos 1)

* Could not execute 'SELECT * FROM "OINV" WHERE "CardCode" = :code'

SAP DBTech JDBC: [467]: cannot use parameter variable: CODE: line 1 col 42 (at pos 41)

Add comment
10|10000 characters needed characters exceeded

1 Answer

  • Best Answer
    Posted on Sep 26, 2016 at 01:49 PM

    You can try the following

    --Global Session Variables

    SET 'MY_VAR' = 'C20000';

    SELECT * FROM "OINV" WHERE "CardCode" = (SELECT SESSION_CONTEXT('MY_VAR') FROM DUMMY);

    UNSET 'MY_VAR'

    DO

    BEGIN

    DECLARE code varchar(20);

    code := 'C20000';

    SELECT * FROM "OINV" WHERE "CardCode" = :code;

    END;

    Regards,

    Rafaee M

    Add comment
    10|10000 characters needed characters exceeded