这个SQL我做错了什么?

时间:2012-06-20 15:20:34

标签: sql tsql

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

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')

1 个答案:

答案 0 :(得分: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')将失败,并且不会执行任何操作。