Question

In SAP HANA I wish to have a view which has a range of number from 1 to 10, or 1 to n where n is any number. So when I select from the view I can select n records to get the first n records from the range.

I was able to create a table with 1000 rows with a ID that increment's by using this stored procedure. Is there an easier way?

DROP PROCEDURE "DEMO_PROC";
CREATE PROCEDURE "DEMO_PROC"(
         IN    ID    INTEGER )
LANGUAGE SQLSCRIPT AS
/*********BEGIN PROCEDURE SCRIPT ************/
BEGIN
DECLARE
         START_ID INTEGER;
DROP TABLE TEST_TABLE;
CREATE COLUMN TABLE "TEST_TABLE" (ID INTEGER, NAME VARCHAR(10));
START_ID := 0;
WHILE START_ID < 1000 DO
  START_ID := START_ID + 1;
  INSERT INTO "TEST_TABLE" VALUES(:START_ID, '');
 END WHILE;
END;

CALL "DEMO_PROC"(1);

SELECT * FROM "TEST_TABLE";
Was it helpful?

Solution

Using a generator is the prefered way:

INSERT INTO "TEST_TABLE" SELECT GENERATED_PERIOD_START as ID, '' as NAME from SERIES_GENERATE_INTEGER(1,1,1001);

is much easier and faster.

OTHER TIPS

I think for loop is easier than while.

FOR START_ID IN 1..1000 DO
INSERT INTO "TEST_TABLE" VALUES(START_ID,'');
END FOR;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top