根据临时表修改主表的某字段数据根据主表的主键

需求:因为主表的Unit字段有些为空,所以需要根据整理出的Excel表(SKUID字段和包装单位字段)将主表的Unit字段补全!

主表:

临时表:

前提:是将Excel表中的数据导入临时表;本过程不在赘述;

begin
	declare @SkuId int
	declare @unit varchar(50)

	declare My_Cursor cursor
	for(select SkuId,Unit from [dbo].[GoodsStockKeepingUnit1])
	open My_Cursor
	fetch next from My_Cursor into @SkuId,@unit;--读取第一行数据
	while @@FETCH_STATUS = 0
		begin
			update [dbo].[GoodsStockKeepingUnit] set Unit=@unit where SkuId=@SkuId; --更新
			fetch next from My_Cursor into @SkuId,@unit;--读取下一行数据
		end
	close My_Cursor --关闭游标
	deallocate My_Cursor --释放游标
end
go

  

原文地址:https://www.cnblogs.com/luoshengjie/p/10196811.html