2010-05-26 52 views
6

我寫過一個觸發器。異構查詢需要ANSI_NULLS

USE [TEST] 
GO 
/****** Object: Trigger [dbo].[TR_POSTGRESQL_UPDATE_YC] Script Date: 05/26/2010 08:54:03 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

ALTER TRIGGER [dbo].[TR_POSTGRESQL_UPDATE_YC] ON [dbo].[BCT_CNTR_EVENTS] 

FOR INSERT 
AS 
BEGIN 


DECLARE @MOVE_TIME varchar(14); 
DECLARE @MOVE_TIME_FORMATED varchar(20); 
DECLARE @RELEASE_NOTE varchar(32); 
DECLARE @CMR_NUMBER varchar(15); 
DECLARE @MOVE_TYPE varchar(2); 

SELECT @MOVE_TIME = inserted.move_time 
     ,@MOVE_TYPE = inserted.move_type 
     ,@RELEASE_NOTE = inserted.release_note 
     ,@CMR_NUMBER = inserted.cmr_number FROM inserted 


IF(@MOVE_TYPE = 'YC') 
    BEGIN 

    SET @MOVE_TIME_FORMATED = SUBSTRING(@MOVE_TIME,1,4) + '-' + SUBSTRING(@MOVE_TIME,5,2) + '-' + SUBSTRING(@MOVE_TIME,7,2) + ' 00:00:00' 

    --UPDATE OpenQuery(POSTGRESQL_SERV,'SELECT visit_cmr,visit_timestamp,visit_pin FROM VISIT') 
    --  SET visit_cmr = @RELEASE_NOTE 
    --  WHERE visit_timestamp = @MOVE_TIME_FORMATED 
    --  AND visit_pin = right(@CMR_NUMBER,5) 
    --  AND visit_cmr IS NULL 

    END 

    SET NOCOUNT ON; 
END 

我收到一個錯誤,當我試圖插入一行。

異構查詢需要爲連接設置ANSI_NULLS和ANSI_WARNINGS選項 。這確保了一致的查詢語義。 啓用這些選項,然後重新發出您的查詢。

之後,我試圖使用SET SET ANSI_WARNINGS is ON,但它不起作用。 (鏈接服務器的PostgreSQL觸發)

+0

同樣的錯誤在觸發設置爲「ON」的時候? – gbn 2010-05-26 06:12:24

+0

語法好的。 我在我的本地服務器上做了一個測試'觸發器' - 這是一個很好的工作!..但是在一個真正的服務器上,它給我一個錯誤(我用php應用程序)來插入行。 **異構查詢需要爲連接設置ANSI_NULLS和ANSI_WARNINGS選項。這確保了一致的查詢語義。啓用這些選項,然後重新發布您的查詢。** – Dezigo 2010-05-26 06:19:38

回答

8

嘗試使用此

GO 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

ALTER TRIGGER [dbo].[TR_POSTGRESQL_UPDATE_YC] ON [dbo].[BCT_CNTR_EVENTS] 

AFTER INSERT 
AS 
BEGIN 


DECLARE @MOVE_TIME varchar(14); 
DECLARE @MOVE_TIME_FORMATED varchar(20); 
DECLARE @RELEASE_NOTE varchar(32); 
DECLARE @CMR_NUMBER varchar(15); 
DECLARE @MOVE_TYPE varchar(2); 

SELECT @MOVE_TIME = inserted.move_time 
     ,@MOVE_TYPE = inserted.move_type 
     ,@RELEASE_NOTE = inserted.release_note 
     ,@CMR_NUMBER = inserted.cmr_number FROM inserted 


IF(@MOVE_TYPE = 'YT') 
    BEGIN 

    SET @MOVE_TIME_FORMATED = SUBSTRING(@MOVE_TIME,1,4) + '-' + SUBSTRING(@MOVE_TIME,5,2) + '-' + SUBSTRING(@MOVE_TIME,7,2) + ' 00:00:00' 

    SET ANSI_NULLS ON 
    SET ANSI_WARNINGS ON 

    UPDATE OpenQuery(POSTGRESQL_SERV,'SELECT visit_cmr,visit_timestamp,visit_pin FROM VISIT') 
      SET visit_cmr = @RELEASE_NOTE 
      WHERE visit_timestamp = @MOVE_TIME_FORMATED 
      AND visit_pin = right(@CMR_NUMBER,5) 
      AND visit_cmr IS NULL 

    END 

    SET NOCOUNT ON; 
END