Monday, March 26, 2012

Loop in SQL

Hi...

I need to create a SELECT with a variable in WHERE...

Example:

SELECT * FROM ANSWERS WHERE id = 1

Quote:

Originally Posted by

Quote:

Originally Posted by

Quote:

Originally Posted by

>>then return the results and execute


SELECT * FROM ANSWERS WHERE id = 2

... ... ...

It's possible?

Thanks and sorry my english.

Grillo"Guilherme Grillo" <webdev@.rncomtotal.com.brwrote in message
news:u%23wJB%23iIIHA.748@.TK2MSFTNGP04.phx.gbl...

Quote:

Originally Posted by

I need to create a SELECT with a variable in WHERE...
>
Example:
>
SELECT * FROM ANSWERS WHERE id = 1
>

Quote:

Originally Posted by

Quote:

Originally Posted by

>>>then return the results and execute


>
SELECT * FROM ANSWERS WHERE id = 2


Just do both SELECTs at the same time:

SELECT * FROM ANSWERS WHERE id = 1
SELECT * FROM ANSWERS WHERE id = 2

If you're populating a DataSet, it'll have two tables...

If you're populating an SqlDataReader, it'll have two Resultsets - use
MyReader.NextResultset()
http://www.dotnetjohn.com/articles.aspx?articleid=23
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
If your combined result sets returns too much data then do them separately.

"Mark Rae [MVP]" <mark@.markNOSPAMrae.netwrote in message
news:uTXyiDjIIHA.484@.TK2MSFTNGP06.phx.gbl...

Quote:

Originally Posted by

"Guilherme Grillo" <webdev@.rncomtotal.com.brwrote in message
news:u%23wJB%23iIIHA.748@.TK2MSFTNGP04.phx.gbl...
>

Quote:

Originally Posted by

>I need to create a SELECT with a variable in WHERE...
>>
>Example:
>>
>SELECT * FROM ANSWERS WHERE id = 1
>>

Quote:

Originally Posted by

>>then return the results and execute


>>
>SELECT * FROM ANSWERS WHERE id = 2


>
Just do both SELECTs at the same time:
>
SELECT * FROM ANSWERS WHERE id = 1
SELECT * FROM ANSWERS WHERE id = 2
>
If you're populating a DataSet, it'll have two tables...
>
If you're populating an SqlDataReader, it'll have two Resultsets - use
MyReader.NextResultset()
http://www.dotnetjohn.com/articles.aspx?articleid=23
>
>
--
Mark Rae
ASP.NET MVP
http://www.markrae.net


declare @.someId int
set @.someId = 1
select * from categories
where categoryid = @.someId

hope it can help :)

Guilherme Grillo wrote:

Quote:

Originally Posted by

Hi...
>
I need to create a SELECT with a variable in WHERE...
>
Example:
>
SELECT * FROM ANSWERS WHERE id = 1
>

Quote:

Originally Posted by

Quote:

Originally Posted by

>then return the results and execute


>
SELECT * FROM ANSWERS WHERE id = 2
>
... ... ...
>
It's possible?
>
Thanks and sorry my english.
>
Grillo


Thanks for all.

But my problem is a little different.

Example:

The system SELECT COUNT the table "QUESTIONS" on DB

commCountQuestions = new SqlCommand("SELECT COUNT(*) AS total FROM
Questions", conn);

Then,

int t = readerPerguntas.GetOrdinal("total");

Then I do a SELECT to Select the Questions in table Questions and Select the
ANSWERS in the table ANSWERS.

for (i=1, i<16, i++)
{
sqlcommQUESTION = "SELECT * FROM Questions WHERE id=" + i + "";
sqlcomm = "SELECT * FROM Answers WHERE id =" + i + "";
}

The problem is: I can't do two readers READ at the same time. I don't know
how to do a JOIN that works.

INFO: One Question have 2 questions or more...

The results should be:

1. What's the color of the sea?

[checkbox] Blue
[checkbox] Red
[checkbox] Yellow
[checkbox] Green

2. Do you like cookies?

[checkbox] Yes
[checkbox] No

3. ... ... ..
[checkbox] ...

I don't know how to solve this problem!!!!

Sorry my english...

Grillo

<JinWeiJie@.gmail.comwrote in message
news:1194578131.074394.231790@.v23g2000prn.googlegr oups.com...

Quote:

Originally Posted by

declare @.someId int
set @.someId = 1
select * from categories
where categoryid = @.someId
>
hope it can help :)
>
>
Guilherme Grillo wrote:

Quote:

Originally Posted by

>Hi...
>>
>I need to create a SELECT with a variable in WHERE...
>>
>Example:
>>
>SELECT * FROM ANSWERS WHERE id = 1
>>

Quote:

Originally Posted by

>>then return the results and execute


>>
>SELECT * FROM ANSWERS WHERE id = 2
>>
>... ... ...
>>
>It's possible?
>>
>Thanks and sorry my english.
>>
>Grillo


>

0 comments:

Post a Comment