Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2013
    Posts
    1

    Unanswered: While loop outside of procedure?

    Hello everyone,

    Can we have a While loop or any loops in a SELECT statement (NOT within a stored procedure)?

    I just ry to run a query with a loop but keep getting error. Sample query is below. Thanks in advance for your help.

    --DECLARE GLOBAL TEMPORARY TABLE SESSION.TEST(ID INT);

    CREATE OR REPLACE VARIABLE PSGSCRATCH.ID INTEGER;
    SET PSGSCRATCH.ID = 9;

    While (PSGSCRATCH.ID > 1) DO
    INSERT INTO SESSION.TEST
    SELECT lodg_property_name
    FROM DM.LODG_PROPERTY_DIM
    WHERE expe_lodg_property_id = PSGSCRATCH.ExpediaID;
    SET PSGSCRATCH.ID = PSGSCRATCH.ID -1
    End while;

    And this is the error:

    Category Timestamp Duration Message Line Position
    Error 2/14/2013 6:48:54 AM 0:00:00.192 DB2 Database Error: ERROR [42601] [IBM][DB2/LINUXX8664] SQL0104N An unexpected token "(" was found following "While ". Expected tokens may include: "JOIN". SQLSTATE=42601
    6 0

  2. #2
    Join Date
    Apr 2006
    Location
    Belgium
    Posts
    2,514
    Provided Answers: 11
    Best Regards, Guy Przytula
    Database Software Consultant
    Good DBAs are not formed in a week or a month. They are created little by little, day by day. Protracted and patient effort is needed to develop good DBAs.
    Spoon feeding : To treat (another) in a way that discourages independent thought or action, as by overindulgence.
    DB2 UDB LUW Certified V7-V8-V9-V9.7-V10.1-V10.5 DB Admin - Advanced DBA -Dprop..
    Information Server Datastage Certified
    http://www.infocura.be

  3. #3
    Join Date
    Apr 2008
    Location
    Iasi, Romania
    Posts
    561
    Provided Answers: 2
    It looks like a compound statement, so you need to enclose it with BEGIN - END. Also, instead of CREATE VARIABLE use DECLARE.
    Florin Aparaschivei
    DB2 9.7, 10.5 on Windows
    Iasi, Romania

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •