2

我在 AWS RDS 数据库服务中有一个 SQL Server 2012 Web 版。排序规则是默认的:SQL_Latin1_General_CP1_CI_AS.

在其中一个表中,我有一列MODIFIED_BY类型varchar(128)- 列排序规则是默认的。

除了少数硬编码为System. 正如您所知道的,它是一个用于存储审计跟踪事务的表,我们在其中存储 id,或者System是否在迁移过程中进行了修改。

无论如何-以上所有内容都只是上下文。现在奇怪的问题:

如果我运行以下命令,我不会得到任何记录,尽管我应该有:

select *
from AUDIT_LOG_TRANSACTIONS alt
where lower(ltrim(rtrim(alt.MODIFIED_BY)))='system'

或者

select *
from AUDIT_LOG_TRANSACTIONS alt
where lower(ltrim(rtrim(alt.MODIFIED_BY)))=convert(varchar,'system')

或者

select *
from AUDIT_LOG_TRANSACTIONS alt
where lower(ltrim(rtrim(alt.MODIFIED_BY)))=convert(varchar(128),'system')

如果我运行以下命令,我会得到记录:

select *
from AUDIT_LOG_TRANSACTIONS alt
where alt.MODIFIED_BY like '%System%'

或者

select *
from AUDIT_LOG_TRANSACTIONS alt
where lower(ltrim(rtrim(CAST(alt.MODIFIED_BY AS nvarchar(max)))))='system'

更新
这是表创建脚本:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[AUDIT_LOG_TRANSACTIONS](
    [AUDIT_LOG_TRANSACTION_ID] [int] IDENTITY(1,1) NOT NULL,
    [DATABASE] [nvarchar](128) NOT NULL,
    [TABLE_NAME] [nvarchar](261) NOT NULL,
    [TABLE_SCHEMA] [nvarchar](261) NOT NULL,
    [AUDIT_ACTION_ID] [tinyint] NOT NULL,
    [HOST_NAME] [varchar](128) NOT NULL,
    [APP_NAME] [varchar](128) NOT NULL,
    [MODIFIED_BY] [varchar](128) NOT NULL,
    [MODIFIED_DATE] [datetime] NOT NULL,
    [AFFECTED_ROWS] [int] NOT NULL,
    [SYSOBJ_ID]  AS (object_id([TABLE_NAME])),
PRIMARY KEY CLUSTERED 
(
    [AUDIT_LOG_TRANSACTION_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]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[AUDIT_LOG_TRANSACTIONS] ADD  DEFAULT (db_name()) FOR [DATABASE]
GO

ALTER TABLE [dbo].[AUDIT_LOG_TRANSACTIONS] ADD  CONSTRAINT [DF_AUDIT_LOG_TRANSACTIONS_MODIFIED_BY]  DEFAULT ('System') FOR [MODIFIED_BY]
GO
4

1 回答 1

2

你的字符串中可能有一些“难以看到”的字符吗?为了消除这种可能性,我会运行这样的查询:

select alt.*, LEN(alt.MODIFIED_BY) as 'Length', cast(alt.MODIFIED_BY as varbinary(max)) as 'Bytes' from AUDIT_LOG_TRANSACTIONS alt where alt.MODIFIED_BY like '%System%' 

检查 Length 和 Bytes 列是否符合您的预期。

于 2013-11-13T00:15:27.163 回答