Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

MS SQL Multiple If

Posted on 2010-01-06
5
Medium Priority
?
185 Views
Last Modified: 2012-05-08
Hi,

I have the following sql Stored Procedure, I'm trying to add several If statements but am not sure if this is the correct syntax

SELECT ReviewerData=

If DraftEORev1Cleared = True Then
(select isnull(username,'NA') from tbl_login where isnull(EORev1ID,'NA') = tbl_login.designation) + ' (Date reviewed: ' + (isnull(cast(EORev1DateRev as varchar),'Not Reviewed')) + ', Status: ' + (isnull(cast(EORev1Status as varchar),'-')) + ')<br>Comments: ' + (isnull(cast(EORev1Comments as varchar(max)),'')) + '<br><br><hr />'
end if

If DraftEORev2Cleared = True Then
(select isnull(username,'NA') from tbl_login where isnull(EORev2ID,'NA') = tbl_login.designation) + ' (Date reviewed: ' + (isnull(cast(EORev2DateRev as varchar),'Not Reviewed')) + ', Status: ' + (isnull(cast(EORev2Status as varchar),'-')) + ')<br>Comments: ' + (isnull(cast(EORev2Comments as varchar(max)),'')) + '<br><br><hr />'+
end if

FROM dbo.tbl_main
WHERE id = @ID
end

Any help is appreciated!

0
Comment
Question by:sanjshah12
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 26192506
SELECT ReviewerData=

case when DraftEORev1Cleared = 1 Then
(select isnull(username,'NA') from tbl_login where isnull(EORev1ID,'NA') = tbl_login.designation) + ' (Date reviewed: ' + (isnull(cast(EORev1DateRev as varchar),'Not Reviewed')) + ', Status: ' + (isnull(cast(EORev1Status as varchar),'-')) + ')
Comments: ' + (isnull(cast(EORev1Comments as varchar(max)),'')) + '


'


when DraftEORev2Cleared = True Then
(select isnull(username,'NA') from tbl_login where isnull(EORev2ID,'NA') = tbl_login.designation) + ' (Date reviewed: ' + (isnull(cast(EORev2DateRev as varchar),'Not Reviewed')) + ', Status: ' + (isnull(cast(EORev2Status as varchar),'-')) + ')
Comments: ' + (isnull(cast(EORev2Comments as varchar(max)),'')) + '


'
else ''
end

FROM dbo.tbl_main
WHERE id = @ID
0
 

Author Comment

by:sanjshah12
ID: 26192671
Thanks aneeshattingal,

but does this means only one condition can be true, I would like to add the select statements together, so that both, or either one can be combined?
0
 
LVL 13

Expert Comment

by:sameer2010
ID: 26192681
You can use the following and code the similar thing for other IFs.:
IF DraftEORev1Cleared = True
  BEGIN
(select isnull(username,'NA') from tbl_login where isnull(EORev1ID,'NA') = tbl_login.designation) + ' (Date reviewed: ' + (isnull(cast(EORev1DateRev as varchar),'Not Reviewed')) + ', Status: ' + (isnull(cast(EORev1Status as varchar),'-')) + ')<br>Comments: ' + (isnull(cast(EORev1Comments as varchar(max)),'')) + '<br><br><hr />'
  END;

Open in new window

0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 1000 total points
ID: 26192727
SELECT ReviewerData=

CASE when DraftEORev1Cleared = 1 Then
(select isnull(username,'NA') from tbl_login where isnull(EORev1ID,'NA') = tbl_login.designation) + ' (Date reviewed: ' + (isnull(cast(EORev1DateRev as varchar),'Not Reviewed')) + ', Status: ' + (isnull(cast(EORev1Status as varchar),'-')) + ')
Comments: ' + (isnull(cast(EORev1Comments as varchar(max)),'')) + '
' ELSE '' END
+
CASE when DraftEORev2Cleared = True Then
(select isnull(username,'NA') from tbl_login where isnull(EORev2ID,'NA') = tbl_login.designation) + ' (Date reviewed: ' + (isnull(cast(EORev2DateRev as varchar),'Not Reviewed')) + ', Status: ' + (isnull(cast(EORev2Status as varchar),'-')) + ')
Comments: ' + (isnull(cast(EORev2Comments as varchar(max)),'')) + '
'
else ''
end

FROM dbo.tbl_main
WHERE id = @ID
0
 

Author Comment

by:sanjshah12
ID: 26192746
Thanks aneeshattingal,

that works!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question