如何在存储过程中拷贝记录
有一个BOM表,表中很多记录的一个关键字(比如product_id)都是一样的值(比如都是“AAA”)
我想新建记录,直接从所有“AAA”的记录拷贝到“BBB”
用存储过程如何写?
CREATE PROCEDURE [dbo].[CopyNode]( @product_id varchar(25), @product_id_new varchar(25))ASdeclare @lft intdeclare @rgt intif exists(select product_id from BOM where product_id = @product_id) begin SET XACT_ABORT ON BEGIN TRANSACTION select * from BOM where product_id = @product_id //如何写呢? //insert into BOM where product_id = @product_id COMMIT TRANSACTION SET XACT_ABORT OFF end
SET XACT_ABORT ONBEGIN TRANSACTION insert into BOM select @product_id_new,[product_id外的其他字段] from BOM where product_id = @product_idCOMMIT TRANSACTION
[解决办法]
insert into BOM select @product_id_new,其他字段 from BOM where product_id = @product_id
[解决办法]
SET XACT_ABORT ON
BEGIN TRANSACTION
insert into BOM
select @product_id_new,[product_id外的其他字段]
from BOM where product_id = @product_id
COMMIT TRANSACTION