I can use Sql Query in "If else" statement

Posted by Mesh_2010 under Interview Questions on 3/14/2011 | Points: 10 | Views : 3245 | Status : [Member] | Replies : 4
Hi,

I can use Sql Query in "If else" statement

i face this problem in interview...

Thanks
RameSH sinGH



Responses

Posted by: T.saravanan on: 3/14/2011 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,

What is your problem?
Do you ask is it possible to use If else condition in SQL Server?
If yes means...its possible....
Follow this syntax...
Here i am explain with simple procedure...
Create proc stp_Test

(
@nOperID int
)
as
BEGIN
IF (@nOperID = 1)
BEGIN
// Insert Statement
END
ELSE IF (@nOperID = 2)
BEGIN
// Update Statement
END
ELSE
BEGIN
// Delete Statement
END
END


Cheers :)

Thanks,
T.Saravanan

Mesh_2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mesh_2010 on: 3/14/2011 [Member] Starter | Points: 25

Up
0
Down

ok thanks

Thanks
RameSH sinGH

Mesh_2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gsolvers on: 3/14/2011 [Member] Starter | Points: 25

Up
0
Down
The use of SQL If else is definetely possible. But i believe the interviewer has asked question in a different context. He would have meant which is better for performance, is it either if..else from code or if...else from SQL. So it depends upon the scenarion and performance will be better if you can have this in code.

Best Regards,

VG
www.TeacherJi.com

Mesh_2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 3/15/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
We can use the if else statement in sql server like

IF condition

Begin
-- Your Code
End
Else
Begin
- Your Code
End


Thanks
Karthik
www.f5Debug.net

Mesh_2010, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response