all groups > sql server programming > august 2004 >
You're in the

sql server programming

group:

INSERT INTO with ORDER BY clause in the SELECT


INSERT INTO with ORDER BY clause in the SELECT User
8/2/2004 11:54:43 PM
sql server programming:
Is there any significance or reliability in putting an ORDER BY clause in
the "derived table" of an INSERT INTO statement?

In the example below:
#A is a table with an identity column representing the order in which
entries were made into table #A
If these records are copied to #B with a INSERT INTO ... SELECT FROM #A ..
ORDER BY ... statement,

Will
SELECT item FROM #A ORDER BY seqno
return records in the same order as
SELECT item FROM #B ORDER BY seqno


====================

CREATE TABLE #A(
item varchar(8),
seqno int IDENTITY(1,1) not null
)

CREATE TABLE #B(
item varchar(8),
seqno int IDENTITY(1,1) not null
)

INSERT INTO #A(item) VALUES('A')
INSERT INTO #A(item) VALUES('B')
INSERT INTO #A(item) VALUES('C')
INSERT INTO #A(item) VALUES('D')

INSERT INTO #B(item) SELECT item FROM #A ORDER BY seqno

SELECT item FROM #A ORDER BY seqno
SELECT item FROM #B ORDER BY seqno

DROP TABLE #A
DROP TABLE #B

Re: INSERT INTO with ORDER BY clause in the SELECT Joe Celko
8/3/2004 7:02:58 AM
[quoted text, click to view]
clause in the "derived table" of an INSERT INTO statement? <<

It is a good way to tell that someone was asleep in their basic
relational database class and does not understand that tables have no
ordering by definition.

I use it as an indicator that the rest of their code is also screwed up
from **fundamental** misunderstandings of RDBMS when I am doing
forensics. You just know that they are about to confuse rows and
records -- Yep! There it is :)!

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Re: INSERT INTO with ORDER BY clause in the SELECT User
8/3/2004 7:14:21 AM
In the Books on Line the INSERT statement is described as follows:

Syntax
INSERT [ INTO]
{ table_name WITH ( < table_hint_limited > [ ...n ] )
| view_name
| rowset_function_limited
}

{ [ ( column_list ) ]
{ VALUES
( { DEFAULT | NULL | expression } [ ,...n] )
| derived_table
| execute_statement
}
}
| DEFAULT VALUES

< table_hint_limited > ::=
{ FASTFIRSTROW
| HOLDLOCK
| PAGLOCK
| READCOMMITTED
| REPEATABLEREAD
| ROWLOCK
| SERIALIZABLE
| TABLOCK
| TABLOCKX
| UPDLOCK
}

Thus the nomenclature "derived table".


[quoted text, click to view]

Re: INSERT INTO with ORDER BY clause in the SELECT Roji. P. Thomas
8/3/2004 3:10:15 PM
Usually it will, at least with small set of data.
But in real life it depends on the index on the target
table and volume of data.

Theoratically the rows in a table are unordered.
So any assumption on the physical order of rows itself is wrong.

--
Roji. P. Thomas
Net Asset Management
https://www.netassetmanagement.com


[quoted text, click to view]

AddThis Social Bookmark Button