检查记录是否存在,如果是,则“更新”,如果不是“插入”

时间:2010-07-06 18:52:57

标签: sql-server stored-procedures merge sql-server-2000 upsert

4 个答案:

答案 0 :(得分:15)

答案 1 :(得分:5)

CREATE PROCEDURE sp_UpdateProcessed
AS
BEGIN
    DECLARE @Processed_Status NVARCHAR(256)
    SET @Processed_Status = 'ACTIVE'
    IF(@Processed_Status <> 'Processed')
    BEGIN
       SET @Processed_Status = 'Active'
       UPDATE ST_JnlMediumMoveNew
       SET ST_JnlMediumMoveNew.Process_Status = @Processed_Status 
    END
END

答案 2 :(得分:1)

您在计算行数,但不在决策中使用它。这是一个可能有用的决策结构

Select @pre_var = count(*) From PREMIUM_SERVICE_USER Where strClientID = @strClientID

IF @pre_var = 0
BEGIN
    /* Run Insert Code Here */
END
ELSE
BEGIN
    /* Run Update Code Here */
END


IF @Premium = 1 
BEGIN
    /* Run Premier Members Update Code Here */
END
ELSE
BEGIN
    /* Run Non-Premier Members Update Code Here */
END

或者这个..

IF @pre_var = 0
BEGIN
    /* Run Insert Code Here */
END
ELSE
BEGIN
    IF @Premium = 1 
    BEGIN
        /* Run Premier Members Update Code Here */
    END
    ELSE
    BEGIN
        /* Run Non-Premier Members Update Code Here */
    END
END

答案 3 :(得分:0)