2016-03-17 75 views
1

以下存儲過程的目標是從其他位置表填充位置表。當程序執行時,我發現記錄有很多重複。我已經多次閱讀代碼。找不到任何問題。使用存儲過程循環訪問表

存儲過程:

ALTER PROCEDURE [dbo].[sp_Populate_Location] 
    @school_id int 
AS 
    DECLARE @country_id int, @country_id_new int, @country_name nvarchar(50) 
    DECLARE @state_id int, @state_id_new int, @state_name nvarchar(50) 
    DECLARE @town_city_id int, @town_city_name nvarchar(50) 
    DECLARE @local_government_id int, @local_government_name nvarchar(50) 

    DECLARE @country TABLE (
         idx smallint Primary Key IDENTITY(1,1), 
         country_id int, 
         country_name nvarchar(50) 
        ) 

    DECLARE @state TABLE (
         idx smallint Primary Key IDENTITY(1,1), 
         state_id int, 
         country_id int, 
         state_name nvarchar(50) 
        ) 

    DECLARE @town_city TABLE (
         idx smallint Primary Key IDENTITY(1,1), 
         town_city_name nvarchar(50) 
        ) 

    DECLARE @local_government TABLE (
         idx smallint Primary Key IDENTITY(1,1), 
         local_government_name nvarchar(50) 
        ) 

    -- populate country table 
    INSERT @country 
     SELECT CountryId, CountryName 
     FROM tbl_Country 
     WHERE IsActive = 1 

    -- enumerate the table 
    DECLARE @icount_1 int 
    DECLARE @numrows_country int 

    SET @icount_1 = 1 
    SET @numrows_country = (SELECT COUNT(*) FROM @country) 

    IF @numrows_country > 0 

    WHILE (@icount_1 <= (SELECT MAX(idx) FROM @country)) 
    BEGIN 
     -- get the next country primary key 
     SET @country_id = (SELECT country_id FROM @country WHERE idx = @icount_1) 
     SET @country_name = (SELECT country_name FROM @country WHERE idx = @icount_1) 

     INSERT tbl_SchoolCountry(CountryName, IsActive, CreatedBy, CreatedOn, SchoolId) 
     VALUES(@country_name, 1, 1, GETDATE(), @school_id); 

     SET @country_id_new = (SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]) 

     INSERT @state 
      SELECT StateId, CountryId, StateName 
      FROM tbl_State 
      WHERE CountryId = @country_id 

     DECLARE @icount_2 int 
     DECLARE @numrows_state int 

     SET @icount_2 = 1 
     SET @numrows_state = (SELECT COUNT(*) FROM @state) 

     IF @numrows_state > 0 
     WHILE (@icount_2 <= (SELECT MAX(idx) FROM @state)) 
     BEGIN 

      SET @state_id = (SELECT state_id FROM @state WHERE idx = @icount_2) 
      SET @state_name = (SELECT state_name FROM @state WHERE idx = @icount_2) 

      INSERT tbl_SchoolState(CountryId, StateName, IsActive, CreatedBy, CreatedOn, SchoolId) 
      VALUES(@country_id_new, @state_name, 1, 1, GETDATE(), @school_id); 

      SET @state_id_new = (SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]) 

      -- Town and City 
      INSERT @town_city 
       SELECT TownCityName 
       FROM tbl_TownCity 
       WHERE StateId = @state_id AND IsActive = 1 

      DECLARE @icount_3 int 
      DECLARE @numrows_town_city int 

      SET @icount_3 = 1 
      SET @numrows_town_city = (SELECT COUNT(*) FROM @town_city) 

      IF @numrows_town_city > 0 
      WHILE (@icount_3 <= (SELECT MAX(idx) FROM @town_city)) 
      BEGIN 
       SET @town_city_name = (SELECT town_city_name FROM @town_city WHERE idx = @icount_3) 

       INSERT tbl_SchoolTownCity(CountryId, StateId, TownCityName, IsActive, CreatedBy, CreatedOn, SchoolId) 
       VALUES(@country_id_new, @state_id_new, @town_city_name, 1, 1, GETDATE(), @school_id); 

       SET @icount_3 = @icount_3 + 1 
      END 

    -- LGA 

    INSERT @local_government 
    SELECT LocalGovernmentName FROM tbl_LocalGovernment where StateId = @state_id and IsActive = 1 

    DECLARE @icount_4 int 
    DECLARE @numrows_lga int 

    SET @icount_4 = 1 
    SET @numrows_lga = (SELECT COUNT(*) FROM @local_government) 
    IF @numrows_lga > 0 
    WHILE (@icount_4 <= (SELECT MAX(idx) FROM @local_government)) 
    BEGIN 

    SET @local_government_name = (SELECT local_government_name FROM @local_government WHERE idx = @icount_4) 

    INSERT tbl_SchoolLocalGovernment(CountryId, StateId, LocalGovernmentName, IsActive, CreatedBy, CreatedOn, SchoolId) 
    VALUES(@country_id_new, @state_id_new, @local_government_name,1,1,GETDATE(),@school_id); 

    SET @icount_4 = @icount_4 + 1 
    END 

    SET @icount_2 = @icount_2 + 1 
    END 
     -- increment counter for next country 
     SET @icount_1 = @icount_1 + 1 
    END 
+1

調試您的查詢,檢查每個塊的輸出 – Sherlock

+0

很多重複在哪個表 - 所有表,一些表或一個表?您是否有任何樣本數據 - 在執行此代碼之前和之後? – AKS

+0

我在傳輸數據的表中有示例數據。我想在執行後有相同的記錄。 – uikrosoft

回答

0

我發現了什麼問題。臨時表是在插入它們之前聲明的。

前:

DECLARE @country TABLE (
         idx smallint Primary Key IDENTITY(1,1), 
         country_id int, 
         country_name nvarchar(50) 
        ) 

    DECLARE @state TABLE (
         idx smallint Primary Key IDENTITY(1,1), 
         state_id int, 
         country_id int, 
         state_name nvarchar(50) 
        ) 

    DECLARE @town_city TABLE (
         idx smallint Primary Key IDENTITY(1,1), 
         town_city_name nvarchar(50) 
        ) 

    DECLARE @local_government TABLE (
         idx smallint Primary Key IDENTITY(1,1), 
         local_government_name nvarchar(50) 
        ) 

後:

DECLARE @country TABLE (
    idx smallint Primary Key IDENTITY(1,1) 
    , country_id int, country_name nvarchar(50) 
    ) 

    -- populate country table 
    INSERT @country 
    SELECT CountryId, CountryName FROM tbl_Country where IsActive = 1 
. 
. 
. 
. 
. 
. 
. 
    DECLARE @state TABLE (
    idx smallint Primary Key IDENTITY(1,1) 
    , state_id int, country_id int, state_name nvarchar(50) 
    ) 

    INSERT @state 
    SELECT StateId, CountryId, StateName FROM tbl_State where CountryId = @country_id 
. 
. 
. 
. 
. 
. 
    DECLARE @town_city TABLE (
    idx smallint Primary Key IDENTITY(1,1) 
    , town_city_name nvarchar(50) 
    ) 

    INSERT @town_city 
    SELECT TownCityName FROM tbl_TownCity where StateId = @state_id and IsActive = 1 
. 
. 
. 
. 
. 
. 
    DECLARE @local_government TABLE (
    idx smallint Primary Key IDENTITY(1,1) 
    , local_government_name nvarchar(50) 
    ) 
    INSERT @local_government 
    SELECT LocalGovernmentName FROM tbl_LocalGovernment where StateId = @state_id and IsActive = 1 

通過這樣做,它會清除之前,新的環路臨時表。