0

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 
0

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.