2013-01-22 85 views
0

我有一個SQL Server過程,我需要它轉換在MySQL工作。怎麼寫 「WITH CTE AS(SELECT ROW_NUMBER)OVER(ORDER(BY CASE」 在MySQL程序

下面是我的MySQL轉換的過程,我試過,但它不是完整的...

DELIMITER $$ 

DROP PROCEDURE IF EXISTS `SelectjqGridUsers` $$ 
CREATE PROCEDURE `SelectjqGridUsers` (IN PageIndex INT,IN SortColumnName VARCHAR(50),IN SortOrderBy VARCHAR(4) ,IN NumberOfRows INT ,OUT TotalRecords INT) 

BEGIN 

DECLARE StartRow INT; 
DECLARE CTE VARCHAR(100); 

SELECT TotalRecords = (SELECT COUNT(1) FROM Users); 

SET StartRow = (PageIndex * NumberOfRows) + 1 ; 


END $$ 

DELIMITER ; 

這是我原來的SQL服務器程序:

CREATE PROC [SelectjqGridUsers] 
@PageIndex INT , 
@SortColumnName VARCHAR(50) , 
@SortOrderBy VARCHAR(4) , 
@NumberOfRows INT , 
@TotalRecords INT OUTPUT 
AS 
BEGIN 

    SET NOCOUNT ON 

    SELECT @TotalRecords = (SELECT COUNT(1) 
           FROM  [Users] 
          ) 

    DECLARE @StartRow INT 
    SET @StartRow = (@PageIndex * @NumberOfRows) + 1 ; 


    WITH CTE 
       AS (SELECT ROW_NUMBER() OVER (ORDER BY CASE 
                  WHEN @SortColumnName = 'UserID' 
                  AND @SortOrderBy = 'asc' 
                  THEN UserID 
                 END ASC, CASE 
                  WHEN @SortColumnName = 'UserID' 
                  AND @SortOrderBy = 'desc' 
                  THEN UserID 
                  END DESC, CASE 
                  WHEN @SortColumnName = 'UserName' 
                  AND @SortOrderBy = 'asc' 
                  THEN UserName 
                  END ASC, CASE 
                  WHEN @SortColumnName = 'UserName' 
                  AND @SortOrderBy = 'desc' 
                  THEN UserName 
                  END DESC, CASE 
                  WHEN @SortColumnName = 'FirstName' 
                  AND @SortOrderBy = 'asc' 
                  THEN FirstName 
                  END ASC, CASE 
                  WHEN @SortColumnName = 'FirstName' 
                  AND @SortOrderBy = 'desc' 
                  THEN FirstName 
                  END DESC , CASE 
                  WHEN @SortColumnName = 'MiddleName' 
                  AND @SortOrderBy = 'asc' 
                  THEN MiddleName 
                  END ASC, CASE 
                  WHEN @SortColumnName = 'MiddleName' 
                  AND @SortOrderBy = 'desc' 
                  THEN MiddleName 
                  END DESC , CASE 
                  WHEN @SortColumnName = 'LastName' 
                  AND @SortOrderBy = 'asc' 
                  THEN LastName 
                  END ASC, CASE 
                  WHEN @SortColumnName = 'LastName' 
                  AND @SortOrderBy = 'desc' 
                  THEN LastName 
                  END DESC, CASE 
                  WHEN @SortColumnName = 'EmailID' 
                  AND @SortOrderBy = 'asc' 
                  THEN EmailID 
                  END ASC, CASE 
                  WHEN @SortColumnName = 'EmailID' 
                  AND @SortOrderBy = 'desc' 
                  THEN EmailID 
                  END DESC) AS RN , 
          UserID , 
          UserName , 
          FirstName , 
          MiddleName , 
          LastName , 
          EmailID 
        FROM  [Users] 
       ) 
     SELECT UserID , 
       UserName , 
       FirstName , 
       LastName , 
       MiddleName , 
       EmailID 
     FROM CTE 
     WHERE RN BETWEEN @StartRow - @NumberOfRows 
        AND  @StartRow - 1 

    SET NOCOUNT OFF 


END 

誰能幫我完成這個

+2

嗨,寫代碼的請求是不是真的對堆棧溢出的話題。你能指出你遇到的具體問題嗎?你可以編輯你的問題。 –

+0

@Pekka웃Thnx爲你的快速回復..如何寫「WITH CTE AS(SELECT ROW_NUMBER()OVER(ORDER BY CASE」in mysql procedure ... – vikas

+0

你要花時間,並得到結果給我。 ..沒有人會這樣做 – Sahal

回答

1

嗯,首先,在MySQL中沒有CTE的直接等價物(語句開頭的WITH子句)。也沒有確切的替代ROW_NUMBER()

但是,您可能會理解並(希望)接受這樣一個事實,即人類語言中的句子並不總是可以逐個翻譯爲另一種語言。以同樣的方式,用一種SQL編寫的SQL查詢可能不會,也不必轉換爲子句的不同種類子句。要翻譯查詢,需要兩件事情,同樣重要:理解查詢在目標SQL變體中的作用是多功能的。

您正在翻譯的存儲過程實現了對特定表的動態排序行的動態分頁。

關於分頁,SQL Server沒有本地語法,就像MySQL用它的LIMIT ... OFFSET ...功能一樣。一個可能最典型的解決方法是,你可以在這裏看到,使用ROW_NUMBER()函數和子查詢(在這種情況下,子查詢恰好採用了CTE的形式)。這是如何運作的? ROW_NUMBER()根據特定的排序標準將數字分配給子查詢中的每一行,主查詢僅對這些數字進行過濾,以獲取行的範圍,從而有效地爲您提供頁面。

你需要什麼樣在MySQL的,只是因爲它提供了你LIMIT ...OFFSET ...,這兩者,在存儲過程的上下文中使用時,可以接受整數類型的參數或局部整數類型變量作爲其參數(好吧,他們可以if you are using MySQL 5.5.6+)。

而對於動態排序,在SQL Server中,排序必須應用於ROW_NUMBER()函數,因爲它與動態分頁結合使用。但在MySQL中,您可以直接將ROW_NUMBER()ORDER BY子句移動到(主)查詢中。

那麼,到底,這就是我想出了作爲SELECT查詢在存儲過程的替代品:

SELECT UserID , 
     UserName , 
     FirstName , 
     LastName , 
     MiddleName , 
     EmailID 

FROM Users 

ORDER BY 
     CASE WHEN @SortColumnName = 'UserID'  AND @SortOrderBy = 'asc' THEN UserID  END ASC, 
     CASE WHEN @SortColumnName = 'UserID'  AND @SortOrderBy = 'desc' THEN UserID  END DESC, 
     CASE WHEN @SortColumnName = 'UserName' AND @SortOrderBy = 'asc' THEN UserName END ASC, 
     CASE WHEN @SortColumnName = 'UserName' AND @SortOrderBy = 'desc' THEN UserName END DESC, 
     CASE WHEN @SortColumnName = 'FirstName' AND @SortOrderBy = 'asc' THEN FirstName END ASC, 
     CASE WHEN @SortColumnName = 'FirstName' AND @SortOrderBy = 'desc' THEN FirstName END DESC, 
     CASE WHEN @SortColumnName = 'MiddleName' AND @SortOrderBy = 'asc' THEN MiddleName END ASC, 
     CASE WHEN @SortColumnName = 'MiddleName' AND @SortOrderBy = 'desc' THEN MiddleName END DESC, 
     CASE WHEN @SortColumnName = 'LastName' AND @SortOrderBy = 'asc' THEN LastName END ASC, 
     CASE WHEN @SortColumnName = 'LastName' AND @SortOrderBy = 'desc' THEN LastName END DESC, 
     CASE WHEN @SortColumnName = 'EmailID' AND @SortOrderBy = 'asc' THEN EmailID END ASC, 
     CASE WHEN @SortColumnName = 'EmailID' AND @SortOrderBy = 'desc' THEN EmailID END DESC 

LIMIT NumberOfRows 
OFFSET StartRow 
; 

有一個陷阱,雖然。 LIMITOFFSET的參數可以僅以單個參考(參數或變量)的形式完成,即,您不能在那裏使用表達式。所以,我不得不使用上面的OFFSET StartRow。然而,它可以把你一個不同的頁面不是由SQL Server版本返回除非你也改變了SET StartRow聲明這樣的一個:

SET StartRow = (PageIndex - 1) * NumberOfRows + 1 ; 
相關問題