2014-05-08 44 views
0

CTE查詢我有這樣的代碼:SQL服務器:與外部表

with cte as 
(select m.ID , 1 i from MyTable m union all 
select ID , cte.i +1 i from cte where i < 5) 
select * from cte 

錯誤消息: 消息319,級別15,狀態1,行 '與' 關鍵字2 附近有語法錯誤。如果此語句是公用表表達式,xmlnamespaces子句或更改跟蹤上下文子句,則前面的語句必須以分號結尾。

出了什麼問題?

+0

'with'必須開始聲明。嘗試在行之前添加';',比如';與cte作爲...' – Andomar

+0

天才它的作品,我不知道這意味着什麼「;」你能給我一個關於這個解釋的鏈接嗎? – user1737934

回答

0
declare @MyTable table (id int) 
insert @MyTable values (1),(2),(3),(4),(5); 

with cte(id,i) as 
(select m.ID , 1 from @MyTable m union all 
select ID , cte.i +1 from cte where cte.i < 5) 
select * from cte 
+0

注意「;」在第二行 – DimaSUN

+0

非常感謝 – user1737934