2011-03-08 96 views
-1

如何從像查詢插入表中的結果:插入到表; with語句

;with foo(foo1,foo2) as (
... 
from ... 
     select from foo 
where ... 
) 
select [1],[2],[3],...,[n] 
from 
(
    select from foo f 
) p 
pivot() pv 
order by row 
+2

對於假設,您留下了很多空缺。 – 2011-03-08 07:26:33

+0

「插入到查詢」是什麼意思?你的意思是「插入到表中」,或「在第二個查詢中使用結果作爲表」或其他內容? – Pondlife 2011-03-08 07:46:56

+0

'在表格中插入'該查詢的一般類型的結果 – cMinor 2011-03-08 07:53:26

回答

1

這是你想要的東西:

;with foo(foo1,foo2)
as
(...from ... select from foowhere ...)

INSERT INTO #SomeTable
select [1],[2],[3],...,[n]
from(
select from foo f) ppivot() pv order by row

+0

如果這樣我得到一個錯誤:'消息208,級別16,狀態1,行2 無效的對象名'#SomeTable'.' – cMinor 2011-03-08 08:05:48

+0

@darkcminor,問題「如何插入表」,如果#SomeTable已經存在,那麼上述將工作。如果沒有,您可以使用下面的select into方法。 – 2011-03-08 08:47:24

0

答案就近了,解決辦法是:

;with foo(foo1,foo2) 
as 
( 
...from ... select from foowhere 
... 
) 
select [1],[2],[3],...,[n] INTO #SomeTable 
from(
    select from foo f 
) ppivot (...) pv 


    order by row