473,385 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Loop in SQL Part II

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

<Ji*******@gmail.comwrote in message
news:11**********************@v23g2000prn.googlegr oups.com...
declare @someId int
set @someId = 1
select * from categories
where categoryid = @someId

hope it can help :)
Guilherme Grillo wrote:
>Hi...

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

Example:

SELECT * FROM ANSWERS WHERE id = 1
>>then return the results and execute

SELECT * FROM ANSWERS WHERE id = 2

... ... ...

It's possible?

Thanks and sorry my english.

Grillo


Nov 9 '07 #1
5 1179

"Guilherme Grillo" <we****@rncomtotal.com.brwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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

<Ji*******@gmail.comwrote in message
news:11**********************@v23g2000prn.googlegr oups.com...
>declare @someId int
set @someId = 1
select * from categories
where categoryid = @someId

hope it can help :)
Guilherme Grillo wrote:
>>Hi...

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

Example:

SELECT * FROM ANSWERS WHERE id = 1

then return the results and execute

SELECT * FROM ANSWERS WHERE id = 2

... ... ...

It's possible?

Thanks and sorry my english.

Grillo


I may have a couple of ideas that can help.

First when you are executing a SQL command that returns one item such as the
count(*) you should use ExecuteScalar. This is much faster than a reader
since it does not have the overhead of creating things it does not need.

Then you could use:

commCountQuestions = new SqlCommand("SELECT COUNT(*) AS total FROM
Questions", conn);
int t = (int)commCountQuestion.ExecuteScalar();

The same can be done when you are getting sqlcommQUESTION = "SELECT * FROM
Questions WHERE id=" + i + "";. If all you are doing is getting the
question text, use the fieldname for the question text rather than *. Then
once you have the question text you can use the reader to get the possible
answers.

Hope this helps
LLoyd Sheen
Nov 9 '07 #2
You can solve this with nested reads, a sql loop, a join or a data table.

nested readers, a connection can only handle on reader, so you just need to
use two connections. normally you woudl use the same transaction scope so
they don't lock each other, but not neccessary in this case.

sql loop (returns mutilple result sets):

set nocount on
declare @i int
set @i =1
while @i < 16
begin
SELECT * FROM Questions WHERE id=@i
SELECT * FROM Answers WHERE id =@i
set @i = @i + 1
end

sql join:

select *
from question q
join answes a on a.id = q.id

datatable:

sql = "select * from question;select * from answers;"

load into dataset (creates two tables), add parent/child constaint

-- bruce (sqlwork.com)
"Guilherme Grillo" wrote:
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

<Ji*******@gmail.comwrote in message
news:11**********************@v23g2000prn.googlegr oups.com...
declare @someId int
set @someId = 1
select * from categories
where categoryid = @someId

hope it can help :)
Guilherme Grillo wrote:
Hi...

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

Example:

SELECT * FROM ANSWERS WHERE id = 1

then return the results and execute

SELECT * FROM ANSWERS WHERE id = 2

... ... ...

It's possible?

Thanks and sorry my english.

Grillo

Nov 9 '07 #3
Set up two tables in a DataSet instead of using two readers. you can then do
whatever you desire with the Questions and Answers. You can even get teh
count by counting rows in one or the other (assuming they are matched). No
reason to do three queries when a single stored proc will do the job.

In a DataSet, the join can be placed prior to doing any filling (ie, when
you are designing). This will mean you have to fill the parent (most likely
QUESTIONS) first. They will be joined without having any issues, as the
tables in the DataSet will be joined.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|
*************************************************
"Guilherme Grillo" <we****@rncomtotal.com.brwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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

<Ji*******@gmail.comwrote in message
news:11**********************@v23g2000prn.googlegr oups.com...
>declare @someId int
set @someId = 1
select * from categories
where categoryid = @someId

hope it can help :)
Guilherme Grillo wrote:
>>Hi...

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

Example:

SELECT * FROM ANSWERS WHERE id = 1

then return the results and execute

SELECT * FROM ANSWERS WHERE id = 2

... ... ...

It's possible?

Thanks and sorry my english.

Grillo



Nov 9 '07 #4
Thanks Everybody....

I 'll have much to do in the weekend with this informations...

:)

Grillo
"Cowboy (Gregory A. Beamer)" <No************@comcast.netNoSpamMwrote in
message news:%2****************@TK2MSFTNGP04.phx.gbl...
Set up two tables in a DataSet instead of using two readers. you can then
do whatever you desire with the Questions and Answers. You can even get
teh count by counting rows in one or the other (assuming they are
matched). No reason to do three queries when a single stored proc will do
the job.

In a DataSet, the join can be placed prior to doing any filling (ie, when
you are designing). This will mean you have to fill the parent (most
likely QUESTIONS) first. They will be joined without having any issues, as
the tables in the DataSet will be joined.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box! |
*************************************************
"Guilherme Grillo" <we****@rncomtotal.com.brwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>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

<Ji*******@gmail.comwrote in message
news:11**********************@v23g2000prn.googleg roups.com...
>>declare @someId int
set @someId = 1
select * from categories
where categoryid = @someId

hope it can help :)
Guilherme Grillo wrote:
Hi...

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

Example:

SELECT * FROM ANSWERS WHERE id = 1

then return the results and execute

SELECT * FROM ANSWERS WHERE id = 2

... ... ...

It's possible?

Thanks and sorry my english.

Grillo




Nov 9 '07 #5
Guilherme Grillo pretended :
>
commCountQuestions = new SqlCommand("SELECT COUNT(*) AS total FROM
Questions", conn);

int t = readerPerguntas.GetOrdinal("total");
Note: the value that gets set in "t" is not the requested total, but
the index of the column containing that total (and that would be 0
here).
Use the GetInt32( ) method to get that total.

Hans Kesting
Nov 12 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rhamphoryncus | last post by:
First a bit about myself. I've been programming in python several years now, and I've got several more years before that with C. I've got a lot of interest in the more theoretical stuff (language...
2
by: Jim | last post by:
Im getting way too many rows retured..what its trying to do is insert a 0 for revenue for months 7 - 12 (aka July through December) for each of these cost centers for each payor type..Im getting a...
4
by: hall | last post by:
Hi. I've come across someting strange. I was trying to make a for-loop execute repetadly until the function called inside it does not return true during the entire loop (see program below). ...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
5
by: Jani Yusef | last post by:
Based on an interview question I heard of but did not know the answer to....... How do you find and remove a loop from a singly linked list? In a google groups search I found the following code...
63
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
5
by: Jay | last post by:
In a sub procedure how do I recall/restart the sub and maintain the position in a loop? Eg (this doesnt work how I need it to work)... sub Get_Language() 'code here to prompt the user to do...
9
by: pauldepstein | last post by:
On my visual c++ compiler, I compiled code which contained something like for( int i =0; i < 5; i++) { double x =5;} I expected it to give a compiler error because x is being redefined
3
by: Gigs_ | last post by:
public class StreamsIOApp { public static void Main(string args) { // create, write, close, open, read, close byte buf1 = new Byte {76,101,116,32,116,104,101,114,101,...
1
by: Peter Duniho | last post by:
On Sat, 17 May 2008 16:36:17 -0700, <msnews.microsoft.comwrote: Sounds like a homework problem. Here's a hint: if you want your loop to go backwards, adding to (incrementing) the counter...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.