SQL 插入与插入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/233919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:03:39  来源:igfitidea点击:

INSERT vs INSERT INTO

sqlsql-servertsql

提问by kristof

I have been working with T-SQL in MS SQL for some time now and somehow whenever I have to insert data into a table I tend to use syntax:

我在 MS SQL 中使用 T-SQL 已经有一段时间了,每当我必须将数据插入表中时,我倾向于使用语法:

INSERT INTO myTable <something here>

I understand that keyword INTOis optional here and I do not have to use it but somehow it grew into habit in my case.

我知道关键字INTO在这里是可选的,我不必使用它,但在我的情况下它以某种方式养成了习惯。

My question is:

我的问题是:

  • Are there any implications of using INSERTsyntax versus INSERT INTO?
  • Which one complies fully with the standard?
  • Are they both valid in other implementations of SQL standard?
  • 使用INSERT语法与INSERT INTO?
  • 哪一个完全符合标准?
  • 它们在 SQL 标准的其他实现中都有效吗?

回答by Bill the Lizard

INSERT INTOis the standard. Even though INTOis optional in most implementations, it's required in a few, so it's a good idea to include it to ensure that your code is portable.

INSERT INTO是标准。尽管INTO在大多数实现中是可选的,但在少数实现中是必需的,因此最好包含它以确保您的代码是可移植的。

You can find links to several versions of the SQL standard here. I found an HTML version of an older standard here.

您可以在此处找到指向 SQL 标准的多个版本的链接。我在这里找到了旧标准的 HTML 版本。

回答by Tomalak

They are the same thing, INTOis completely optional in T-SQL (other SQL dialects may differ).

它们是一样的东西,INTO在 T-SQL 中是完全可选的(其他 SQL 方言可能不同)。

Contrary to the other answers, I think it impairs readability to use INTO.

与其他答案相反,我认为使用INTO.

I think it is a conceptional thing: In my perception, I am not inserting a rowinto a table named "Customer", but I am inserting a Customer. (This is connected to the fact that I use to name my tables in singular, not plural).

我认为这是一个概念上的事情:在我看来,我不是在名为“Customer”的表中插入一行,而是插入一个Customer。(这与我习惯用单数而不是复数来命名我的表的事实有关)。

If you follow the first concept, INSERT INTO Customerwould most likely "feel right" for you.

如果您遵循第一个概念,INSERT INTO Customer那么您很可能会“感觉正确”。

If you follow the second concept, it would most likely be INSERT Customerfor you.

如果您遵循第二个概念,它很可能INSERT Customer适合您。

回答by Tony Andrews

It may be optional in mySQL, but it is mandatory in some other DBMSs, for example Oracle. So SQL will be more potentially portable with the INTO keyword, for what it's worth.

它在 mySQL 中可能是可选的,但在其他一些 DBMS 中它是必需的,例如 Oracle。因此,SQL 使用 INTO 关键字将更有可能移植,因为它的价值。

回答by David.Chu.ca

One lesson I leaned about this issue is that you should always keep it consistent! If you use INSERT INTO, don't use INSERT as well. If you don't do it, some programmers may ask the same question again.

我对这个问题的一个教训是,你应该始终保持一致!如果您使用 INSERT INTO,也不要使用 INSERT。如果你不这样做,一些程序员可能会再次问同样的问题。

Here is my another related example case: I had a chance to update a very very long stored procedure in MS SQL 2005. The problem is that too many data were inserted to a result table. I had to find out where the data came from. I tried to find out where new records were added. At the beginning section of SP, I saw several INSERT INTOs. Then I tried to find "INSERT INTO" and updated them, but I missed one place where only "INSERT" was used. That one actually inserted 4k+ rows of empty data in some columns! Of course, I should just search for INSERT. However, that happened to me. I blame the previous programmer IDIOT:):)

这是我的另一个相关示例案例:我有机会在 MS SQL 2005 中更新一个非常长的存储过程。问题是向结果表中插入了太多数据。我必须找出数据的来源。我试图找出添加新记录的位置。在SP的开头部分,我看到了几个INSERT INTO。然后我试图找到“INSERT INTO”并更新它们,但我错过了一个只使用“INSERT”的地方。那个实际上在某些列中插入了 4k+ 行空数据!当然,我应该只搜索 INSERT。然而,这发生在我身上。我责怪以前的程序员白痴:):)

回答by devXen

In SQL Server 2005, you could have something in between INSERT and INTO like this:

在 SQL Server 2005 中,您可以在 INSERT 和 INTO 之间添加如下内容:

INSERT top(5) INTO tTable1 SELECT * FROM tTable2;

Though it works without the INTO, I prefer using INTO for readability.

虽然它在没有 INTO 的情况下工作,但我更喜欢使用 INTO 以提高可读性。

回答by DOK

They both do the same thing. INTO is optional (in SQL Server's T-SQL) but aids readability.

他们都做同样的事情。INTO 是可选的(在 SQL Server 的 T-SQL 中)但有助于提高可读性。

回答by Tom

If available use the standard function. Not that you ever need portability for your particular database, but chances are you need portability for your SQL knowledge. A particular nasty T-SQL example is the use of isnull, use coalesce!

如果可用,请使用标准功能。并不是说您需要特定数据库的可移植性,而是您的 SQL 知识可能需要可移植性。一个特别讨厌的 T-SQL 示例是使用 isnull,使用合并!

回答by AgentThirteen

I prefer using it. It maintains the same syntax delineation feel and readability as other parts of the SQL language, like group BY, order BY.

我更喜欢使用它。它保持与 SQL 语言的其他部分相同的语法描述感觉和可读性,例如group BY, order BY

回答by Garry_G

I started wtiting SQL on ORACLE, so when I see code without INTO it just looks 'broken' and confusing.

我开始在 ORACLE 上使用 SQL,所以当我看到没有 INTO 的代码时,它看起来只是“损坏”且令人困惑。

Yes, it is just my opinion, and I'm not saying you shouldalways use INTO. But it you don't you should be aware that many other people will probably think the same thing, especially if they haven't started scripting with newer implementations.

是的,这只是我的意见,我并不是说你应该总是使用 INTO。但是你不应该知道,许多其他人可能会想到同样的事情,特别是如果他们还没有开始使用较新的实现编写脚本。

With SQL I think it's also very important to realise that you ARE adding a ROW to a TABLE, and not working with objects. I think it would be unhelpful to a new developer to think of SQL table rows/entries as objects. Again, just me opinion.

对于 SQL,我认为意识到您正在向 TABLE 添加一个 ROW 而不是使用对象也很重要。我认为将 SQL 表行/条目视为对象对新开发人员没有帮助。再次,只是我的意见。