Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a table with data as follows.

Name     Size
----------------
A          1
B          2
C          1
D          1
E          3
F          2

I have to write an SQL (sql server) which will return 3(as there  are 3 types of sizes are in the table) tables. The number of produced table can be increased by inserting new size data in the main table.

The result of the required SQL should be as follows:-
Name     Size
----------------
A          1
C          1
D          1

Name     Size
----------------
B          2
F          2

Name     Size
----------------
E          3

Is there any easy way to produce the result without using any loop in the SQL?
Posted

1 solution

If you want three resultsets, then the answer is no. You would need a loop to do that (unless you hard-coded the statements, which would be worse). However, I don't see the business case for doing something like this. I would recommend returning one resultset and then break it apart in code (if you need to).

If you really needed three (or more) resultsets to be returned to your code, you could also create a stored procedure that takes in the Size you want and returns that resultset. That way you could be more flexible in how you called it and yet it would still handle the multiple cases.

These are just some ideas to refactor your design. In the end, the basic answer to your question is no, you cannot do that without a loop.
 
Share this answer
 
Comments
JakirBB 27-Jun-12 2:22am    
Thanks, Tim Corey for your quick response. I agree with you. But one question, as I have two options - first is, generating one result set and use it in code [probably to generate other result sets], and other is using loop in SQL and generate multiple result sets. But my question is which one is better?

Thanks again for your quick reply.
Tim Corey 27-Jun-12 8:43am    
I can give my general opinion, but some of this comes down to environment. In general, I like to let SQL do what it does best (process data) and I let my application do what it does best (interpret/display/capture data). That means I would probably create one stored procedure on the server that returned one list at a time. I would then probably call it three times. Three calls isn't ideal, but it would allow me to be more flexible in how I operated. My next choice would be the one stored procedure that looped through the items and returned multiple resultsets. As in any situation, though, you need to take your environment into account. Is your SQL server overloaded? If so, maybe you should process the data at the client. Is your front-end a bit slow? Then do all you can on the server before you send the data down. In the end, test, test, test.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900