Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / General / Other SQL Server Topics / November 2007

Tip: Looking for answers? Try searching our database.

Create table and default order by clause

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
D. - 26 Nov 2007 13:30 GMT
Hi,
I have this problem.

My software creates a temporary table (#MyTable).
This table should be used by a report engine and printed each time with
different "order by" clause, depending on some parameters (and the program
that creates the temporary table obviously knows these parameters...)

Now,  I don't want to pass these paramete to the report engine, because I
want that the logic of the report will stay only in the program that create
the table (the report engine should onnly do a "SELECT * FROM #MyTable").

So, I'm asking if there is a way to define, for a table, a default "order
by" clause to use when no "order by" clause is specified in a "select" query
statement on that table.

If not, I think the only alternative is to create a view on that table. Is
it correct?

Thanks,
Davide.
Dan Guzman - 26 Nov 2007 13:53 GMT
> So, I'm asking if there is a way to define, for a table, a default "order
> by" clause to use when no "order by" clause is specified in a "select"
> query
> statement on that table.

No, the only way to guarantee result order is to specify ORDER BY in the
SELECT statement that selects from the table.

> If not, I think the only alternative is to create a view on that table. Is
> it correct?

No, the only way to guarantee result order is to specify ORDER BY in the
SELECT statement that selects from the view.  Although ORDER BY can be
specified in a view along with TOP, it will not guarantee ordering.

Note that it is likely, but not guaranteed, that you will get results
ordered in sequence by the table's clustered index in a trivial query with
no joins or where clause.  However, it's risky to rely on behavior because
it may change between SQL Server service packs and releases.

Signature

Hope this helps.

Dan Guzman
SQL Server MVP

> Hi,
> I have this problem.
[quoted text clipped - 19 lines]
> Thanks,
> Davide.
Plamen Ratchev - 26 Nov 2007 13:59 GMT
Hi Davide,

The table is unordered set and you have to explicitly use ORDER BY if you
need to retrieve data in particular order. Even a view will not work, as you
cannot use ORDER BY in a view, unless the TOP clause is specified (and in
that case the ORDER BY is used only to determine the rows returned by the
TOP clause, you still have to use ORDER BY when selecting from the view to
return ordered set).

Since your software controls the creation of the temporary table, it may be
easier to add <sort> column to the table and to populate with values based
on the parameters. Then the report engine can still use static SQL, like
"SELECT <columns> FROM #MyTable ORDER BY sort".

HTH,

Plamen Ratchev
http://www.SQLStudio.com
Erland Sommarskog - 26 Nov 2007 22:42 GMT
> My software creates a temporary table (#MyTable).
> This table should be used by a report engine and printed each time with
[quoted text clipped - 12 lines]
> If not, I think the only alternative is to create a view on that table. Is
> it correct?

As the others have said: the only way to be guaranteed to get an ordered
result is to use ORDER BY. There is no way around that.

However, you could create your table with a rowno column which you
populate with the row_number() function (available from SQL 2005), and
the report engine could do "SELECT ... FROM #MyTable ORDER BY rowno".

Signature

Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

D. - 27 Nov 2007 08:32 GMT
Thanks to everyone,
I think the I will pass the order by clause to the report engine.
Madhivanan - 28 Nov 2007 14:30 GMT
> Thanks to everyone,
> I think the I will pass the order by clause to the report engine.

Yes it is. If you want to show data in reports, better order the
result there
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.