2013-05-30 66 views
0

什麼是錯在此過程中SQL錯誤:不正確的語法錯誤

CREATE PROCEDURE [dbo].[Question_ReadBySort] 
-- Add the parameters for the stored procedure here 
(
    @PageNumber int, 
    @Gid bigint, 
    @Sorttype int, 
    @Df int 
) 
AS 
BEGIN 
    if @Gid=0 
     BEGIN 
     With Cust AS 
      (SELECT * , 
      ROW_NUMBER() OVER (order by q_id DESC) as RowNumber 
      from tbl_Question 
      where q_del=0) 
     END 
    ELSE 
     BEGIN 
     With Cust AS 
      (SELECT * , 
      ROW_NUMBER() OVER (order by q_id DESC) as RowNumber 
      from tbl_Question 
      where q_del=1) 
     END 
END 
GO 

,並出現在SQL Server這樣的錯誤:

Msg 156, Level 15, State 1, Procedure Question_ReadBySort, Line 23
Incorrect syntax near the keyword 'END'.

Msg 156, Level 15, State 1, Procedure Question_ReadBySort, Line 31
Incorrect syntax near the keyword 'END'.

+0

你是否順序地減少了查詢的每個部分,以消除罪魁禍首,並在實際問題中歸零,或者只是在這裏要求我們從頭開始理清它? –

回答

6

的CTE定義必須緊跟使用CTE聲明。例如像

WITH Cust 
    AS (SELECT *, 
       ROW_NUMBER() OVER (ORDER BY q_id DESC) AS RowNumber 
     FROM tbl_Question 
     WHERE q_del = CASE 
          WHEN @Gid = 0 THEN 0 
          ELSE 1 
         END) 
SELECT * 
FROM Cust 

不能定義不同的CTE的定義有條件與IF那麼,你似乎在試圖以後使用它們。