日期:2014-05-18  浏览次数:20500 次

如何在存储过程中拷贝记录
有一个BOM表,表中很多记录的一个关键字(比如product_id)都是一样的值(比如都是“AAA”)
我想新建记录,直接从所有“AAA”的记录拷贝到“BBB”
用存储过程如何写?
SQL code

CREATE PROCEDURE [dbo].[CopyNode]
(
    @product_id varchar(25),
    @product_id_new varchar(25)
)
AS
declare @lft int
declare @rgt int
if 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



------解决方案--------------------
select * from BOM where product_id = @product_id
//如何写呢?
//insert into BOM where product_id = @product_id
:
insert into bom
select 除了product_id 列外的其他所有列,'BBB'
from bom where product_id='AAA'

注意上面product_id在字段列表中的位置.
------解决方案--------------------
SQL code

SET XACT_ABORT ON
BEGIN TRANSACTION
 insert into BOM
 select @product_id_new,[product_id外的其他字段] 
   from BOM where product_id = @product_id
COMMIT TRANSACTION

------解决方案--------------------
SQL code
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