Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Insert INTO Statement syntax [duplicate]

Why does not the following INSERT statement give any error?

CREATE TABLE Table1(id INT,name VARCHAR(10))

INSERT INTO Table1(xx.id,yyyy.name) Values (1,'A')

Why does the above statement ignore xx. and yyyy.? What does this imply ?

like image 736
Shiva Avatar asked Nov 06 '14 11:11

Shiva


People also ask

How do you insert duplicates in SQL?

Use the context menu on the same table, to get another script: "Script Table as | SELECT To | New Query Window". This will be a totally standard select list, with all your fields listed out. Copy the whole query and paste it in over the VALUES clause in your first query window. This will give you a complete INSERT ...

How can we prevent insertion of duplicate data?

Note − Use the INSERT IGNORE command rather than the INSERT command. If a record doesn't duplicate an existing record, then MySQL inserts it as usual. If the record is a duplicate, then the IGNORE keyword tells MySQL to discard it silently without generating an error.


2 Answers

I checked the below query also.

INSERT INTO Table11(xx.xx.xx.xx.xx.xx.xx.id,yy.yy.yy.yy.yy.yy.yy.yy.name) 
Values (1,'A')

It also got worked. Usually we use alias for joins. As I know, For Insert query Using alias near table name is restricted in sql. In the case of column name, the query use only the string next to the last Dot(.).

I conclude it as, The Insert query don't care about the string prefixed to the column name separated by Dot(.).

like image 174
Veera Avatar answered Oct 18 '22 22:10

Veera


The only thing I can think of is that the database engine is ignoring the name space as the query's scope is limited to the Table's scope when dealing with INSERT INTO. When it comes to say UPDATE where multiple tables can be part of the scope, the below would fail. Don't know why this happens but if I were to guess, probably all values to the left of the last period'.' is ignored

If you analyze the execution plan for the below query

CREATE TABLE Table1(id INT,name VARCHAR(10))

INSERT INTO Table1(Table2.dbo.id,...................name) Values (1,'A')

AS

INSERT INTO [Table1]([id],[name]) Values(@1,@2)
like image 21
Gouri Shankar Aechoor Avatar answered Oct 18 '22 23:10

Gouri Shankar Aechoor