INSERTED,UPDATED和DELETE表不记录NULL值

时间:2015-07-20 08:06:34

标签: sql-server triggers audit

我创建了一个表,其中一个表的列可以接受空值。这个表可以在下面找到

CREATE TABLE [dbo].[tbl_Locations](
    [location_ID] [int] IDENTITY(1,1) NOT NULL,
    [location_Code] [varchar](100) NULL,
    [location_Name] [varchar](250) NOT NULL,
 CONSTRAINT [PK_tbl_Locations] PRIMARY KEY CLUSTERED 
(
    [location_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

我为这个表创建了一个触发器,这样当我插入,更新或删除该表中的记录时,我将能够审核旧记录和新记录。以下是我正在使用的代码:

IF EXISTS(SELECT * FROM   inserted) 
      BEGIN 
          IF EXISTS(SELECT * FROM   deleted) 
            BEGIN 
                ...
            END 
          ELSE 
            BEGIN 
                SET @NewValues = (SELECT * FROM inserted FOR XML AUTO);
                SET @XXXID = (SELECT location_ID FROM inserted);
                SET @Type = 'INSERT'; -- Row has been inserted 
            END 
      END 
    ELSE 
      ...

问题是当我插入或更新记录并且列Code的值为null时,inserted表将只包含另外两列的值。换句话说,忽略空值:

<inserted location_ID="1" location_Name="Malta"/>

有没有办法如何获得空值,即我得到以下XML:

<inserted location_ID="1" location_Name="Malta"/>

提前致谢

0 个答案:

没有答案