【发布时间】:2011-10-25 15:56:47
【问题描述】:
类似以下字段的表:ID、InvoiceDate、SystemEntryDate、InvoiceNo、GrossTotal、Hash。 对于 ID=1 的记录,使用 SignByAsymKey 函数使用记录 1 中的所有字段计算 Hash。
对于所有其他记录 (ID>2),使用相同的函数 SignByAsymKey 计算哈希,记录中的所有字段和上一条记录中的哈希。
例如,对于 ID=3,哈希是使用该记录的 InvoiceDate、SystemEntryDate、InvoiceNo、GrossTotal 字段以及 ID=2 记录的哈希计算的。
我该怎么做?使用触发器还是存储过程更好?
我做了一个这样的更新触发器,但它为所有记录提供相同的哈希>1:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[Invoice_update]
ON [dbo].[Invoice]
AFTER update
AS
BEGIN
SET NOCOUNT ON;
update dbo.Invoice
set [hash]=
(Case
when
dbo.Invoice.ID=1
Then
SignByAsymKey (
AsymKey_Id ('SecureAsymmetricKeyINVOICE'),
CONVERT([nvarchar],(select [InvoiceDate] from Invoice where [ID]=1),0)+';'+
CONVERT([nvarchar],(select [SystemEntryDate] from Invoice where [ID]=1),0)+';'+
CONVERT([nvarchar],(select [InvoiceNo] from Invoice where [ID]=1),0)+';'+
CONVERT([nvarchar],(select [GrossTotal] from Invoice where [ID]=1),0)
,N'Portal2011!')
else
SignByAsymKey (
AsymKey_Id ('SecureAsymmetricKeyINVOICE'),
CONVERT([nvarchar],inserted.[InvoiceDate],0)+';'+
CONVERT([nvarchar],inserted.[SystemEntryDate],0)+';'+
CONVERT([nvarchar],inserted.[InvoiceNo],0)+';'+
CONVERT([nvarchar],inserted.[GrossTotal],0)+';'+
CONVERT([nvarchar],
(select [hash] from dbo.Invoice
where [id]=inserted.id-1),0)
,N'Portal2011!')
End)
from inserted
End
【问题讨论】:
标签: sql sql-server-2008 calculated-columns