2013-02-18 75 views
1

我想插入數據到臨時表中,我使用的是select * into語法。插入數據到臨時表使用選擇*到

但我得到的錯誤:

Msg 102, Level 15, State 1, Line 8 
Incorrect syntax near ')'. 

雖然單個查詢獲取執行成功。

代碼:

Create Table #_Total 
(
A Int, 
B Int, 
C Int, 
D Int 
) 

Select * InTo #_Total From(
Select 
Sum(Case When Closed=0 And ISNULL(VendorTicketNo,'')='' Then 1 Else 0 End), 
Sum(Case When Closed=1 And TicketType<>8 AND ISNULL(VendorTicketNo,'')<>'' Then 1 Else 0 End), 
Sum(Case When Closed=1 And CAST(ClosedOn As DATE)= CONVERT(VARCHAR(8),GETDATE(),112) Then 1 Else 0 End), 
Sum(Case When Closed=0 And TicketType=8 Then 1 Else 0 End) 
From ALBATMStatus.dbo.Ticket 
) 

Select * From #_Total 

數據庫 - SQL SERVER 2008

+1

您不使用Select * INTO的現有表。刪除該表並嘗試再次執行該查詢。或者你需要使用插入語句 – Ankit 2013-02-18 11:28:12

+0

@Ankit:感謝您的信息.. !!! – Shaggy 2013-02-18 11:34:56

回答

2

因爲您的手之前創建表時,你必須使用

INSERT INTO 

試試這個

Create Table #_Total 
(
A Int, 
B Int, 
C Int, 
D Int 
) 

Insert Into #_Total 
Select * From(
Select 
Sum(Case When Closed=0 And ISNULL(VendorTicketNo,'')='' Then 1 Else 0 End), 
Sum(Case When Closed=1 And TicketType<>8 AND ISNULL(VendorTicketNo,'')<>'' Then 1 Else 0 End), 
Sum(Case When Closed=1 And CAST(ClosedOn As DATE)= CONVERT(VARCHAR(8),GETDATE(),112) Then 1 Else 0 End), 
Sum(Case When Closed=0 And TicketType=8 Then 1 Else 0 End) 
From ALBATMStatus.dbo.Ticket 
) AS a 

Select * From #_Total 

Raj

+0

它顯示相同的錯誤 – Shaggy 2013-02-18 11:12:31

+0

表別名丟失。現在更新代碼 – Raj 2013-02-18 11:25:48

1

正如@Raj說你要麼使用Create要麼使用select into

但是他錯過了as ALIAS_NAME。如下所示:

Create Table #_Total 
(
A Int, 
B Int, 
C Int, 
D Int 
) 

Insert Into #_Total 
Select * From(
Select 
Sum(Case When Closed=0 And ISNULL(VendorTicketNo,'')='' Then 1 Else 0 End) A, 
Sum(Case When Closed=1 And TicketType<>8 AND ISNULL(VendorTicketNo,'')<>'' Then 1 Else 0 End) B, 
Sum(Case When Closed=1 And CAST(ClosedOn As DATE)= CONVERT(VARCHAR(8),GETDATE(),112) Then 1 Else 0 End) C, 
Sum(Case When Closed=0 And TicketType=8 Then 1 Else 0 End) D 
From ALBATMStatus.dbo.Ticket 
) as q1 

Select * From #_Total 
+0

良好的捕獲。我實際上覆制了代碼並將選擇改爲插入。沒有測試代碼。 – Raj 2013-02-18 11:24:04