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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.