multiple if conditions

I have a stored procedure in which a field is being returned as Yes or No based upon a column in a table using case statement.now I need to add another condition to retrieve the field.below is the condition.

If a particular input parameter is 1,I need to retain the existing condition itself. If the same input parameter is 2,I need to return yes or no based upon another date column in a different table.

How can I implement this.

September 8th, 2015 1:30pm

Is that table included into original query? If yes, just write the query accordingly using the outer CASE statement. If not, I would probably use 2 separate statements and IF / ELSE construct.
Free Windows Admin Tool Kit Click here and download it now
September 8th, 2015 1:33pm

YES it is included,

can you give a sample query with the multiple if conditions.or can i retain the CASE along with if .?

September 8th, 2015 1:51pm

case @InputParameter 

when 1

  then case ... -- original case statement

when 2

  then -- your new condition here

else -- what should you do for other possible values of input parameter?

  ---

end

Free Windows Admin Tool Kit Click here and download it now
September 8th, 2015 1:58pm

I have a stored procedure in which a field [sic] is being returned as Yes or No [sic] based upon a column in a table using CASE statement [sic]. now I need to add another condition to retrieve the field [sic]. below is the condition.

Please read a book on SQL. 

The term field means part of a temporal value {YEAR, MONTH, DAY, HOUR, MINUTE, SECOND}. I think that you meant column instead. 

CASE is an expression, not a statement. This I not just SQL! This is a fundamental programming concept.

Too bad we cannot see the code. But I guess you have magic powers and can programming without seeing code. 

>> If a particular input parameter is 1, I need to retain the existing condition itself. If the same input parameter is 2, I need to return yes or no based upon another date column in a different table. <<

Another fundamental programming concept error. A parameter is a formal marker in the procedure declaration. A specific value assigned to a parameter on invocation is an argument. 

Finally, we do not use flags in SQL or other languages. If you had taken a course or read a single book on Software Engineering, you would know about coupling and cohesion. 

Ideally, a module of code in any language performs one and only one clearly defined task, not matter where it is invoked; this is called functional cohesion (think of a mathematical function as the best example). But your incoherent code depends on a flag to determine the task to be done. It might be automobiles, it might be squids, it might be Lady GaGa! You will find out at invocation! The environment determines what the code does! Magic! 

If you had followed forum netiquette, we could help you. Want to try again? 

You obviously never did any real programming before, and it is hard to get the basics the first few months. And SQL was a bad place to start learning (most of us begin with procedural rather than declarative languages). Stick with it and you get it in a few years. 

September 8th, 2015 3:09pm

I have a stored procedure in which a field [sic] is being returned as Yes or No [sic] based upon a column in a table using CASE statement [sic]. now I need to add another condition to retrieve the field [sic]. below is the condition.

Please read a book on SQL. 

The term field means part of a temporal value {YEAR, MONTH, DAY, HOUR, MINUTE, SECOND}. I think that you meant column instead. 
CASE is an expression, not a statement. This I not just SQL! This is a fundamental programming concept.

Too bad we cannot see the code. But I guess you have magic powers and can programming without seeing code. 

>> If a particular input parameter is 1, I need to retain the existing condition itself. If the same input parameter is 2, I need to return yes or no based upon another date column in a different table. <<

Another fundamental programming concept error. A parameter is a formal marker in the procedure declaration. A specific value assigned to a parameter on invocation is an argument. 

Finally, we do not use flags in SQL or other languages. If you had taken a course or read a single book on Software Engineering, you would know about coupling and cohesion. 

Ideally, a module of code in any language performs one and only one clearly defined task, not matter where it is invoked; this is called functional cohesion (think of a mathematical function as the best example). But your incoherent code depends on a flag to determine the task to be done. It might be automobiles, it might be squids, it might be Lady GaGa! You will find out at invocation! The environment determines what the code does! 

If you had followed forum netiquette, we could help you. Want to try again?

Free Windows Admin Tool Kit Click here and download it now
September 8th, 2015 3:11pm

Try this example:

DECLARE @flag INT

SET @flag = 2

SELECT CASE WHEN @flag = 1 THEN CASE WHEN 1 = 1 THEN 'Yes'
                                     WHEN 2 = 1 THEN 'No'
								END
			WHEN @flag = 2 THEN CASE WHEN 1 = 1 THEN 'No'
			                         WHEN 2 = 1 THEN 'Yes'
								END
            ELSE 'Who knows?!'
	   END

September 8th, 2015 3:30pm

Hi ams16,

Are you looking for a nested CASE WHEN expression? Please see below sample.

DECLARE @T TABLE(ID INT,a_column VARCHAR(99))
DECLARE @T2 TABLE(ID INT,date_column DATE)

DECLARE @input_parameter INT =1

SELECT 
CASE WHEN @input_parameter = 1 
		THEN CASE WHEN t.a_column = 'Yes' THEN 'Yes'
				  ELSE 'No'
			 END
	 WHEN @input_parameter =2
		THEN CASE WHEN t2.date_column = GETDATE() THEN 'Yes'
				  ELSE 'No'
			 END
END AS A_FIELD
FROM @T t JOIN @T2 t2 ON t.ID=t2.ID

If you have any question, feel free to let me know.
Free Windows Admin Tool Kit Click here and download it now
September 8th, 2015 10:56pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics