Quantcast
Channel: SCN: Message List - SAP HANA Developer Center
Viewing all articles
Browse latest Browse all 9165

Re: Obtaining number of rows after select statement in SQL Script

$
0
0

Hi Akshay,

 

you have two options here: you can have the count as a scalar value, or as an internal table. Depends on how you want to continue using this.

 

Both options can be derived from the information given in the SQL Script reference guide availabe at http://help.sap.com/hana_appliance in section Development Information.

 

I give you some examples below. The version to select into a table variable is rather trivial. Getting the count into a scalar variable requires you to write the statement in the form:

select count(*)  into <scalar_variable> from :<table_variable> where <condition>;

 

Best regards,

Richard

 


/* create test table with two entries */

create

columntable TEST_TABLE

( f1 varchar(3),

  f2 integer,

  primarykey (f1)

);


insertinto TEST_TABLE (f1, f2) values ('AAA', 1);

insertinto TEST_TABLE (f1, f2) values ('BBB', 2);

/* first example: return the count as a table type variable */

createtype tt_count astable (f_count int);

createprocedure test_count_lt (out var_out tt_count) LANGUAGE SQLSCRIPT AS

begin

  lt_result = select * from"TEST_TABLE";

  var_out = selectcount(*) as f_count from :lt_result;

END;


/* call the procedure with output into a local temporary table */

createlocaltemporarytable #st_count (f_count int);

call test_count_lt (#st_count) WITH OVERVIEW;

select * from #st_count;

/* --> returns 2 */

 

/* 2nd alternative: count into a scalar variable */

createprocedure test_count_sv (out var_out int) LANGUAGE SQLSCRIPT AS

  lv_count int := 0;

begin

  lt_result = select * from"TEST_TABLE";

  selectcount(*) as f_count into lv_count from :lt_result;

  var_out := lv_count;

END;

call test_count_sv ( ? );

/* --> returns 2 */


/* clean up */

droptable TEST_TABLE;

droptype tt_count;

droptable #st_count;

dropprocedure test_count_lt;

dropprocedure test_count_sv;

 


Viewing all articles
Browse latest Browse all 9165

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>