Updated on Kisan Patel
This tutorial will show you how to use IF
condition in SQL Server?
If condition works in SQL server in the same way it works in other programming language.
DECLARE @pdId int SET @pdId = 11 -- change here IF(EXISTS(SELECT PersonalDetailsId FROM PersonalDetails WHERE PersonalDetailsId = @pdId)) BEGIN SELECT * FROM PersonalDetails WHERE PersonalDetailsId = @pdId END ELSE BEGIN SELECT 'No record found.' as Results END
Look at above SQL code, we have first declared @pdId variable and setting its value as 11.In the next like we are checking for using EXISTS
in-built function with parameter as the record from PersonalDetail
s whose PersonalDetailsId
as @pdId
. If the record is found, EXISTS
returns true and the first IF block executes and gives all columns from PersonalDetails
otherwise ‘No record found’ message is show.
Records found »
Records not found »