Hey, readers! Hope you all are well. In the series of SQL SERVER, today we will be having a look at one of the most useful and interesting concept of SQL Server — SQL SERVER SELECT INTO Statement.

嘿,读者们! 希望大家一切都好。 在SQL SERVER系列文章中,今天我们将看看SQL Server最有用和最有趣的概念之一-SQL SERVER SELECT INTO语句



SQL SERVER SELECT INTO语句的工作 (Working of SQL SERVER SELECT INTO statement)

SQL Server SELECT INTO statement helps us create a new table within the same database with all or specific records from the selected(old) table. Moreover, we can add certain restrictions to the data being copied into the new table by validating it through certain conditions.

SQL Server SELECT INTO statement可帮助我们使用来自selected(old)表的所有或特定记录在同一数据库内创建一个新表。 此外,我们可以通过某些条件对其进行验证,从而对要复制到新表中的数据添加某些限制。

For example:

例如:

At times, it may happen that we would want our fellow developers or database engineers to work on the same database as ours. That is, it may happen that while developing a project, we would want our junior developers to work on a portion of the database.

有时,我们可能希望我们的开发人员或数据库工程师与我们在同一个数据库上工作。 也就是说,在开发项目时,我们可能希望初级开发人员在数据库的一部分上工作。

Moreover, we would also want to grant them entire access to any kind of alteration in the given database.

此外,我们还希望授予他们对给定数据库中任何种类的更改的完整访问权限。

This is when SQL SERVER SELECT INTO comes into picture.

这是SQL SERVER SELECT INTO出现的时候。

By this, a new table gets created in the database with the columns and data values from the selected/old table.

这样,数据库中就会创建一个新表,其中包含来自选定/旧表的列和数据值。

Now let us understand the structure of SQL Server SELECT INTO statement in the below section.

现在,让我们在下面的部分中了解SQL Server SELECT INTO语句的结构。



SQL SELECT INTO语句的语法 (Syntax of SQL SELECT INTO statement)

SQL Server SELECT INTO statement creates a new table within the same database and copies the data from the old table into it.

SQL Server SELECT INTO语句在同一数据库中创建一个新表,并将数据从旧表复制到该表中。

Syntax:

句法:


SELECT column-names
INTO new-table
FROM old-table;
  • column-names: The column names of the old table.column-names :旧表的列名。
  • new-table: This table is the copy of the old table with the specified values.new-table :此表是具有指定值的旧表的副本。
  • old-table: The table from which the data is to be copied into the new table.old-table :要将数据从中复制到新表中的表。

Now, let us implement SQL Server SELECT INTO statement with various examples.

现在,让我们使用各种示例来实现SQL Server SELECT INTO语句。



通过示例实现SQL SERVER SELECT INTO (Implementing SQL SERVER SELECT INTO through examples)

At first, let us create a table and insert values into the table.

首先,让我们创建一个表并将值插入表中。


CREATE TABLE Info (id INT PRIMARY KEY,City VARCHAR (255) NOT NULL
); INSERT INTO Info (id,City
)
VALUES(1,'Pune'),(2,'Satara'),(3,'California');

Output: Table — ‘Info’

输出:表—“信息”

SQL SERVER-Create TableSQL SERVER-创建表

Now that we have created the table – ‘Info’ with above data values, let us now create a new table ‘Info_backup’ with all the data values from the table – ‘Info’ in it.

现在,我们已经使用上面的数据值创建了表–'Info',现在让我们创建一个新表'Info_backup',其中包含表–'Info'中的所有数据值。



SQL Server SELECT INTO语句复制所有数据值 (SQL Server SELECT INTO statement to copy all the data values)

Example: Using SQL Server SELECT INTO statement to create a backup of Table – ‘Info’ as ‘Info_backup’ within the same database.

示例:使用SQL Server SELECT INTO语句在同一数据库中创建表-“ Info”的备份作为“ Info_backup”。


SELECT *
INTO Info_backup
FROM Info;

We will view the data of the newly created table – ‘Info_backup’ using the SELECT query as shown below–

我们将使用SELECT查询查看新创建表的数据“ Info_backup”,如下所示–


Select * from Info_backup;

Output:

输出:

SQL SERVER Table – Info_backupSQL SERVER表– Info_backup


使用AS子句SQL SELECT INTO查询 (SQL SELECT INTO query with AS clause)

If we wish to change the column names of the data values that are being inserted into the new table from the old new, SELECT INTO table along with the AS clause can help us serve the purpose.

如果我们希望从新旧表中更改要插入到新表中的数据值的列名,则SELECT INTO表以及AS子句可以帮助我们达到目的。

Example:

例:


SELECT id AS info_id, city
INTO Info_view
FROM Info;

SELECT * from Info_view;

Here, we have changed the column name – ‘id’ to ‘info_id’ in the newly created table – ‘Info_view’ using the SQL AS Clause along with SELECT INTO statement.

在这里,我们使用SQL AS子句以及SELECT INTO语句将新创建的表中的列名“ id”更改为“ info_id”“ Info_view”。

Output:

输出:

SQL SERVER SELECT INTO-Example 4SQL SERVER SELECT INTO-示例4


使用SELECT INTO语句复制特定的列 (Copying specific columns using SELECT INTO statement)

Now, we will be referring to the below created table – ‘Stock’ for the below examples.

现在,在下面的示例中,我们将参考下面创建的表-“股票”。


CREATE TABLE Stock (id INT PRIMARY KEY,City VARCHAR (255) NOT NULL,Cost INT
); INSERT INTO Stock (id,City,Cost
) VALUES(1,'Pune',100),(2,'Satara',500),(3,'California',50);

Output: Table — ‘Stock’

输出:表-“库存”

SQL SERVER-Table 2SQL SERVER表2

In the below example, we have copied only the few selected columns from the table – ‘Stock’ into the newly created table – ‘Stock_view’.

在下面的示例中,我们仅将表中的几个选定列“ Stock”复制到了新创建的表“ Stock_view”中。

Example: Copying specific columns from the old table into the new table using SQL Server SELECT INTO statement.

示例:使用SQL Server SELECT INTO语句将特定的列从旧表复制到新表中。


select id, cost
INTO Stock_view
From Stock;

Here, we have selected and copied the data values of the columns ‘id’ and ‘cost’ into the new table – ‘Stock_view’ from ‘Stock’.

在这里,我们选择了“ id”和“ cost”列的数据值并将其复制到新表–“ Stock”中的“ Stock_view”。

Output:

输出:

SQL SERVER SELECT INTO-Example 2SQL SERVER SELECT INTO-示例2


带WHERE子句SQL Server SELECT INTO语句 (SQL Server SELECT INTO statement with WHERE clause)

We can frame the SQL Server SELECT INTO statement with conditional clauses as well, such as, WHERE clause.

我们还可以使用条件子句(例如WHERE子句)来构造SQL Server SELECT INTO语句。

Thus, data values would be copied from old table to the new table depending upon the specified condition mentioned in the WHERE clause.

因此,根据WHERE子句中提到的指定条件,数据值将从旧表复制到新表。

Example:

例:


Select *
INTO Conditional_Stock
From Stock
WHERE Cost>50 and Cost<400;

The SELECT INTO statement copies all the data values from ‘Stock’ into the new table ‘Conditional_Stock’ whose ‘cost’ value lies between 50 to 400.

SELECT INTO语句将所有数据值从“库存”复制到新表“ Conditional_Stock”,其“成本”值介于50到400之间。

Output:

输出:

SQL SERVER SELECT INTO-Example 3 SQL SERVER SELECT INTO-示例3


要注意的一点! (Point to be Noted!)

‘Why do we need SQL Server SELECT INTO statement, when we have SQL Views to server the purpose?’

“当我们有SQL视图来服务于目的时,为什么我们需要SQL Server SELECT INTO语句?”

This might be a question-mark for many of you, by the time you reach the end of this article.

在您到达本文结尾时,这对于您中的许多人来说可能是一个问号。

Let me help you with it.

让我帮你。

Using SQL Views, we can limit or restrict the access to the data by specifying condition. Same job can be performed by SELECT INTO Statement.

使用SQL视图,我们可以通过指定条件来限制或限制对数据的访问。 SELECT INTO语句可以执行相同的工作。

But the point to understand is that, all the changes made in the view are reflected in the original database. While the changes made in the new tables created out of SELECT INTO statement do no reflect any changes in the original data.

但是要理解的一点是, 视图中所做的所有更改都反映在原始数据库中使用SELECT INTO语句创建的新表中所做的更改不会反映原始数据中的任何更改



结论 (Conclusion)

By this, we have come to the end of this topic. Please feel free to comment below in case, you come across any doubt.

至此,我们到了本主题的结尾。 如果您有任何疑问,请在下面发表评论。

For more such posts related to SQL, please do visit SQL JournalDev.

有关与SQL有关的更多此类帖子,请访问SQL JournalDev 。



参考资料 (References)

  • SQL SERVER SELECT INTO — DOCUMENTATIONSQL SERVER SELECT INTO —文档

翻译自: https://www.journaldev.com/41734/sql-server-select-into-statement

了解SQL Server SELECT INTO语句相关推荐

  1. SQL server USE GO语句学习总结

    语法 USE USE语句的基本语法如下所示 - USE database_name; //数据库名称 数据库名称在RDBMS中必须是唯一的 如果SQL模式中有多个数据库,那么在新建查询开始操作之前,需 ...

  2. SQL server 2008基本语句大全与提升语句

    SQL server 2008 常用语句 1.基础语句 2.sql语句提升 1.基础语句 ##1.创建数据库 CREATE DATABASE database-name on primary (nam ...

  3. SQL Server中T-SQL语句查询使用的函数

    SQL Server中T-SQL语句查询使用的函数 一,字符串函数 字符串函数用于对字符串数据进行处理,并返回一个字符串或数字. 函数名 描述 举例 CHARINDEX 用来寻找一个指定的字符串在另一 ...

  4. SQL Server - 使用 Merge 语句实现表数据之间的对比同步

    SQL Server - 使用 Merge 语句实现表数据之间的对比同步 原文:SQL Server - 使用 Merge 语句实现表数据之间的对比同步 表数据之间的同步有很多种实现方式,比如删除然后 ...

  5. SQL Server 常用更新语句,用B表数据作为条件或数据源更新A表数据

    SQL Server 常用更新语句,用B表数据作为条件或数据源更新A表数据 示例:用B表姓名覆盖A表姓名 假设: A表数据 Id,Name 1,张三 B表数据 Id,Name 1,李四 用下面这个语句 ...

  6. SQL Server select语句执行顺序

    根据<Microsoft SQL Server2000 宝典>,Select语句的完整执行顺序: 1.from子句组装来自不同数据源的数据 2.where子句基于指定的条件对记录行进行筛选 ...

  7. sql server之数据库语句优化

    一切都是为了性能,一切都是为了业务 一.查询的逻辑执行顺序 (1) FROM left_table (3) join_type JOIN right_table (2) ON join_conditi ...

  8. SQL update select结合语句详解及应用

    SQL update select语句 最常用的update语法是: 1 2 UPDATETABLE_NAME SET column_name1 =VALUE WHRER column_name2 = ...

  9. 【转载】SQL update select结合语句详解及应用

    最常用的update语法是: 1 2 UPDATE TABLE_NAME SET column_name1 = VALUE WHRER column_name2 = VALUE 如果我的更新值Valu ...

最新文章

  1. 毕业,新的开始,撸起袖子加油干!
  2. 【GDOI2016模拟3.16】幂(容斥 + 模型复杂转化)
  3. 关于引进制转换(凌乱)
  4. RT-Flash imxrt 系列rt1052 rt1060量产神器宣传
  5. Head First summary
  6. linux中的运行模式,Linux系统运行模式介绍
  7. Java 继承——2
  8. springboot配置文件(.yml)中自定义属性值并在controller里面获取
  9. Venture Sprint创新冲刺:源自硅谷设计 感知创新力量
  10. 用java开发的软件_java开发需要用到的软件有哪些
  11. idea使用教程-安装
  12. [ZT]大型企业局域网安全解决方案
  13. python中的cd是什么意思_cd是什么意思 如果learnPython放在其它盘内该如果解决
  14. Vue3和Vue2组件单元素的过渡
  15. [unreal] 切换关卡
  16. PAT乙级练习题1030 完美数列
  17. 【已解决】ansys打开没有主界面,只有output窗口怎么回事?
  18. 仿携程oracle课程设计,一个不错的仿携程自定义数据下拉选择select
  19. 全国计算机等级考试一级练习题,全国计算机等级考试一级练习题
  20. C++一本通题库1024

热门文章

  1. 4月10日下午学习日志
  2. 团队第二次冲刺第一天
  3. iOS字符串处理笔记(正则表达式、NSScanner扫描、CoreParse解析器)
  4. XCode6 ,iOS之PCH文件配置
  5. 查询数据库里所有表名,字段名的语句
  6. iOS直播集成和问题总结(阿里云直播)
  7. 【Java Web开发学习】Spring MVC 拦截器HandlerInterceptor
  8. html中script标签的使用方法
  9. 同一个项目,项目名称不一致,这两个项目同时在Eclipse中出现
  10. C++之---class 的三种访问修饰符( public、private、protected )