使用Select * Into将数据插入临时表

时间:2021-11-23 16:45:52

I want to insert data into temporary table for that i am using select * into syntax.

我想将数据插入临时表,因为我使用select * into语法。

But i am getting error :

但我得到错误:

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

Though Single Query getting executed successful.

虽然单个查询执行成功。

Code:

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

Database - SQL SERVER 2008

数据库 - SQL SERVER 2008

2 个解决方案

#1


2  

Since you are creating the table before hand, you have to use

由于您事先创建了表,因此必须使用

INSERT INTO

Try this

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

#2


1  

As @Raj said you either use Create or select into.

正如@Raj所说,你要么使用Create,要么选择into。

However he missed as ALIAS_NAME. It is as below :

然而他错过了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

#1


2  

Since you are creating the table before hand, you have to use

由于您事先创建了表,因此必须使用

INSERT INTO

Try this

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

#2


1  

As @Raj said you either use Create or select into.

正如@Raj所说,你要么使用Create,要么选择into。

However he missed as ALIAS_NAME. It is as below :

然而他错过了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