存储过程OutPut参数

时间:2011-10-21 06:57:08

标签: sql sql-server stored-procedures

我需要编写一个存储过程,它将返回一个string.logic

当用户尝试插入新记录时,我需要检查该记录是否已经存在。如果存在则需要返回msg“记录存在”,否则返回“已插入”

以下是我现在所做的事情,我被困在这里。一些人帮我完成了程序

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 

1 个答案:

答案 0 :(得分:3)

要检查该行是否已存在,您可以执行

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