异类查询需要ANSI_NULLS

时间:2010-05-26 06:05:29

标签: sql-server tsql triggers

我写了一个触发器。

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的触发器)

1 个答案:

答案 0 :(得分: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