SQL查询结果如何插入临时表?

SQL查询结果如何插入临时表?

问题描述:

我有一个 SQL 查询 (SQL Server) 并且它生成报告,我想将该确切报告存储在临时表中,以便我以后可以使用它.现在的问题是我需要先创建临时表然后将SQL查询结果存储到其中,还是有什么方法可以动态创建表并存储查询结果?

I have a SQL query (SQL Server) and it generate reports, I want to store that exact report in temp table so I can play with it later. Now question is do I need to create temp table first and then store SQL query result into it, or is there any way to dynamically create table and store query result?

SELECT INTO.这将为您创建一个新表,如果您愿意,可以通过在表名前加上井号 (#) 来创建临时表.

Look at SELECT INTO. This will create a new table for you, which can be temporary if you want by prefixing the table name with a pound sign (#).

例如,您可以:

SELECT * 
INTO #YourTempTable
FROM YourReportQuery