Skip to Content

How to assign a variable to the result of a query in HANA?

Nov 02, 2016 at 11:24 AM


avatar image

Hello, all

How does one assign the result of a query to a scalar variable in HANA? In MS SQL I could:

SET @myvar = ( SELECT CardName FROM OCRD WHERE CardCode = @cc )

or, for several variables:

SELECT @var1 = Field1, @var2 = Field2 FROM MYTABLE WHERE Code = @code

The only way to do itin HANA that I have found is via a very long compass, i.e. by dint of a cursor. But cursors being intended for the iteration over several data rows, they are very clumsy in such simple an operation as a single assignment of a single scalar variable. I should say it is actually an abuse of cursor, so there ought to be a better way. Is there one?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
Anton Shepelev Jul 02 at 12:23 PM

Here is an example, ready for testing in Hana Studio:

    DECLARE V1, v2 vARCHAR(8);
    SELECT 'Hello', 'there' INTO V1, V2 FROM DUMMY;
Show 2 Share
10 |10000 characters needed characters left characters exceeded

This is one of the many cases where just flipping through the documentation would’ve been the quicker way to get the answer.


Yes. But why the downvote? I saw my old unanswered question and answered it.