2013-06-21 77 views
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 

我想使用從tableB到tableA的SQL/Stored Procedure輸入行。例如,當插入第一行'Home'時,它不存在於MastercategoryDe​​sc中,因此將在MasterCategoryID中插入'4'。第二行應該在MasterCategoryID中再次保留'4'。 下面的代碼是在第一行之後,MastercategoryID對所有行保持不變。我不知道如何在插入新行時跟蹤id。在保留ID的同時在表格中插入行

p.s.請不要回復說我需要使用IDENTITY()索引。我必須保持表結構相同,不能改變它。謝謝

+0

的[TSQL插入記錄和追蹤ID]可能重複(http://stackoverflow.com/questions/17194722/tsql-inserting-records-and-track-id) – Luv

+0

我想你應該重新命名Desc字段,我想是Description,因爲Desc是保留的並且令人困惑。 – christiandev

+0

嗨Luv - 你幫我解決了上一個問題,但是我需要同時插入多個記錄,而不是1個(手動)。如代碼所示。我將不勝感激,如果你能再次幫助我 –

回答

0

使用CURSOR來完成這項工作。如果在TableB中找不到TableC,則遊標循環遍歷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 
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。

+0

謝謝你會試試看 –

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 @[email protected]+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][email protected] 
        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][email protected] 
        group by [SubCategoryID]) 
      end as [SubCategoryID] 

      from Table1 
     END 
     select * from Table1 order by MasterCategoryID 

END 
GO 

exec x_experiment --SP Execute 

SQL FIDDLE

相關問題