Simple question I hope. Using 12, 16 and 17 can I leave a for loop in a procedure

FOR LoopName AS CursorName INSENSITIVE CURSOR FOR
  SELECT STATEMENT
DO
  IF Condition Exists THEN
   LEAVE LoopName;
  END IF;
END FOR;

asked 28 Jan, 15:21

J%20Diaz's gravatar image

J Diaz
889263146
accept rate: 13%

edited 28 Jan, 16:14

Breck%20Carter's gravatar image

Breck Carter
28.7k480641935


The "beginning statement-label" in Volker's reply is not the "FOR LoopName" but a separate label in front of the FOR.

It can be the same text name, however... there's no collision:

BEGIN
LoopName:
FOR LoopName AS CursorName INSENSITIVE CURSOR FOR
  SELECT row_num AS @row_num
    FROM rowgenerator
   ORDER BY row_num
DO
  IF @row_num > 2 THEN
   LEAVE LoopName;
  END IF;
  MESSAGE STRING ( '@row_num = ', @row_num ) TO CONSOLE;
END FOR;
MESSAGE STRING ( 'done' ) TO CONSOLE;
END;

@row_num = 1
@row_num = 2
done
permanent link

answered 28 Jan, 16:23

Breck%20Carter's gravatar image

Breck Carter
28.7k480641935
accept rate: 20%

edited 28 Jan, 16:24

From the FOR statement doc topic:

The LEAVE statement can be used to resume execution at the first statement after the END FOR. If the ending statement-label is specified, it must match the beginning statement-label.

Here's a variant of Breck's sample with LEAVE without a statement-lable:

BEGIN
FOR LoopName AS CursorName INSENSITIVE CURSOR FOR
  SELECT row_num AS @row_num
    FROM rowgenerator
   ORDER BY row_num
DO
  IF @row_num > 2 THEN
   LEAVE;
  END IF;
  MESSAGE STRING ( '@row_num = ', @row_num ) TO CONSOLE;
END FOR;
MESSAGE STRING ( 'done leaving without a statement name' ) TO CONSOLE;
END;

displays

@row_num = 1
@row_num = 2
done leaving without a statement name
permanent link

answered 28 Jan, 15:57

Volker%20Barth's gravatar image

Volker Barth
33.4k329482710
accept rate: 32%

edited 29 Jan, 03:32

Breck/Vollker -

Thanks very much. I'm working on an interesting application in which we are using SQL Anywhere to interface with an IIoT appliance running Linux and MySQL. The appliance interfaces with an OPC UA server stores the data in the local MySQL database for eventual retrieval by SQL Anywhere using the integral HTTP Get statements which is fired via a database Event.

This loop enumerates the data available and must validate several variables before saving to both a local SQL Anywhere table as well as outputting in a compressed format to a SQL Anywhere directory server.

It's amazing how easy SQL Anywhere flows this to happen with all built in functionality.

Thanks Again

Jim

PS See my next question on directory server free space

(29 Jan, 07:16) J Diaz
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:

×30

question asked: 28 Jan, 15:21

question was seen: 127 times

last updated: 29 Jan, 07:16