Click here to Skip to main content
15,886,110 members
Home / Discussions / Database
   

Database

 
GeneralRe: Check Different Select query result Pin
Naunt12-Aug-11 0:46
Naunt12-Aug-11 0:46 
GeneralRe: Check Different Select query result Pin
Wendelius12-Aug-11 0:55
mentorWendelius12-Aug-11 0:55 
GeneralRe: Check Different Select query result Pin
Naunt12-Aug-11 1:09
Naunt12-Aug-11 1:09 
GeneralRe: Check Different Select query result Pin
Wendelius12-Aug-11 2:38
mentorWendelius12-Aug-11 2:38 
GeneralRe: Check Different Select query result Pin
Naunt12-Aug-11 0:50
Naunt12-Aug-11 0:50 
AnswerRe: Check Different Select query result Pin
Wendelius11-Aug-11 21:04
mentorWendelius11-Aug-11 21:04 
GeneralRe: Check Different Select query result Pin
Naunt11-Aug-11 22:24
Naunt11-Aug-11 22:24 
AnswerRe: Check Different Select query result [modified] Pin
Wendelius11-Aug-11 23:04
mentorWendelius11-Aug-11 23:04 
Ok,

If I take an example. Perhaps the easiest way to combine different statements would be to use union. For example if you have following logic (pseudo):
SQL
select @result = count(*) from table where column1 < 100
if @result > 10 then color = blue
else
  select @result = count(*) from table where column1 > 100
  if @result = 20 then color = yellow

Now you could combine these to something like:
SQL
cursor = select count(*) from table where column1 < 100
         union all
         select count(*) from table where column1 > 100
if first_row_in_cursor > 10 then color = blue
else if second_row_in_cursor = 20 then  color = yellow

However, as you see, both select statements would get the count from different rows. In example 1 the second query isn't executed at all if count > 10. In the second example all the selects are executed even if the results in different portions aren't needed. This can be very time (and resource) consuming.

This was a trivial case and most likely isn't exactly the same as in your situation but the point I'm trying to make is that is it really beneficial to combine all the statements. Another point of view is that the statement may become very large and hard to maintain if it contains too much logic.

Another (perhaps an easy ) option is to use scalar queries. This might work more easily for your case. The previous example could be something like:
SQL
select
      @result1 = (select count(*) from table where column1 < 100)
      @ersult2 = (select count(*) from table where column1 > 100);

if @result1 > 10 then color = blue
else if @result2 = 20 then  color = yellow


However the same performance problem as described earlier may arise.
The need to optimize rises from a bad design.My articles[^]
modified on Friday, August 12, 2011 6:43 AM

GeneralRe: Check Different Select query result Pin
Naunt12-Aug-11 0:56
Naunt12-Aug-11 0:56 
AnswerRe: Check Different Select query result Pin
Pascal Ganaye16-Aug-11 6:28
Pascal Ganaye16-Aug-11 6:28 
QuestionSee this Pin
sivakumat11-Aug-11 1:20
sivakumat11-Aug-11 1:20 
QuestionCompare 3columns from a row with data Null value Pin
Naunt10-Aug-11 23:16
Naunt10-Aug-11 23:16 
AnswerRe: Compare 3columns from a row with data Null value Pin
Mycroft Holmes10-Aug-11 23:26
professionalMycroft Holmes10-Aug-11 23:26 
AnswerRe: Compare 3columns from a row with data Null value Pin
sujit076110-Aug-11 23:55
sujit076110-Aug-11 23:55 
GeneralRe: Compare 3columns from a row with data Null value Pin
Naunt11-Aug-11 0:18
Naunt11-Aug-11 0:18 
QuestionHELP !! Pin
Josh7harington10-Aug-11 5:14
Josh7harington10-Aug-11 5:14 
AnswerRe: HELP !! Pin
Eddy Vluggen10-Aug-11 11:04
professionalEddy Vluggen10-Aug-11 11:04 
QuestionLoop in a Stored procedure Pin
lionelcyril9-Aug-11 20:50
lionelcyril9-Aug-11 20:50 
AnswerRe: Loop in a Stored procedure Pin
Shameel9-Aug-11 22:20
professionalShameel9-Aug-11 22:20 
GeneralRe: Loop in a Stored procedure Pin
lionelcyril10-Aug-11 0:40
lionelcyril10-Aug-11 0:40 
GeneralRe: Loop in a Stored procedure Pin
Shameel10-Aug-11 8:42
professionalShameel10-Aug-11 8:42 
GeneralRe: Loop in a Stored procedure Pin
lionelcyril10-Aug-11 8:46
lionelcyril10-Aug-11 8:46 
GeneralRe: Loop in a Stored procedure Pin
S Douglas20-Aug-11 9:20
professionalS Douglas20-Aug-11 9:20 
AnswerRe: Loop in a Stored procedure Pin
jschell10-Aug-11 10:07
jschell10-Aug-11 10:07 
QuestionInner Query in mysql Pin
Rakesh Meel8-Aug-11 0:56
professionalRakesh Meel8-Aug-11 0:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.