2011-03-10 52 views
0

我正在嘗試開發一個「文章」系統的過程中,數據庫中可能有一條文章條目,那麼儘可能多的文章數據鏈接到該ArticleData。我該如何重新組織SQL Server中的頁面順序(T-SQL)

現在我正在嘗試正確更新PageOrder。例如,我希望數字始終保持一致,如1到15。我不希望頁碼像這樣去。 1,1,3,6,8,9,10 ...它應該只是1,2,3,4,5,6,7。

我已經設法完成添加新記錄文章,因爲我插入一條新記錄,然後重新計算順序。問題是我想改變官方記錄。例如:

可以說我有5頁。
- 第1頁,
- 第2頁,
- 第3頁,
- 第4頁,
- 第5頁

可以說,我想第3頁成爲第5頁
- 第1頁,
- 第2頁,
- 第4頁,
- 第5頁,
- 第3頁

現在索引我希望能夠重新編制索引,以便它變成它應該是的樣子。

現在的問題是......如何在T-SQL過程(SQL Server 2005)中執行此操作?

ALTER PROCEDURE [dbo].[Admin_InsertOrUpdateArticle] 
    @Id int = null 
    ,@ArticleId int = null 
    ,@Header varchar(50) 
    ,@ParentId int = null 
    ,@ArticleType int 
    ,@DisplayOrder int = 0 
    ,@Content text 
    ,@ModifiedById int 
    ,@ModifiedBy varchar(50) 
    ,@ModifiedDate datetime 
    ,@Subject varchar(100) 
    ,@NewPage int 
AS 
BEGIN 

    SET NOCOUNT ON; 

    UPDATE 
     [Article] 
     SET 
      [Header] = @Header 
      ,[ParentId] = @ParentId 
      ,[DisplayOrder] = @DisplayOrder 
      ,[Type] = @ArticleType 
     WHERE 
      [Article].[Id] = @ArticleId 

    -- If Article does not exists then we don't have any record of even Article Data! 
    -- NEW RECORD! 
    IF(@@ROWCOUNT = 0) 
     BEGIN 

      INSERT INTO [Article] 
      (
       [Header] 
       ,[ParentId] 
       ,[DisplayOrder] 
       ,[Type] 
      ) 
      VALUES 
      (
       @Header 
       ,@ParentId 
       ,@DisplayOrder 
       ,@ArticleType 
      ); 

      INSERT INTO [ArticleData] 
      (
       [ArticleId] 
       ,[Content] 
       ,[CreatedBy] 
       ,[CreatedById] 
       ,[ModifiedBy] 
       ,[ModifiedById] 
       ,[PostDate] 
       ,[ModifiedDate] 
       ,[Subject] 
       ,[PageOrder] 
      ) 
      VALUES 
      (
       @@IDENTITY 
       ,@Content 
       ,@ModifiedBy 
       ,@ModifiedById 
       ,@ModifiedBy 
       ,@ModifiedById 
       ,@ModifiedDate 
       ,@ModifiedDate 
       ,@Subject 
       ,1 -- First Page!!! 
      ) 

     END 
    ELSE 
     BEGIN 

      -- We do have a Article Record, therefore we update the current page and check to see if it is a new page! 
      UPDATE 
       [ArticleData] 
       SET 
        [Content] = @Content 
        ,[Subject] = @Subject 
        ,[ModifiedBy] = @ModifiedBy 
        ,[ModifiedById] = @ModifiedById 
        ,[ModifiedDate] = @ModifiedDate 
       WHERE 
        [ArticleData].[Id] = @Id AND 
        [ArticleData].[ArticleId] = @ArticleId 

      -- Are we a new Article Data? 
      IF(@@ROWCOUNT = 0) 
      BEGIN 

       -- We are lets check to see if the page we want this article for exists 
       IF EXISTS (SELECT [PageOrder] FROM [ArticleData] WHERE [ArticleId] = @ArticleId AND [PageOrder] = @NewPage) 
       BEGIN 

        -- It does exists therefore we need to reorder the pages 
        UPDATE 
         [ArticleData] 
         SET 
          [PageOrder] = A.PageOrder + 1 
          FROM [ArticleData] A 
         WHERE 
          A.PageOrder >= @NewPage AND 
          [ArticleId] = @ArticleId 

        -- We now Insert The New data that we want at that page 
        INSERT INTO [ArticleData] 
        (
         [ArticleId] 
         ,[Content] 
         ,[CreatedBy] 
         ,[CreatedById] 
         ,[ModifiedBy] 
         ,[ModifiedById] 
         ,[PostDate] 
         ,[ModifiedDate] 
         ,[Subject] 
         ,[PageOrder] 
        ) 
        VALUES 
        (
         @ArticleId 
         ,@Content 
         ,@ModifiedBy 
         ,@ModifiedById 
         ,@ModifiedBy 
         ,@ModifiedById 
         ,@ModifiedDate 
         ,@ModifiedDate 
         ,@Subject 
         ,@NewPage 
        ) 

       END  
       ELSE 
       BEGIN 

        -- Since that page we want to add is not in reach we will simply add it to the very last page     
        INSERT INTO [ArticleData] 
        (
         [ArticleId] 
         ,[Content] 
         ,[CreatedBy] 
         ,[CreatedById] 
         ,[ModifiedBy] 
         ,[ModifiedById] 
         ,[PostDate] 
         ,[ModifiedDate] 
         ,[Subject] 
         ,[PageOrder] 
        ) 
        VALUES 
        (
         @ArticleId 
         ,@Content 
         ,@ModifiedBy 
         ,@ModifiedById 
         ,@ModifiedBy 
         ,@ModifiedById 
         ,@ModifiedDate 
         ,@ModifiedDate 
         ,@Subject 
         ,(SELECT COUNT(Id)+1 FROM [ArticleData] WHERE [ArticleId] = @ArticleId) -- Add new Page as Last Record 
        ) 

       END  

      END 
      ELSE 
      BEGIN 

       -- The article did update therefore lets reorder our pages based on the new page defined 
       -- Does this page in the article exists? IF it does lets update the pages 
       IF EXISTS (SELECT [PageOrder] FROM [ArticleData] WHERE [ArticleId] = @ArticleId AND [PageOrder] = @NewPage) 
       BEGIN 

        -- Update all pages with new numbers 
        UPDATE 
         [ArticleData] 
         SET 
          [PageOrder] = A.PageOrder + 1 
          FROM [ArticleData] A 
         WHERE 
          A.PageOrder >= @NewPage AND 
          [ArticleId] = @ArticleId 

        -- Update THIS article data with the new page number 
        UPDATE 
        [ArticleData] 
        SET 
         [PageOrder] = @NewPage 
        WHERE 
         [ArticleData].[Id] = @Id AND 
         [ArticleData].[ArticleId] = @ArticleId 

       END 
       ELSE 
       BEGIN 

        -- Page doesn't exists because what we want to change it to is to far out of range for example if there are only 5 pages, then the ordering will simply set this page to the last index 
        UPDATE 
        [ArticleData] 
        SET 
         [PageOrder] = (SELECT COUNT(Id) FROM [ArticleData] WHERE [Id] = @Id AND [ArticleId] = @ArticleId) 
        WHERE 
         [ArticleData].[Id] = @Id AND 
         [ArticleData].[ArticleId] = @ArticleId 

       END 

      END 

     END 

END 
+1

我不認爲試圖保持這些順序號順序真的很有意義 - 你只會導致大量不必要的更新....額外提示: t使用1,2,3,4作爲頁面順序 - 而不是使用10,20,30,40 - 那麼你會留下一些空間來「擠進」兩個現有頁面中間的新頁面,而不必重新編寫大量頁面!如果你真的需要一個順序號碼爲您的網頁按正確的順序,你總是可以看看'ROW_NUMBER()OVER(ORDER BY ....)'構造這是自動爲你做... – 2011-03-10 08:51:37

+0

哦,頁碼是通過發送新頁面的頁碼來自動完成。如果文章和ArticleData已經存在,那麼它將僅用該新的訂單號來更新該訂單。最壞的情況下,我將不得不在代碼中執行此操作... – Benjamin 2011-03-10 08:56:41

+0

你所要做的事沒有任何意義,因爲它可以說是所有'不按順序',所以你需要做的是寫一個查詢,它會爲你做一個命令。否則,請將頁碼設置爲主鍵,因爲表的物理佈局由主鍵確定。 – Ryk 2011-03-10 09:54:45

回答

3

重新編號的頁面比較簡單,但我不能告訴你是否會想插槽到您現有的代碼這一點,在這種情況下,不知道在哪裏,這將去:

DECLARE @OldPageID int 
DECLARE @NewPageID int 

set @OldPageID = 3 
set @NewPageID = 5 

UPDATE 
    Page 
SET 
    PageID = CASE 
     WHEN PageID = @OldPageID THEN @NewPageID ELSE 
     WHEN @OldPageID < @NewPageID THEN PageID -1 ELSE PageID + 1 END 
where 
    PageID between 
     CASE WHEN @OldPageID < @NewPageID THEN @OldPageID ELSE @NewPageID END 
     and 
     CASE WHEN @OldPageID < @NewPageID THEN @NewPageID ELSE @OldPageID END 
+0

感謝一百萬兄弟......我會發布我的整個事情只是爲了看看我把它放在哪裏=) – Benjamin 2011-03-10 17:53:36

0

這是我作品的最終結果

ALTER PROCEDURE [dbo].[Admin_InsertOrUpdateArticle] 
    @Id int = null 
    ,@ArticleId int = null 
    ,@Header varchar(50) 
    ,@ParentId int = null 
    ,@ArticleType int 
    ,@DisplayOrder int = 0 
    ,@Content text 
    ,@ModifiedById int 
    ,@ModifiedBy varchar(50) 
    ,@ModifiedDate datetime 
    ,@Subject varchar(100) 
    ,@NewPage int 
AS 
BEGIN 

    SET NOCOUNT ON; 

    UPDATE 
     [Article] 
     SET 
      [Header] = @Header 
      ,[ParentId] = @ParentId 
      ,[DisplayOrder] = @DisplayOrder 
      ,[Type] = @ArticleType 
     WHERE 
      [Article].[Id] = @ArticleId 

    -- If Article does not exists then we don't have any record of even Article Data! 
    -- NEW RECORD! 
    IF(@@ROWCOUNT = 0) 
     BEGIN 

      INSERT INTO [Article] 
      (
       [Header] 
       ,[ParentId] 
       ,[DisplayOrder] 
       ,[Type] 
      ) 
      VALUES 
      (
       @Header 
       ,@ParentId 
       ,@DisplayOrder 
       ,@ArticleType 
      ); 

      INSERT INTO [ArticleData] 
      (
       [ArticleId] 
       ,[Content] 
       ,[CreatedBy] 
       ,[CreatedById] 
       ,[ModifiedBy] 
       ,[ModifiedById] 
       ,[PostDate] 
       ,[ModifiedDate] 
       ,[Subject] 
       ,[PageOrder] 
      ) 
      VALUES 
      (
       @@IDENTITY 
       ,@Content 
       ,@ModifiedBy 
       ,@ModifiedById 
       ,@ModifiedBy 
       ,@ModifiedById 
       ,@ModifiedDate 
       ,@ModifiedDate 
       ,@Subject 
       ,1 -- First Page!!! 
      ) 

     END 
    ELSE 
     BEGIN 

      -- We do have a Article Record, therefore we update the current page and check to see if it is a new page! 
      UPDATE 
       [ArticleData] 
       SET 
        [Content] = @Content 
        ,[Subject] = @Subject 
        ,[ModifiedBy] = @ModifiedBy 
        ,[ModifiedById] = @ModifiedById 
        ,[ModifiedDate] = @ModifiedDate 
       WHERE 
        [ArticleData].[Id] = @Id AND 
        [ArticleData].[ArticleId] = @ArticleId 

      -- Are we a new Article Data? 
      IF(@@ROWCOUNT = 0) 
      BEGIN 

       -- We are lets check to see if the page we want this article for exists 
       IF EXISTS (SELECT [PageOrder] FROM [ArticleData] WHERE [ArticleId] = @ArticleId AND [PageOrder] = @NewPage) 
       BEGIN 

        -- It does exists therefore we need to reorder the pages 
        UPDATE 
         [ArticleData] 
         SET 
          [PageOrder] = A.PageOrder + 1 
          FROM [ArticleData] A 
         WHERE 
          A.PageOrder >= @NewPage AND 
          [ArticleId] = @ArticleId 

        -- We now Insert The New data that we want at that page 
        INSERT INTO [ArticleData] 
        (
         [ArticleId] 
         ,[Content] 
         ,[CreatedBy] 
         ,[CreatedById] 
         ,[ModifiedBy] 
         ,[ModifiedById] 
         ,[PostDate] 
         ,[ModifiedDate] 
         ,[Subject] 
         ,[PageOrder] 
        ) 
        VALUES 
        (
         @ArticleId 
         ,@Content 
         ,@ModifiedBy 
         ,@ModifiedById 
         ,@ModifiedBy 
         ,@ModifiedById 
         ,@ModifiedDate 
         ,@ModifiedDate 
         ,@Subject 
         ,@NewPage 
        ) 

       END  
       ELSE 
       BEGIN 

        -- Since that page we want to add is not in reach we will simply add it to the very last page     
        INSERT INTO [ArticleData] 
        (
         [ArticleId] 
         ,[Content] 
         ,[CreatedBy] 
         ,[CreatedById] 
         ,[ModifiedBy] 
         ,[ModifiedById] 
         ,[PostDate] 
         ,[ModifiedDate] 
         ,[Subject] 
         ,[PageOrder] 
        ) 
        VALUES 
        (
         @ArticleId 
         ,@Content 
         ,@ModifiedBy 
         ,@ModifiedById 
         ,@ModifiedBy 
         ,@ModifiedById 
         ,@ModifiedDate 
         ,@ModifiedDate 
         ,@Subject 
         ,(SELECT COUNT(Id)+1 FROM [ArticleData] WHERE [ArticleId] = @ArticleId) -- Add new Page as Last Record 
        ) 

       END  

      END 
      ELSE 
      BEGIN 

       -- The article did update therefore lets reorder our pages based on the new page defined   
       DECLARE @OldPage int 
       SET @OldPage = (SELECT [PageOrder] FROM [ArticleData] WHERE [ArticleId] = @ArticleId AND [Id] = @Id) -- Get the old Page from the Id we have 

       IF (@OldPage != @NewPage) 
       BEGIN 
        UPDATE 
         [ArticleData] 
        SET 
         PageOrder = CASE 
           WHEN PageOrder = @OldPage THEN @NewPage 
           WHEN @OldPage < @NewPage THEN PageOrder -1 ELSE PageOrder + 1 END 
        WHERE 
         PageOrder BETWEEN 
          CASE WHEN @OldPage < @NewPage THEN @OldPage ELSE @NewPage END 
          AND 
          CASE WHEN @OldPage < @NewPage THEN @NewPage ELSE @OldPage END 
       END 

      END 

     END 

END