我有一张包含产品交付信息的表格。我得到每日文件,附加到此表。但是,如果导入出现问题,我不能简单地重新导入同一个文件,因为它已经导入了一些数据然后我会重复。
我正在考虑添加校验和/哈希行,以便我可以比较这些行并省略已有的东西。
表格如下(简化):
我想添加一行“校验和”,它将是行ordernr,item,itemcount和deliverydate的组合哈希。由于表格相当大(1m +行),我甚至更喜欢右(hash,8),因此varchar(8)足以用于校验和行。
到目前为止,我到了这里:
SELECT o1.rdid, HASHBYTES('md5', (
SELECT ordernr, item, deliverydate, itemcount
FROM shop_tt_deliveries_uk o2
WHERE o2.rdid = o1.rdid FOR XML RAW))
FROM shop_tt_deliveries_uk o1
产生哈希的喜欢这样:
rdid (No column name)
98851 0xA819EEB0B7463DCB11AE8170879F867C
98852 0x73DB5FE112707BB3D9564DE063882F30
98853 0x3F81DBCED4D6B200CD605BC774FD5EB4
98854 0xF388A700995F8B589AE31CFE0AEF9568
但已经在奇怪的东西中添加了right()结果(我希望第一行的'879F867C'):
rdid (No column name)
98851 ®p‡Ÿ†|
98852 ÙVMàcˆ/0
98853 Í`[Çtý^´
98854 šãþ
并且更新表根本不起作用:
UPDATE shop_tt_deliveries_uk d
SET checksum = HASHBYTES('md5', (
SELECT ordernr, item, deliverydate, itemcount
FROM shop_tt_deliveries_uk o2
WHERE o2.rdid = o1.rdid ))
WHERE checksum IS NULL
任何见解?
答案 0 :(得分:0)
declare @source varbinary(max), @id int
declare chk cursor for SELECT o1.rdid, HASHBYTES('md5', (SELECT ordernr, item, deliverydate, itemcount FROM shop_tt_deliveries_uk o2 WHERE o2.rdid = o1.rdid FOR XML RAW)) FROM shop_tt_deliveries_uk o1
open chk
fetch next from chk into @id, @source
while (@@fetch_status = 0)
begin
select @source = right(cast('' as xml).value('xs:hexBinary(sql:variable("@source"))', 'varchar(max)'),8)
update shop_tt_deliveries_uk d SET checksum = @source WHERE checksum IS NULL //put your more conditions
fetch next from chk into @id, @source
end