2012-10-14 38 views
1

如果在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为varbinary在T_Referral表(记录类似于0x4A6F686E205761796E65),当更新触发器称它会将该记录加载到审计表中的John Wayne。当记录被插入到BookingUserReferenceValue中时,它将被加密。列BookingUserReferenceValue的数据类型是Varbinary(256)。当我尝试更新该列中的记录,我得到错误:

在指定的上下文非布尔类型,其中一个 状况的预期,附近“)”的一种表现。

任何想法什么是错的? 感谢

+0

utl.sfDecrypt(NewValue,0)和NewValue的数据类型是否相同? – GilM

回答

5

你需要逃脱单引号:

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))) 
'; 
+0

我收到错误不允许将数据类型varchar隐式转换为varbinary。使用CONVERT函数来运行此查询。 – mike

+0

@mike我想这只是缺乏铸造。编辑。 –

+0

我试图在更新脚本中使用转换,但它会抛出相同的错误 – mike

0

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

+0

这是肯定的错误。但我想首先它会尝试执行选择并陷入两个错误:首先缺乏引号转义。其次缺少铸造。 –

1

如果我们写在IF声明,WHERE从句,从句HAVING等它是非布尔表达式来下的语法错误。请检查您的代码。

相关问题