Conditional Statement in StoreProcedure


For example, if we have to insert person name and before inserting the person name we have to check Name availability in database. Then we can write one store procedure like this.

CREATE PROCEDURE InsertData

(

@firstName varchar(50),

@lastName varchar(50),

@age int

)

AS

/* SET NOCOUNT ON */

DECLARE @Count int

select @Count = Count(PersonID) from Person WHERE FirstName = @firstName

IF @Count = 0

BEGIN

INSERT INTO Person (FirstName, LastName, Age)

VALUES

(@firstName, @lastName, @age)

END

RETURN @Count

RETURN

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: