SQL SELECT INTO语句

This article will cover the SQL SELECT INTO statement including syntax, parameters and use with multiple tables, filegroups and a WHERE condition

本文将介绍SQL SELECT INTO语句,包括语法,参数以及与多个表,文件组和WHERE条件一起使用

We regularly insert data into SQL Server tables either from an application or directly in SSMS. We can insert data using the INSERT INTO statement. To do this, we should have a table already in place to insert data into it as we cannot create a table using Insert into the statement.

我们定期从应用程序或直接在SSMS中将数据插入SQL Server表中。 我们可以使用INSERT INTO语句插入数据。 为此,我们应该已经有一个表可以在其中插入数据,因为我们不能使用插入到语句来创建表。

We need to do the following tasks using INSERT INTO statement.

我们需要使用INSERT INTO语句执行以下任务。

  • Create a table structure with appropriate data types

    创建具有适当数据类型的表结构
  • Insert data into it

    将数据插入其中

But the good news is that we can do both the task together, elegantly, using the SQL SELECT INTO statement. It creates a table structure for the columns returned by Select statement.

但是好消息是,我们可以使用SQL SELECT INTO语句优雅地一起完成这两个任务。 它为Select语句返回的列创建一个表结构。

Suppose we want to update many records in a table. We can use the SELECT INTO statement to create a backup table with the existing structure as of source table. Let us explore the SELECT INTO in this article.

假设我们要更新表中的许多记录。 我们可以使用SELECT INTO语句创建一个备份表,该备份表具有与源表相同的现有结构。 让我们探讨本文中的SELECT INTO。

SELECT INTO语句语法 (SELECT INTO statement syntax)

SELECT column1,column2...ColumnN
INTO New_table
FROM tables
[Where conditions];

SELECT INTO语句中的参数 (Parameters in the SELECT INTO Statement)

  • Columns list: We need to specify column we want to retrieve and insert into a new table 列列表:我们需要指定要检索的列并插入到新表中
  • New_table: We can specify the new table name here. SQL Server creates a new table with columns mentioned in columns list. We cannot replace an existing table using this. Table name should be unique New_table:我们可以在此处指定新表的名称。 SQL Server使用列列表中提到的列创建一个新表。 我们不能使用此表替换现有表。 表名应该是唯一的
  • Tables: It contains a table from where we want to get records. We can have multiple tables defined here as well with proper Join clause 表格:它包含一个我们要从中获取记录的表格。 我们也可以在这里定义多个表以及适当的Join子句
  • WHERE conditions: We can filter records using Where clause. It is an optional clause WHERE条件:我们可以使用Where子句过滤记录。 这是一个可选子句

Let us explore the SQL SELECT INTO statement using examples.

让我们使用示例探索SQL SELECT INTO语句。

环境: (Environment:)

In this example, we are using sample database AdventureWorks2017. Suppose we want to select records from [Employee] table and creates new table [Employee_Demo] using the SELECT INTO statement. We do want selected columns only in the new table. Let us run a select statement on Employee table with the columns we want to have.

在此示例中,我们使用的是示例数据库AdventureWorks2017。 假设我们要从[Employee]表中选择记录,并使用SELECT INTO语句创建新表[Employee_Demo]。 我们确实只希望在新表中选择列。 让我们在Employee表上运行带有想要的列的select语句。

SELECT TOP (10) 
      [NationalIDNumber]
      ,[LoginID]
       ,[JobTitle]
      ,[BirthDate]
      ,[MaritalStatus]
      ,[Gender]
      ,[HireDate]
      ,[VacationHours]         
  FROM [AdventureWorks2017].[HumanResources].[Employee]

SQL SELECT INTO语句_第1张图片

Before we execute the SQL SELECT INTO statement, we can verify that Employee_Demo table does not exist in AdventureWorks2017 database using sp_help command.

在执行SQL SELECT INTO语句之前,可以使用sp_help命令验证AdventureWorks2017数据库中不存在Employee_Demo表。

sp_help '[AdventureWorks2017].[HumanResources].[Employee_Demo]'

In the following screenshot, we can see that Employee_Demo table does not exist in my database.

在以下屏幕截图中,我们可以看到Employee_Demo表在我的数据库中不存在。

你可能感兴趣的:(数据库,java,mysql,python,sql)