chamara
chamara

Reputation: 12709

Stored Procedure OutPut Parameters

i need to write a stored procedure which will return a string.logic is

when user try to insert a new record i need to check whether that record already exist.if exist need to return msg "Record exist" else return "Inserted"

following is what i have done for the moment and i'm stuck here.can some one help me to complete the procedure

CREATE PROCEDURE [dbo].[spInsetPurpose]
@Purpose VARCHAR(500),
@Type VARCHAR(6),
@Result VARCHAR(10)= NULL OUTPUT
AS
BEGIN
Declare @Position VARCHAR(20)
DECLARE @TempTable TABLE  (Purpose VARCHAR(500))

INSERT INTO @TempTable
SELECT Purpose FROM tblPurpose WHERE Purpose=@Purpose

INSERT INTO tblPurpose(Purpose,[Type]) VALUES(@Purpose,@Type) 

END 

Upvotes: 0

Views: 436

Answers (1)

mabstrei
mabstrei

Reputation: 1220

To check if the row already exists you can do

If Exists (Select Top 1 1 from tblPurpose where Purpose = @Purpose and [Type] = @Type)
Begin
   Insert Into tblPurpose
     (Purpose, [Type])
   Select 
     @Purpose, @Type

   SET @Result = 'Inserted'
End
Else
Begin
   SET @Result = 'Record exists'
End

Upvotes: 3

Related Questions