Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2009
    Posts
    47

    Question Unanswered: While loop with dynamic sql

    Hi could you please help me with the following...
    The syntax for while loop is

    While Exists(select * from Customers)
    End While

    How can I use the above select statment using dynamic SQL?

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Code:
    EXEC('While Exists(select * from Customers) BEGIN
    END')
    Your code isn't valid T-SQL for starters.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    Mar 2009
    Posts
    349
    cuz itz homework and it is finals time.

  4. #4
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Quote Originally Posted by Thrasy
    cuz itz homework and it is finals time.
    Obviously you haven't read this guy's previous posts. It is 100% NOT homework. He'll be asking how to get values returned from a cursor looping through tables defined by a variable within that loop next.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  5. #5
    Join Date
    Mar 2009
    Posts
    47
    The above posted code Gives me error which I m not getting why

    My code

    BEGIN
    DECLARE @ACCID AS CHAR(12)
    DECLARE @DBNAME AS NVARCHAR(20)
    EXEC('WHILE EXISTS (SELECT * FROM '+@DBNAME+'..ACCOUNTMASTER) BEGIN
    SET @CUSTID=(SELECT CUSTID FROM ACCOUNTMASTER)
    PRINT(@CUSTID)
    END')

    THE ABOVE CODE GIVES ME ERROR THT @CUSTID IS NOT DECLARED

  6. #6
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    There's so many errors it's hard to know where to start. You never read the dynamic sql link did you?

    Apart from anything else, if you must write it dynamically at least get it working without dynamic SQL first, then convert it.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  7. #7
    Join Date
    Mar 2009
    Posts
    47
    Yes I did read the dynamic sql link...Allthnks to u I came to know about SQL Injection but in the above case the DBName is not accepted by user..It's the (I mean my)prog. which takes this so I think using EXEC in the above query should not be a prob.Is there any other Problem in the above query?
    Waiting for ur reply

Posting Permissions

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