SQL Server 2017触发器-声明变量

时间:2019-01-08 16:50:41

标签: sql-server triggers

今天是我第一次必须使用SQL Server,通常使用MySQL。

我做了一些搜索,但是,我还是没有得到。

代码如下:

CREATE TRIGGER "PREMI_after_insert" 
ON "PREMI" 
AFTER INSERT 
AS
    DECLARE @umur INT

    SELECT 
        @umur = DATEDIFF(YEAR, CAST(GETDATE() AS DATE),
       (CASE 
           WHEN ISNUMERIC(SELECT NoTerdaftar FROM inserted) = 1 
              THEN (SELECT CAST(TglLahir AS DATE) 
                    FROM KARYAWAN 
                    WHERE NoPegawai = (SELECT NoTerdaftar FROM inserted)) 
              ELSE (SELECT CAST(TglLahir AS DATE) 
                    FROM KELUARGA 
                    WHERE NoPeserta = (SELECT NoTerdaftar FROM inserted)) 
       END))

    DECLARE @plafond DECIMAL(20,0) 

    SELECT @plafond = (CASE  
                          WHEN ISNUMERIC(SELECT NoTerdaftar FROM inserted) = 1 
                             THEN (SELECT Plafond 
                                   FROM KARYAWAN 
                                   WHERE NoPegawai = (SELECT NoTerdaftar FROM inserted)) 
                             ELSE (SELECT Plafond 
                                   FROM KELUARGA 
                                   WHERE NoPeserta = (SELECT NoTerdaftar FROM inserted)) 
                       END)

    DECLARE @rat DECIMAL(7,6) 

    SELECT @rat = RE.Rate 
    FROM RATES RE 
    WHERE RE.KodeAsuransi = (SELECT JenisAsuransi FROM ASURANSI 
                             WHERE Id = (SELECT ID_Asuransi FROM inserted)) 
      AND @umur BETWEEN RE.UsiaMin AND RE.UsiaMax

BEGIN

    UPDATE PREMI
    SET JumlahPremi = (@plafond * @rat)
    WHERE Id = (SELECT Id FROM insterted)
END;

这是错误

enter image description here

拜托,别欺负我,那是我读了无数其他线程后得到的代码。...

我只是不了解SQL Server中触发器的结构。

有些使用“ GO”字,有些甚至不使用BEGIN和END,这是MySQL中的关键。...

0 个答案:

没有答案