In this post we will discuss how to use If Else statement in sql server 2008. Also you can check my previous psots on:
- If Else statements in JavaScript Example
- Triggers in sql server 2008
- How to give Tooltips in WPF in Asp.Net?
We can use conditional statements in sql server 2008 and widely used while we are using stored procedures.
You can also use nested is else statements.
Syntax: (from msdn)
IF Boolean_expression
{ sql_statement | statement_block }
[ ELSE
{ sql_statement | statement_block } ]
Example:
Here in the below example I have used one if else statement. It will check whether @EmployeeID >0 or not. If @EmployeeID>0 then it will execute one statement else it will execute another statement.
USE [TestDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetEmployees]
@EmployeeID Int
AS
BEGIN
BEGIN TRY
if(@EmployeeID > 0)
BEGIN
SELECT * FROM Employee WHERE EmployeeID=@EmployeeID
END
ELSE
BEGIN
SELECT * FROM Employee
END
END
GO
- If Else statements in JavaScript Example
- Triggers in sql server 2008
- How to give Tooltips in WPF in Asp.Net?
We can use conditional statements in sql server 2008 and widely used while we are using stored procedures.
You can also use nested is else statements.
Syntax: (from msdn)
IF Boolean_expression
{ sql_statement | statement_block }
[ ELSE
{ sql_statement | statement_block } ]
Example:
Here in the below example I have used one if else statement. It will check whether @EmployeeID >0 or not. If @EmployeeID>0 then it will execute one statement else it will execute another statement.
USE [TestDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetEmployees]
@EmployeeID Int
AS
BEGIN
BEGIN TRY
if(@EmployeeID > 0)
BEGIN
SELECT * FROM Employee WHERE EmployeeID=@EmployeeID
END
ELSE
BEGIN
SELECT * FROM Employee
END
END
GO