SQL Server中的触发器中的IF条件

时间:2016-07-07 15:27:20

标签: sql-server-2012

我将此Oracle触发器转换为SQL Server。在Oracle中,使用if条件来更新表。我想知道是否有必要在SQL Server中使用if语句。我想在where子句中使用。如果这是正确的,请告诉我?

由于

   CREATE OR REPLACE TRIGGER "DB1"."TG_PG_CODES"
   BEFORE UPDATE OF "str_number", "op_status"
   ON "DB1"."Header_T"
   REFERENCING OLD AS oldrow NEW AS newrow
   FOR EACH ROW
   BEGIN
   IF :newrow.op_status = 'X'
   THEN
   UPDATE DB1.pg_pollutant
   SET op_status = 'X',
         str_comp_status = '98',
         str_mod_person = '3327'
   WHERE str_number = :newrow.str_number;
   END IF;

   IF     :newrow.op_status <> 'X'
   AND SUBSTR (:oldrow.str_pgcodes, 1, 1) = '1'
   AND SUBSTR (:newrow.str_pgcodes, 1, 1) = '0'
   THEN
   UPDATE DB1.pg_pollutant
     SET op_status = 'X',
         str_comp_status = '98'
   WHERE str_number = :newrow.str_number
   AND str_pollutant_key = :newrow.str_number || '0';
   END IF;
   END;

 =============================================
 In sql server

CREATE TRIGGER [dbo].[TG_TG_PG_CODES] ON [dbo].[Header_T]
AFTER UPDATE
AS
 IF(UPDATE(str_number)
    OR UPDATE(op_status))
     BEGIN
         IF(
           (
               SELECT op_status
               FROM inserted
           ) = 'X')
             BEGIN
                 UPDATE a
                   SET
                       a.op_status = 'X',
                       a.str_comp_status = '98',
                       a.str_mod_person = '3327'
                 FROM DB1.pg_pollutant a
                      INNER JOIN inserted AS i ON a.str_number = i.str_number;
             END;
         UPDATE a
           SET
               a.op_status = 'X',
               a.str_comp_status = '98'
         FROM DB1.pg_pollutant a
              INNER JOIN inserted AS i ON a.[str_number] = i.[str_number]
                                          AND a.str_pollutant_key = i.[str_number]+'0'
              INNER JOIN Deleted AS d ON a.[str_number] = d.[str_number]
         WHERE i.stroperationalstatus <> 'X'
               AND SUBSTRING(d.str_pgcodes, 1, 1) = '1'
               AND SUBSTRING(i.str_pgcodes, 1, 1) = '0'; /* I used this condition in where clause */
     END;

0 个答案:

没有答案