【问题标题】:An expression of non-boolean type specified in a context where a condition is expected, near ')'在预期条件的上下文中指定的非布尔类型的表达式,靠近 ')'
【发布时间】:2012-10-14 20:26:52
【问题描述】:

如果 T_Referral 表上发生任何更新,我在触发器中有一个脚本来插入审计表,并且我正在使用 Decrypt 函数来解密其中一列中的数据。我在触发器中的代码是:

DECLARE @Sql_Insert nvarchar(max)
SET @Sql_Insert = ''
SET @Sql_Insert='INSERT INTO [Logg].AuditLogData(TableName, ColumnName, OldValue, OldValue_Decode, NewValue, NewValue_Decode, AuditSubCategoryID,[GuID])
select TableName, ColumnName, OldValue_Decode, case when ColumnName = ''BookingUserReferenceValue'' then utl.sfDecrypt(NewValue,0) Else NewValue end, NewValue_Decode, AuditSubCategoryID,[GuID] from #AuditLogData
where ISNULL(OldValue,'') != ISNULL([NewValue],'')'

exec utl.uspOpenOrCloseEncryptionKey 'open'
exec(@Sql_Insert )
exec utl.uspOpenOrCloseEncryptionKey 'close'

我的更新脚本是

Update RTS.T_Referral   
set BookingUserReferenceValue =  cast ('John Wayne' as varbinary(256))
where ReferralId = 20

我正在将记录更新为John Wayne 作为T_Referral 表中的varbinary(记录看起来像0x4A6F686E205761796E65),并且当调用更新触发器时,它将将该记录作为John Wayne 加载到审计表中。当记录插入 BookingUserReferenceValue 时,它​​将被加密。 BookingUserReferenceValue 列的数据类型是 Varbinary(256)。当我尝试更新该列中的记录时,出现错误:

在上下文中指定的非布尔类型的表达式 条件是预期的,在 ')' 附近。

任何想法有什么问题吗? 谢谢

【问题讨论】:

  • utl.sfDecrypt(NewValue,0) 和 NewValue 是否属于同一数据类型?

标签: sql sql-server-2008 tsql


【解决方案1】:

你需要转义单引号:

SET @Sql_Insert='
INSERT INTO [Logg].AuditLogData(TableName, ColumnName, OldValue, OldValue_Decode, NewValue, NewValue_Decode, AuditSubCategoryID,[GuID])
select 
    TableName, ColumnName, OldValue, OldValue_Decode, 
    case when ColumnName = ''BookingUserReferenceValue'' then utl.sfDecrypt(NewValue,0) Else NewValue end,
    NewValue_Decode, AuditSubCategoryID,[GuID] 
from #AuditLogData
where 
    ISNULL(OldValue, cast('''' as varbinary(256))) != 
    ISNULL([NewValue], cast('''' as varbinary(256)))
';

【讨论】:

  • 我收到错误不允许从数据类型 varchar 到 varbinary 的隐式转换。使用 CONVERT 函数运行此查询。
  • @mike 我想这只是缺少演员表。已编辑。
  • 我也尝试在更新脚本中使用转换,但它会引发同样的错误
  • 您的 SELECT 列表中是否缺少 OldValue?
  • 这不仅仅是你的错误。这是原始帖子中的错误。也许这就是错误的原因。
【解决方案2】:

如果我们在IF 语句、WHERE 子句、HAVING 子句等中编写非布尔表达式,则会出现语法错误。请检查您的代码。

【讨论】:

    【解决方案3】:

    我认为您的 SELECT 列表中缺少 OldValue。我不知道你为什么没有收到列数不匹配的错误。

    【讨论】:

    • 这肯定是一个错误。但我想首先它会尝试执行选择并被两个错误卡住:首先是缺少引号转义。其次是缺乏演员表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 2011-09-22
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多