在保持ID的同时在表中插入行

时间:2013-06-21 11:30:46

标签: sql tsql


TABLEA
MasterCategoryID    MasterCategoryDesc
1                   Housing
1                   Housing 
1                   Housing 
2                   Car
2                   Car
2                   Car
3                   Shop   

TABLEB
ID                  Description
1                   Home
2                   Home
3                   Plane
4                   Car

INSERT into TableA
(
    [MasterCategoryID]
    [MasterCategoryDesc]
)
Select
     case when (Description) not in (select MasterCategoryDesc from TableA) 
        then (select max(MasterCategoryID)+1 from TableA)
        else (select top 1 MasterCategoryID from TableA where MasterCategoryDesc = Description)
     end as [MasterCategoryID]

    ,Description as MasterCategoryDesc
from TableB

我想使用SQL / Stored Procedure从tableB到tableA输入行。例如,当插入第一行'Home'时,它在MastercategoryDe​​sc中不存在,因此将在MasterCategoryID中插入'4'。第二行应该再次在MasterCategoryID中保留'4'。 下面的代码执行它然而在第一行之后,MastercategoryID对于所有行保持相同。我不知道如何在插入新行时跟踪id。

P.S。请不要回答说我需要使用IDENTITY()索引。我必须保持表结构相同,不能改变它。谢谢

3 个答案:

答案 0 :(得分:1)

使用字段x_MasterCategoryDescx_SubCategoryDesc

创建一个新表your_table

在该表中插入所有值,然后运行以下 SP

CREATE PROCEDURE x_experiment
AS
BEGIN

    IF object_id('TEMPDB..#TABLES') IS NOT NULL
    BEGIN
    DROP TABLE #TABLES
    END

    DECLARE @ROWCOUNT INT
    DECLARE @ROWINDEX INT =0,
    @MasterCategoryDesc VARCHAR(256),            
    @SubCategoryDesc VARCHAR(256)

    select IDENTITY(int,1,1) as ROWID,* 
    into #TABLES 
    From your_table

    SELECT @ROWCOUNT=COUNT(*) from #TABLES --where ROWID between 51 and 100

    WHILE (@ROWINDEX<@ROWCOUNT)
    BEGIN
        set @ROWINDEX=@ROWINDEX+1

        Select 
                @MasterCategoryDesc=x_MasterCategoryDesc,
                @SubCategoryDesc=x_SubCategoryDesc
        from #TABLES t
        where rowid = @ROWINDEX

        INSERT into Table1
            ([MasterCategoryID], [MasterCategoryDesc], [SubCategoryDesc], [SubCategoryID])
        select TOP 1
            case when @MasterCategoryDesc not in (select [MasterCategoryDesc] from Table1) 
                then (select max([MasterCategoryID])+1 from Table1)
                else (select distinct max([MasterCategoryID]) from Table1 
                    where [MasterCategoryDesc]=@MasterCategoryDesc
                    group by [MasterCategoryID]) 
            end as [MasterCategoryID]
            ,@MasterCategoryDesc as [MasterCategoryDesc]
            ,@SubCategoryDesc as [SubCategoryDesc]
            ,case when @SubCategoryDesc not in (select [SubCategoryDesc] from Table1) 
                then (select max([SubCategoryID])+1 from Table1 )
                else (select max([SubCategoryID]) from Table1 
                    where [SubCategoryDesc]=@SubCategoryDesc
                    group by [SubCategoryID]) 
            end as [SubCategoryID]

            from Table1
        END
        select * from Table1 order by MasterCategoryID

END
GO

exec x_experiment --SP Execute

<强> SQL FIDDLE

答案 1 :(得分:0)

您的数据结构有待改进。您不应该有一个具有重复值的主ID列。

但你仍然可以做你想做的事:

INSERT into TableA ([MasterCategoryID], [MasterCategoryDesc])
Select coalesce(a.MasterCategoryId,
                amax.maxid + row_number() over (partition by (a.MasterCategoryId) order by b.id)
               ),
       coalesce(a.MasterCategoryDesc, b.desc)
from TableB b left outer join
     (select desc, max(MasterCaegoryId) as maxid
      from TableA a
      group by desc
     ) a
     on b.desc = a.desc  left outer join
     (select max(MasterCategoryID) as maxid
      from TableA
     ) amax

这个想法是在主表可用时从主表中获取信息。如果不可用,则MasterCategoryId将为NULL。计算新ID,使用row_number()生成序列号。然后将它们添加到先前的最大ID。

答案 2 :(得分:0)

使用CURSOR完成工作。如果在TableB中找不到,则游标循环遍历TableA的每一行,并且MasterCategoryID增加。这是在将下一行TableA加载到游标中之前发生的......

DECLARE @ID int
DECLARE @Description VARCHAR(MAX)

DECLARE my_cursor CURSOR FOR 
  SELECT ID, Description FROM TableB
OPEN my_cursor

FETCH NEXT FROM my_cursor
INTO @ID, @Description

WHILE @@FETCH_STATUS = 0
BEGIN
    INSERT into TableA(MasterCategoryID, MasterCategoryDesc)
    SELECT CASE WHEN @Description NOT IN (SELECT MasterCategoryDesc FROM TableA) 
           THEN (SELECT MAX(MasterCategoryID)+1 FROM TableA)
           ELSE (SELECT TOP 1 MasterCategoryID 
                 FROM TableA 
                 WHERE MasterCategoryDesc = @Description)
           END AS MasterCategoryID, Description as MasterCategoryDesc
    FROM TableB
    WHERE ID = @ID

    FETCH NEXT FROM my_cursor
    INTO @ID, @Description
END