---
title: "Checking IF…ELSE condition in Stored Procedure SQL Server"  
description: "SQL IF...ELSE Statement used to test a condition. IF...ELSE Statement using in execution of a Transact-SQL statement (Store Procedure or T-SQL) and Tr"  
author: "AVADHESH PATEL"  
published: 2012-09-06  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/354/checking-if-else-condition-in-stored-procedure-sql-server  
category: "database"  
tags: ["database"]  
reading_time: 2 minutes  

---

# Checking IF…ELSE condition in Stored Procedure SQL Server

SQL IF...ELSE Statement used to test a [condition](https://www.mindstick.com/forum/12711/select-query-with-and-condition). IF...ELSE Statement using in [execution](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) of a Transact-SQL statement ([Store Procedure](https://www.mindstick.com/forum/193/auto-increment-by-store-procedure) or T-SQL) and Trigger.\

IF tests can be [nested](https://www.mindstick.com/forum/45/itemcommand-event-in-nested-repeater-and-listview) after another IF or following an ELSE. There is no limit to the number of nested levels.\
\
IF condition is satisfied and the [Boolean](https://www.mindstick.com/forum/160332/how-does-the-boolean-function-work) [expression](https://www.mindstick.com/articles/1861/sqlite-expressions) returns TRUE, it will executed IF Block SQL statement. \
IF condition is not satisfied and the Boolean expression returns FALSE, it will executed ELSE Block SQL Statement query.

```
--CREATE PROCEDURECREATE PROCEDURE ELIGIBILITY(  @MaxAge INT --PARAMETER)ASBEGIN      IF(@MaxAge>=18) --CONDITION            BEGIN                  PRINT 'YOU ARE ELIGIBLE FOR ADMITION'--CONDITION TRUE            END      ELSE            BEGIN                  PRINT 'SORRY, YOU ARE NOT ELIGIBLE FOR ADMITION'--CONDITION FALSE            END

END
```

[Execute](https://www.mindstick.com/interview/49/how-can-i-execute-a-php-script-using-command-line) Procedure with [parameter](https://www.mindstick.com/blog/450/parameter-class-in-c-sharp) and see [output](https://www.mindstick.com/forum/161319/how-does-the-print-function-handle-output-in-python)

```
-- EXECUTE PROCEDUREEXEC ELIGIBILITY 2
```

Output- SORRY, YOU ARE NOT ELIGIBLE FOR ADMITION

```
EXEC ELIGIBILITY 22
```

Output- YOU ARE ELIGIBLE FOR ADMITION

```
EXEC ELIGIBILITY 2
```

Output- SORRY, YOU ARE NOT ELIGIBLE FOR ADMITION

```
EXEC ELIGIBILITY 22
```

Output- YOU ARE ELIGIBLE FOR ADMITION

##### IF-ELSE Condition with Procedure and SQL-T

```
 --CREATE PROCEDURE CREATE PROCEDURE Check_Exist(@Id INT,@Name VARCHAR(20),@Address VARCHAR(20))ASBEGIN      IF NOT EXISTS (SELECT [Id] FROM INFO WHERE [Id]=@Id)      BEGIN            INSERT INTO INFO VALUES(@Id,@Name,@Address)            PRINT 'New Record Insert Successfuly'      END      ELSE      BEGIN            PRINT 'Id All Ready Exist'      END

END
```

--EXECUTE PROCEDURE WITH ARGUMENT

EXEC Check_Exist <id>,<name>,<address>

---

Original Source: https://www.mindstick.com/blog/354/checking-if-else-condition-in-stored-procedure-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
