2011-10-21 146 views
0

我需要寫一個存儲過程將返回一個string.logic是存儲過程輸出參數

當用戶嘗試插入一個新的記錄,我需要檢查記錄已經exist.if存在是否需要返回味精「記錄存在」否則返回「插入」

以下是我的時刻做,我又立刻陷入here.can有一個人幫我辦手續

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 [email protected] 

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

END 

回答

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