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 a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

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 a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.