Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2003
    Posts
    35

    Question Unanswered: ?Queries in While Condition

    When using a query in the condition of a while loop, is that query being performed each time?

    EX:
    Code:
    While (@X <= (Select count(*) from SOME_TABLE))
    "My Idea is that everyone should be required to use small fonts. That way we'll save disk space.
    ~Asok.

  2. #2
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    I was going to say no....but...

    Code:
    USE Northwind
    GO
    
    SET NOCOUNT ON
    CREATE TABLE myTable99(Col1 int IDENTITY(1,1), Col2 char(1))
    GO
    
    INSERT INTO myTable99(Col2)
    SELECT 'A' UNION ALL
    SELECT 'B' UNION ALL
    SELECT 'C' UNION ALL
    SELECT 'D' UNION ALL
    SELECT 'E'
    GO
    
    
    DECLARE @x int
    SELECT @x = 1
    WHILE @x < (SELECT COUNT(*) FROM myTable99)
      BEGIN
    	SELECT @x = @x + 1
      END
    SELECT @x
    
    SELECT @x = 1
    WHILE @x < (SELECT COUNT(*) FROM myTable99)
      BEGIN
    	IF @x IN (1,3,5) INSERT INTO myTable99(Col2) SELECT 'x'
    	SELECT @x = @x + 1
      END
    SELECT @x
    GO
    
    SET NOCOUNT OFF
    DROP TABLE myTable99
    GO
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

Posting Permissions

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