更新后我在CONTENT表上触发了一个触发器。在MESSAGE列值更新触发器的表中,必须根据国家/地区将包含CONTENT_ID和MESSAGE的新记录插入4个不同表中的一个表中。 将根据CONTENT表中的CONTENT_ID列值从CONTENT_COUNTRY_INFO表中检索国家/地区值。 4个不同的表是CONTENT_IN,CONTENT_MY,CONTENT_CN,CONTENT_HK。 我是按照以下方式做的。但不知何故,很少行没有插入区域表,也没有将MESSAGE列更新为" Moved"。
ALTER TRIGGER CONTENT_MOVE_TRG ON CONTENT
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON
DECLARE @V_CONTENTID INT
DECLARE @V_COUNTRY VARCHAR(10)
DECLARE CONTENTIDS CURSOR LOCAL FOR SELECT TR.CONTENT_ID FROM CONTENT TR INNER JOIN INSERTED I ON TR.CONTENT_ID = I.CONTENT_ID
OPEN CONTENTIDS
FETCH NEXT FROM CONTENTIDS INTO @V_CONTENTID
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @V_COUNTRY = COUNTRY_NAME FROM CONTENT_COUNTRY_INFO WHERE CONTENT_ID = @V_CONTENTID
IF @V_COUNTRY = 'CN' BEGIN
INSERT INTO CONTENT_CN(CONTENT_ID,MESSAGE) SELECT CONTENT_ID, MESSAGE FROM CONTENT WHERE CONTENT_ID = @V_CONTENTID
END
ELSE IF @V_COUNTRY = 'IN' BEGIN
INSERT INTO CONTENT_IN(CONTENT_ID,MESSAGE) SELECT CONTENT_ID, MESSAGE FROM CONTENT WHERE CONTENT_ID = @V_CONTENTID
END
ELSE IF @V_COUNTRY = 'HK' BEGIN
INSERT INTO CONTENT_HK(CONTENT_ID,MESSAGE) SELECT CONTENT_ID, MESSAGE FROM CONTENT WHERE CONTENT_ID = @V_CONTENTID
END
ELSE IF @V_COUNTRY = 'MY' BEGIN
INSERT INTO CONTENT_MY(CONTENT_ID,MESSAGE) SELECT CONTENT_ID, MESSAGE FROM CONTENT WHERE CONTENT_ID = @V_CONTENTID
END
FETCH NEXT FROM CONTENTIDS INTO @V_CONTENTID
END
UPDATE CONTENT SET MESSAGE='Moved' WHERE CONTENT_ID IN (SELECT TR.CONTENT_ID FROM CONTENT TR INNER JOIN INSERTED I ON TR.CONTENT_ID = I.CONTENT_ID)
CLOSE CONTENTIDS
DEALLOCATE CONTENTIDS
END
我担心使用游标会出现这类问题,寻找动态查询将记录移动到各自的区域表中。 任何想法都会有所帮助!
谢谢,
拉朱