如何将 SELECT 和 INSERT 命令组合到 SQL Server 中不存在的表中? [英] How to combine SELECT and INSERT commands into a non existent table in SQL Server?

查看:18
本文介绍了如何将 SELECT 和 INSERT 命令组合到 SQL Server 中不存在的表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何组合SELECTINSERT INTO 命令并将结果插入到SQL Server 的临时表中?

How does one combine the SELECT and INSERT INTO commands and insert the results into a temporary table in SQL Server?

像这样:

INSERT INTO #TempTable 
    SELECT * FROM MyTable;

推荐答案

您有两个选择.第一个是创建临时表,然后使用 INSERT INTO ... SELECT,如下所示:

You got two options. First one is to create your temp table and then use INSERT INTO ... SELECT, like this:

CREATE TABLE #temp (
  Col1 ...
);

INSERT INTO #temp
SELECT * FROM OtherTable;

第二个选项是直接插入一个新的临时表:

The second option is to insert directly in a new temp table:

SELECT  *
INTO    #temp
FROM    OtherTable;

最大的不同在于,使用方法1,您需要提前指定临时表中的所有列.方法 2 为您提供一个临时表,该表自动包含 OtherTable 的所有列.

Big difference is that using method 1, you need to specify all the columns in your temp table in advance. Method 2 gives you a temp table that automatically has all the columns of your OtherTable.

这篇关于如何将 SELECT 和 INSERT 命令组合到 SQL Server 中不存在的表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆