Saturday 30 July 2011

IF Statement in SQL


Imposes conditions on the execution of a Transact-SQL statement. The Transact-SQL statement that follows an IF keyword and its condition is executed if the condition is satisfied: the Boolean expression returns TRUE. The optional ELSE keyword introduces another Transact-SQL statement that is executed when the IF condition is not satisfied: the Boolean expression returns FALSE.

Example: 

declare @TotalAgra int
declare @TotalShimla int
set @TotalAgra=(select COUNT(*) from tblStudentMaster where collegeCampusId =10 )
if @TotalAgra>1
begin
print 'Agra Total Students='+ convert(varchar(10),@TotalAgra)
end

else
set @TotalShimla=(select COUNT(*) from tblStudentMaster where collegeCampusId =11 )
begin
print 'Shimla Total Students='+ convert(varchar(100),@TotalShimla)
end

No comments:

Post a Comment