111

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:

INSERT INTO myTable <something here>

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

My question is:

  • Are there any implications of using INSERT syntax versus INSERT INTO?
  • Which one complies fully with the standard?
  • Are they both valid in other implementations of SQL standard?
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
kristof
  • 52,923
  • 24
  • 87
  • 110

10 Answers10

112

INSERT INTO is the standard. Even though INTO is optional in most implementations, it's required in a few, so it's a good idea to include it if you want your code to be portable.

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

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • 3
    The book "SQL-99 Complete, Really" says that Sybase (and hence MS SQL Server) behave in the nonstandard way, allowing INTO to be optional. Other brands of database require the keyword. – Bill Karwin Oct 24 '08 at 16:38
  • 4
    Right. If you always use INTO, you don't need to remember which ones consider it optional. All implementations allow it to be used. – Bill the Lizard Oct 24 '08 at 19:12
25

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

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

I think it is a conceptional thing: In my perception, I am not inserting a row into 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).

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

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

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • 5
    I think the risk with your approach is that you may lead new programmers to confuse objects with tables. – Dave DuPlantis Oct 24 '08 at 15:25
  • 12
    Your answer is factually correct. It's a shame when people down-vote opposing points of view. I think one of the benefits of SO is hearing those difffering opinions. – DOK Oct 24 '08 at 15:29
  • It's not matter of thinking in objects and tables. It's thinking in a conceptional way as a entity relational model. – Andre Figueiredo Jul 21 '17 at 21:30
  • @DaveDuPlantis Ah, the Jackie Treehorn fallacy. +1 – ruffin Oct 17 '17 at 17:51
  • Off topic but I think that was a flaw in the design. It made inconsistent. Consistency would have made UPDATE to use a a keyword IN ...UPDATE VALUES(...) IN MYTABLE(...)and INSERT VALUES(....) IN MYTABLE(.....). That's just my opinion though. – FernandoZ Aug 05 '21 at 17:04
11

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.

Tony Andrews
  • 129,880
  • 21
  • 220
  • 259
5

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

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

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

devXen
  • 3,013
  • 3
  • 35
  • 44
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 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:):)

David.Chu.ca
  • 37,408
  • 63
  • 148
  • 190
  • It seems more like author created it with INSERT INTO and some put a INSERT in a maintenance. It happens a lot, much more than it "should", unfortunately. – Andre Figueiredo Jul 21 '17 at 21:26
3

INSERT INTO is SQL standard while INSERT without INTO is not SQL standard.

I experimented them on SQL Server, MySQL, PostgreSQL and SQLite as shown below.

Database INSERT INTO INSERT
SQL Server Possible Possible
MySQL Possible Possible
PostgreSQL Possible Impossible
SQLite Possible Impossible

In addition, I also experimented DELETE FROM and DELETE without FROM on SQL Server, MySQL, PostgreSQL and SQLite as shown below:

Database DELETE FROM DELETE
SQL Server Possible Possible
MySQL Possible Impossible
PostgreSQL Possible Impossible
SQLite Possible Impossible
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
1

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

DOK
  • 32,337
  • 7
  • 60
  • 92
1

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

Yes, it is just my opinion, and I'm not saying you should always 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.

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.

Garry_G
  • 169
  • 5
  • 12
0

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.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
AgentThirteen
  • 261
  • 1
  • 4
  • 8
0

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!

C B
  • 1,677
  • 6
  • 18
  • 20
Tom
  • 1,381
  • 3
  • 15
  • 26