sql server中联合两个表update数据

该博客介绍了如何在SQL Server中联合两个表进行数据更新。首先创建了一个新表temp2,将原表T_wStock的数据复制过去。然后通过JOIN操作找出T_wStock中与temp1不匹配的记录,根据条件更新T_wStock表的GoodsStyle字段。最后展示了一些查询语句来检查更新后的数据和原始数据的对应情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

INSERT INTO temp2 --新建一个表,表结构跟原表结构一样,并表原表中的数据插入进去,再做测试
SELECT WarehouseTaskCode, CustCode, GoodsCode, GoodsName, GoodsModel, GoodsPackNum, GoodsPieceNum, GoodsCubage, GoodsWeight, GoodsGWeight, WarehousePosition, GoodsLock, InStockDate, GoodsPrice, GoodsTotalPrice, DamageTag, InStockType, ExpectPieceNum, ExpectCubage, ExpectWeight, ExpectGWeight, NeatenID, Recorder, RecordDate, Modifier, ModifyDate, OriginalCountry, BarcodeNum, BarcodePrefix, GoodsLatchCode, GoodsProductiondate, GateWayCode, GateWayDate, GoodsStyle, BelongToWarehouse, FinanceYear, FinanceMonth, GoodsPackUnit, GoodsPieceUnit, BargainValuta, EradioCode, CIQGoodsFNum, CIQGoodsSNum, XmlCol, Reserve_One, Reserve_Two, Reserve_Three, ZeroDate, GoodsType, DamageTagLevel FROM T_wStock

select COUNT(1) FROM T_wStock
select COUNT(1) FROM temp2

select COUNT(1) FROM temp2

select GoodsCode,GoodsStyle ,htype from 
temp2 inner join temp1 on temp2.GoodsCode = dbo.temp1.num
where temp2.CustCode='GD01' and dbo.temp2.GoodsStyle!=dbo.temp1.htype
GROUP BY GoodsCode,GoodsStyle ,htype

update T_wStock set T_wStock.GoodsStyle = temp1.htype from
T_wStock,temp1 where T_wStock.GoodsCode = temp1.num and T_wStock.CustCode='GD01' 

SELECT * FROM dbo.T_wStock

select * from T_wStock where CustCode='GD01' and GoodsCode in (
select num from temp1)

select * from temp1

select GoodsCode,GoodsStyle ,htype from 
T_wStock inner join temp1 on T_wStock.GoodsCode = dbo.temp1.num
where T_wStock.CustCode='GD01' and dbo.T_wStock.GoodsStyle!=dbo.temp1.htype
GROUP BY GoodsCode,GoodsStyle ,htype

SELECT GoodsCode,COUNT(1) 
FROM dbo.T_wStock
WHERE GoodsCode IN(SELECT num FROM dbo.temp1)
GROUP BY GoodsCode

select num,COUNT(1) from temp1 group by num

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值