cancel
Showing results for 
Search instead for 
Did you mean: 

LOOP - variable not found

0 Kudos
2,421

Trying to run a loop statement, but it fails at the first line with - Variable 'i' not found. Do I need to define this first somehow? Thanks

SET i = 203;
WHILE i <= 264 LOOP
SELECT Cust_Name AS RC_Name FROM Customers WHERE Customer_ID = i ;

--- INSERT STATEMENTS ---

SET i = i + 1;
END LOOP;

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

Hi, you need to declare or create it first:

CREATE VARIABLE i INTEGER;

Alternatively, you will need to declare it, but then you will need to use compound statement:

BEGIN

DECLARE i INTEGER;

--YOUR CODE--

END;

0 Kudos

Compound statement?

johnsmirnios
Advisor
Advisor
0 Kudos

You probably don't want that first one -- that creates a connection-scope variable that persists until your connection is dropped or you explicitly drop the variable.

0 Kudos

Using CREATE VARIABLE throws - Syntax error near 'SET'

0 Kudos

Using BEGIN and END; (Compound Statement??) works - thank you

0 Kudos

We have an isolated existence here 🙂 Will try to remember that.

Vlad
Product and Topic Expert
Product and Topic Expert
0 Kudos

Oh! Now I know the new term! Thank you Breck!

Answers (0)