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;

asked 28 Feb '18, 16:58

gchq's gravatar image

gchq
421263140
accept rate: 27%


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;

permanent link

answered 28 Feb '18, 18:15

Evgeniya's gravatar image

Evgeniya
6615
accept rate: 50%

Compound statement?

(28 Feb '18, 18:17) gchq

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.

(28 Feb '18, 18:19) John Smirnios

Using CREATE VARIABLE throws - Syntax error near 'SET'

(28 Feb '18, 18:20) gchq

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

(28 Feb '18, 18:23) gchq
Replies hidden

We have an isolated existence here :-) Will try to remember that.

(02 Mar '18, 18:48) gchq

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

(03 Mar '18, 10:48) Vlad
showing 5 of 7 show all flat view
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×260

question asked: 28 Feb '18, 16:58

question was seen: 1,540 times

last updated: 03 Mar '18, 10:48