Coder Perfect



I’ve been using MS SQL for a while now, and everytime I need to put data into a table, I like to use syntax like this:

INSERT INTO myTable <something here>

I understand that the keyword INTO is optional and that I am not required to use it, but it has been a habit in my instance.

My question is:

Asked by kristof

Solution #1

The standard is INSERT INTO. Although INTO is optional in most implementations, it is needed in a few, thus include it is a good idea to ensure that your code is portable.

Here you’ll find links to various versions of the SQL standard. Here’s a link to an HTML version of an older standard.

Answered by Bill the Lizard

Solution #2

INTO is completely optional in T-SQL, but they are the same thing (other SQL dialects may differ).

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

I believe it is a matter of perception: I believe I am entering a Customer rather than a row into a database named “Customer.” (This is due to the fact that I name my tables singularly rather than plurally.)

If you stick to the first principle, INSERT INTO Customer will probably “feel right” to you.

If you follow the second principle, your answer will very certainly be INSERT Customer.

Answered by Tomalak

Solution #3

Although it is optional in mySQL, it is required in some other DBMSs, such as Oracle. For what it’s worth, SQL will be more potentially portable with the INTO keyword.

Answered by Tony Andrews

Solution #4

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.

Here’s another scenario in which I’ve used a similar approach: In MS SQL 2005, I got the opportunity to change a rather long stored procedure. The issue is that an excessive amount of data was loaded into a result table. I needed to figure out where the information came from. I looked for places where new records had been added. I noticed numerous INSERT INTOs in the first portion of SP. Then I tried to update them by looking for “INSERT INTO,” but I missed one instance where only “INSERT” was used. In some columns, that one really entered 4k+ rows of empty data! Of course, I should just type in INSERT and see what comes up. That, however, happened to me. I’m blaming IDIOT, the previous programmer:):)

Answered by

Solution #5

Between INSERT and INTO in SQL Server 2005, you may have anything like this:

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

Despite the fact that it works without the INTO, I prefer to use it for readability.

Answered by devXen

Post is based on