cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to Fetch Distinct Values using For loop and Cursor in SAP HANA.

Vamshi9
Discoverer
0 Kudos

I am trying to fetch Distinct values into the For loop in SAP HANA using CURSOR and For Loop and I am not getting all the distinct values into the For Loop. Instead of getting 10 distinct names, I am getting only 5 names in my output. Could you please guide me with the correct SQL?

CREATE PROCEDURE DBADMIN.FetchDistinctStrings()
LANGUAGE SQLSCRIPT
AS
BEGIN
        DECLARE STRING1 VARCHAR(255);
        DECLARE CURSOR MyTenIDFetch (STRING1 VARCHAR(255)) FOR
        SELECT DISTINCT EMPLOYEE_NAMES FROM "H_CPO_MAIN"."TABLE_NURO";
        OPEN MyTenIDFetch(:STRING1);
        FETCH MyTenIDFetch INTO STRING1;
        FOR CUR_ROW AS MyTenIDFetch(STRING1) DO
        SELECT STRING1 as STRING_NAMES FROM DUMMY;
        END FOR;
    CLOSE MyTenIDFetch;
END;

 

JimSpath
Active Contributor
0 Kudos

It looks like you posted the same question twice. Could you delete the redundant post?

For the SQL, what results do you get when (if you can) you execute the statement directly against the database? In other words, how certain are you that the answer must be 10?

Vamshi9
Discoverer
0 Kudos

@JimSpath 
When I run the below SQL directly on the Database table, I get 10 distinct results. But when fetched thru the For Loop am not getting those 10 records. Instead I am getting only 5 of them. My loop logic is incorrect somewhere. 

SELECT DISTINCT EMPLOYEE_NAMES FROM "H_CPO_MAIN"."TABLE_NURO";

 

JimSpath
Active Contributor
0 Kudos

Interesting.

Can you do a SQL trace and look for something like this?

FETCH ... LIMIT 5

Is there any configuration for data cache buffers such that the next set is returned by a subsequent FETCH? Packet size, or something.

 

Accepted Solutions (0)

Answers (0)