在上下文中指定非布尔类型的表达式,其中的条件是预期的,邻近“)”(An expression of

2019-09-27 09:40发布

我有一个脚本触发插入审计表,如果任何更新发生在T_Referral表,我使用的是解密函数来解密列的一个数据。 我在扳机的代码是:

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在VARBINARY T_Referral表(记录类似于0x4A6F686E205761796E65 ),当更新触发器被调用时会加载那个纪录约翰·韦恩在审计表。 当记录插入BookingUserReferenceValue它将被加密。 柱BookingUserReferenceValue的数据类型是varbinary(256)。 当我尝试更新该列中我得到错误的记录:

在上下文中指定非布尔型,其中一个条件是预期的,邻近“)”的表达式。

任何想法什么错? 谢谢

Answer 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)))
';


Answer 2:

如果我们写在非布尔表达式IF语句, WHERE子句, HAVING子句等也谈到在语法错误。 请检查你的代码。



Answer 3:

我想是的OldValue从你的SELECT列表中缺少。 我不知道为什么你没有得到错误的列数不匹配。



文章来源: An expression of non-boolean type specified in a context where a condition is expected, near ')'