Msg
(1)
Entriesfrom
(1)
IDENTITY
(1)
Address1
(1)
Line1
(1)
Line2
(1)

INSERT INTO behaviour???

Asked By Burrows
20-Nov-09 10:23 AM
Hi all I was just writing a quick insert INTO and got an error
Msg 156, Level 15, State 1, Line 15
Incorrect syntax near the keyword 'ORDER'.

INSERT INTO [Test].[dbo].[address1]
(
[ID] ,
[customerId] ,
[line1] ,
[line2]
)
(   SELECT
[ID] ,
[customerId] ,
[line1] ,
[line2]
FROM
[dbo].[address]
ORDER BY
[customerId]
)

the funny thing is if I remove the brackets from around the select
statement it works fine any one know why this is the case???

The syntax of INSERT...SELECT is without the brackets:http://msdn.microsoft.

Plamen Ratchev replied to Burrows
20-Nov-09 10:08 AM
The syntax of INSERT...SELECT is without the brackets:
http://msdn.microsoft.com/en-us/library/ms174335.aspx

Also, not sure why you have the ORDER BY, as tables are unordered sets and inserting data in order does not make sense.

--
Plamen Ratchev
http://www.SQLStudio.com

Maybe ID column is an IDENTITY, and thus used for "versioning" the entriesfrom

Peso replied to Plamen Ratchev
20-Nov-09 11:17 AM
Maybe ID column is an IDENTITY, and thus used for "versioning" the entries
from oldest to newest?

//Peter

The ID column is included in the INSERT/SELECT list so if IDENTITY in the

Plamen Ratchev replied to Peso
20-Nov-09 11:53 AM
The ID column is included in the INSERT/SELECT list so if IDENTITY in the target table then it is forcing existing
values and ORDER BY has no effect. It will have effect if there is another column in the target table that is IDENTITY.
And sort is by customer id...

--
Plamen Ratchev
http://www.SQLStudio.com
Post Question To EggHeadCafe