20

如果没有插入表的记录,我想检查表PREMIUM_SERVICE_USER是否存在 strClientID更新timeValid+30 的记录。strClientIDpremium_service_user

我究竟做错了什么?

它增加timeValid了 +30 天,但也插入了另一行。

SELECT @pre_var = count(*) 
FROM PREMIUM_SERVICE_USER 
WHERE strClientID = @strClientID

/* bronze premium - 200 cash */
IF @Premium = 1
BEGIN
    INSERT INTO PREMIUM_SERVICE_USER 
        (strClientID, timeReg, timeValid, bCurrent, durum) 
    VALUES 
        (@strClientID,getdate(),getdate() + 30,'1','1')

    UPDATE TB_USER 
    SET cash = cash+200 
    WHERE strAccountID = @strClientID
END

IF @Premium = 1 AND @pre_var = 1
BEGIN
    UPDATE PREMIUM_SERVICE_USER 
        SET timevalid = timevalid+30 where strClientID = @strClientID
    UPDATE PREMIUM_SERVICE_USER 
        SET bCurrent = 1 where strClientID = @strClientID
    UPDATE TB_USER 
        SET cash = cash+200 WHERE strAccountID = @strClientID
END
4

4 回答 4

18

您的问题是运行第一个if而不考虑@pre_var.

这是一种稍微不同的方法,如果PREMIUM_SERVICE_USER很大,效率会更高。

if @Premium = 1
  begin
    if exists(Select 1 From PREMIUM_SERVICE_USER Where strClientID = @strClientID)
      BEGIN
        update PREMIUM_SERVICE_USER set timevalid = timevalid+30 where strClientID = @strClientID
        update PREMIUM_SERVICE_USER set bCurrent = 1 where strClientID = @strClientID
        UPDATE TB_USER SET cash = cash+200 WHERE strAccountID = @strClientID
       END
    ELSE
      BEGIN
        INSERT INTO PREMIUM_SERVICE_USER (strClientID, timeReg, timeValid, bCurrent, durum) VALUES (@strClientID,getdate(),getdate() + 30,'1','1')
        UPDATE TB_USER SET cash = cash+200 WHERE strAccountID = @strClientID
       END
  end
于 2010-07-06T18:57:10.817 回答
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
于 2011-11-25T07:03:01.880 回答
2

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

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
于 2010-07-06T18:56:57.477 回答
0

看起来您在第一个 IF 语句中检查了错误的变量。如果 @Premium = 1 那么你会看到这种行为。

/* bronze premium - 200 cash */
IF @pre_var = 0
BEGIN
INSERT INTO PREMIUM_SERVICE_USER (strClientID, timeReg, timeValid, bCurrent, durum) VALUES (@strClientID,getdate(),getdate() + 30,'1','1')
UPDATE TB_USER SET cash = cash+200 WHERE strAccountID = @strClientID
END

IF @Premium = 1 AND @pre_var = 1
BEGIN
update PREMIUM_SERVICE_USER set timevalid = timevalid+30 where strClientID = @strClientID
update PREMIUM_SERVICE_USER set bCurrent = 1 where strClientID = @strClientID
UPDATE TB_USER SET cash = cash+200 WHERE strAccountID = @strClientID
END
于 2010-07-06T19:00:46.213 回答