存储过程插入时自增ID关联多过滤器的问题解决方法
问题场景与需求
我有两个数据表,需要通过如下存储过程完成数据插入操作:
现有存储过程
ALTER PROCEDURE [dbo].[DeviceInvoiceInsert] @dt AS DeviceInvoiceArray READONLY AS DECLARE @customerDeviceId BIGINT DECLARE @customerId BIGINT DECLARE @filterChangeDate DATE BEGIN SET @customerId = (SELECT TOP 1 CustomerId FROM @dt WHERE CustomerId IS NOT NULL) SET @filterChangeDate = (SELECT TOP 1 filterChangeDate FROM @dt) INSERT INTO CustomerDevice (customerId, deviceId, deviceBuyDate, devicePrice) SELECT customerId, deviceId, deviceBuyDate, devicePrice FROM @dt WHERE CustomerId IS NOT NULL SET @customerDeviceId = SCOPE_IDENTITY() INSERT INTO FilterChange (customerId, filterId, customerDeviceId, filterChangeDate) SELECT @customerId, dt.filterId, @customerDeviceId, @filterChangeDate FROM @dt AS dt END
当前问题
执行该存储过程向FilterChange表插入数据时,@customerDeviceId始终只能获取到CustomerDevice表中最后一条插入记录的自增ID,无法正确关联对应设备的所有过滤器。
补充说明
此前收到@T N的解答,但该方案仅支持一个设备对应一个过滤器的场景,而我的实际需求是一个设备需要对应多个过滤器,请问该如何解决这个问题?
内容的提问来源于stack exchange,提问作者Reza Paidar
相关产品推荐
相关产品推荐

