1

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/存储过程从 tableB 输入行到 tableA。例如,当插入第一行“Home”时,它在 MastercategoryDe​​sc 中不存在,因此将在 MasterCategoryID 中插入“4”。第二行应在 MasterCategoryID 中再次保留“4”。下面的代码执行此操作,但是在第一行之后,所有行的 MastercategoryID 保持不变。我不知道如何在插入新行时跟踪 id。

ps 请不要回复说我需要使用 IDENTITY() 索引。我必须保持表结构相同并且不能更改它。谢谢

4

3 回答 3

1

创建一个带有字段的新表 your_table x_MasterCategoryDescx_SubCategoryDesc

在该表中插入所有值并运行以下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 小提琴

于 2013-06-21T12:13:43.413 回答
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

这个想法是在可用时从主表中获取信息。当不可用时,MasterCategoryIdNULL. 计算一个新的 id,row_number()用于生成序列号。然后将这些添加到先前的最大 id。

于 2013-06-21T11:39:06.003 回答
0

使用 CURSOR 来完成这项工作。游标循环遍历 TableA 的每一行,如果在 TableB 中找不到,则 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
于 2013-06-21T11:59:01.020 回答