If...Else statement is used to test a condition. If we have more than 1 condition we can use If...Else statement. We can also have nested If...Else statements also. We have nested If...Else statements when we have to include 1 or more If...Else statements in 1 If...Else statement. If...Else Statement in SQL is used in execution os Stored Procedures.
Syntax for simple If...Else statement is,
IF ( Condition )
BEGIN
Sql Statements
END
ELSE
BEGIN
Sql Statements
END
Syntax for nested If...Else statements is,
IF ( Condition )
BEGIN
Sql Statements
IF ( Condition )
BEGIN
Sql Statements
END
ELSE
BEGIN
Sql Statements
END
END
ELSE
BEGIN
Sql Statements
END
Example for simple If...Else statement is,
CREATE PROCEDURE [dbo].[USP_InsertUserSession]
@userId int,
@userSession nvarchar(255),
@userDate datetime,
AS
BEGIN
declare @tmpuserId int
SELECT @tmpuserId=userId from UsersSessions WHERE userId=@userId
if @tmpuserId > 0
begin
UPDATE UsersSessions SET userSession=@userSession, userDate=@userDate WHERE userId=@tmpuserId
end
else
begin
INSERT INTO UsersSessions(userId,userSession,userDate) VALUES (@userId,@userSession,@userDate)
end
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment