SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Sort Order when SELECT FROM Table Variables


Sort Order when SELECT FROM Table Variables

Author
Message
DCF
DCF
Valued Member
Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)

Group: General Forum Members
Points: 64 Visits: 70
Hi,

Would I be correct in thinking that if you omit an ORDER BY clause when querying a Table variable in TSQL that you cannot guarantee the order in which the resulting records will be returned? That is, the Query Optimizer treats them the same as a normal user table.

For example, the output from the following TSQL would not guarantee a result set ordered by Col1:

--Insert into a table variable in order of Column 1, asceding.
INSERT @aTable
SELECT
Col1
, Col2
, Col3
FROM
anotherTable
ORDER BY
Col1

--Select from the table variable with no ORDER BY clause.
SELECT
*
FROM
@aTable


Thanks!
Ninja's_RGR'us
Ninja's_RGR'us
SSC Guru
SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)

Group: General Forum Members
Points: 68531 Visits: 9671
There are 0 exceptions to that rule. If you want order by you MUST use order by.
DCF
DCF
Valued Member
Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)Valued Member (64 reputation)

Group: General Forum Members
Points: 64 Visits: 70
Great, thanks!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search