Professional Web Applications Themes

factoring out a query result to reuse it in a loop - Microsoft SQL / MS SQL Server

Zeng, > I'm looking a way to factor out a query result to reuse it in a > loop, something like this > > set myresult = select * from mytable > > for loop > begin > if x is in myresult > do something > end You can insert the results into a temp table or table variable and work with that in the loop. Linda...

  1. #1

    Default Re: factoring out a query result to reuse it in a loop

    Zeng,
    > I'm looking a way to factor out a query result to reuse it in a
    > loop, something like this
    >
    > set myresult = select * from mytable
    >
    > for loop
    > begin
    > if x is in myresult
    > do something
    > end
    You can insert the results into a temp table or table variable and
    work with that in the loop.

    Linda

    lindawie Guest

  2. #2

    Default Re: factoring out a query result to reuse it in a loop


    Would you be able to give me a syntax or sample? thanks

    "lindawie" <lindawiemy-deja.com> wrote in message
    news:#B4uJe1PDHA.2036TK2MSFTNGP10.phx.gbl...
    > Zeng,
    >
    > > I'm looking a way to factor out a query result to reuse it in a
    > > loop, something like this
    > >
    > > set myresult = select * from mytable
    > >
    > > for loop
    > > begin
    > > if x is in myresult
    > > do something
    > > end
    >
    > You can insert the results into a temp table or table variable and
    > work with that in the loop.
    >
    > Linda
    >

    Zeng Guest

  3. #3

    Default Re: factoring out a query result to reuse it in a loop

    Zeng,
    > Would you be able to give me a syntax or sample? thanks
    >
    > "lindawie" <lindawiemy-deja.com> wrote in message
    > news:#B4uJe1PDHA.2036TK2MSFTNGP10.phx.gbl...
    >> Zeng,
    >>
    >>> I'm looking a way to factor out a query result to reuse it in a
    >>> loop, something like this
    >>>

    declare i int
    declare results table (
    ProductID int,
    ProductName varchar(40)
    )
    insert results
    select ProductID, ProductName from Northwind..Products
    set i = 1
    while i <= (select max(ProductID) from results) begin
    select Productname from results where ProductID = i
    set i = i + 1
    end

    Linda

    lindawie Guest

Similar Threads

  1. Loop a Result Set Within a StoredProc
    By Warden in forum Coldfusion - Advanced Techniques
    Replies: 1
    Last Post: June 30th, 10:54 PM
  2. displaying query result of 0 as N/A
    By eggwater in forum Macromedia ColdFusion
    Replies: 3
    Last Post: May 10th, 11:34 AM
  3. loop count down until result is achieved
    By AirJar in forum Macromedia ColdFusion
    Replies: 2
    Last Post: March 21st, 01:54 PM
  4. Replies: 4
    Last Post: September 18th, 08:18 PM
  5. how to add another column to a query result?
    By Junaili Lie in forum Microsoft SQL / MS SQL Server
    Replies: 4
    Last Post: July 7th, 06:03 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139