Khrys
Khrys

Reputation: 2774

Loop through SQL results and do a IF, ELSE

I have a select, like:

SELECT item, group, paid FROM table WHERE group=120

The result will be:

item    group    paid
A       120      1
B       120      1
C       120      1
D       120      0

I need to loop through the result and check if all the itens have paid = 1 do something, instead, do another thing. Thanks.

This is what I have:

<%      
    SET Lista= MSSQL.Execute("SELECT item, group, paid FROM table WHERE group=120")
        Do While Not Lista.EOF
        paid= Lista("paid")
            IF paid= 1 THEN
                Response.Write "1"
            ELSE
                Response.Write "0"      
            END IF
        Loop
%>

Upvotes: 1

Views: 1940

Answers (2)

VMV
VMV

Reputation: 576

Try this:

SET Lista= MSSQL.Execute("SELECT item, group, paid FROM table WHERE group=120")
Lista.Filter = "paid = 0"
If (Lista.Eof) Then
Response.write("0")
Else
Response.Write("1")
End If

Upvotes: 1

StephenCollins
StephenCollins

Reputation: 805

How about this slight modification...

 <%      
        SET Lista= MSSQL.Execute("SELECT item, group, paid FROM table WHERE group=120")
            While Not Lista.EOF
            paid = Lista("paid")
                IF paid= 1 THEN
                    Response.Write "1"
                ELSE
                    Response.Write "0"      
                END IF
           Lista.MoveNext
           Wend
    %>

Upvotes: 1

Related Questions