1

当我运行它时,它不会引发错误,但它也不会插入/删除条目。我不知道为什么它不起作用。

DECLARE @trAction VARCHAR(5)
  SET @trAction = 'addTR'

  IF EXISTS (SELECT TransgeneID
            FROM tbl_MT
            WHERE MouseID = '3' AND TransgeneID = '3')
    IF (@trAction = 'rmvTR')    
      DELETE
      FROM tbl_MT
      WHERE MouseID = '3' AND TransgeneID = '3'
  ELSE
    IF (@trAction = 'addTR')  
      INSERT INTO tbl_MT 
      VALUES ('3', '3')
4

1 回答 1

2

首先,将BEGINEND放在IF's

  DECLARE @trAction VARCHAR(5)
  SET @trAction = 'addTR'

  IF EXISTS (SELECT TransgeneID
            FROM tbl_MT
            WHERE MouseID = '3' AND TransgeneID = '3')
  BEGIN
    IF (@trAction = 'rmvTR')    
    BEGIN
      DELETE
      FROM tbl_MT
      WHERE MouseID = '3' AND TransgeneID = '3'
    END
  END
  ELSE
  BEGIN
    IF (@trAction = 'addTR')  
    BEGIN
      INSERT INTO tbl_MT 
      VALUES ('3', '3')
    END
  END

第二个也是最后一个,如果第一个IF是真的,内在IF (@trAction = 'rmvTR')就会失败,什么也做不了。

于 2012-06-20T15:26:17.523 回答