Loop in SQL

  • Thread starter Thread starter Guilherme Grillo
  • Start date Start date
G

Guilherme Grillo

Hi...

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

Example:

SELECT * FROM ANSWERS WHERE id = 1

SELECT * FROM ANSWERS WHERE id = 2

.... ... ...

It's possible?

Thanks and sorry my english.

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

Example:

SELECT * FROM ANSWERS WHERE id = 1


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
 
declare @someId int
set @someId = 1
select * from categories
where categoryid = @someId

hope it can help :)
 
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
 
Back
Top