?
Solved

How to sort while inserting

Posted on 2006-06-25
7
Medium Priority
?
267 Views
Last Modified: 2012-05-05
Hello experts,

I have a database app with many tables and stored procs. Some stored procedures do insert into tamporary tables. My insert statements also include select statements. When I select I use order by clause but after insert, the rows are not ordered by the column name in order by clause. Any idea?

Thanks
0
Comment
Question by:sunny012097
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16980168
if you query the temp tables without ORDER BY clause, they are never guaranteed to come out "ordered" by the select.
you have to apply a ORDER BY when selecting from the temp table also
0
 

Author Comment

by:sunny012097
ID: 16980210
Here is a sample query from a stored procedure:
I want rows to be ordered by SessionDate which correspons to day column in the report_montly_value table

            
      INSERT INTO report_monthly_value (day,time_scale,inv_cost_val,inv_retail_val)
                  SELECT ins.SessionDate, RIGHT(CONVERT(char(9), ins.SessionDate, 6), 6), SUM(ind.ItemCOST * ind.ItemQTY), SUM(ind.ItemRETAIL * ind.ItemQTY)
                  FROM Inv_Data as ind, Inv_Sessions as ins
                  WHERE ins.ClientID  = @client_id AND
                        ins.ID = ind.SessionID AND
                        ind.InventorySTATUS = 'POST'
                  GROUP BY ins.SessionDate
                  ORDER BY ins.SessionDate
0
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 16980236
sunny, don't worry about insert order, let SQL take care of that internally.

If you want to see the results ordered, then do this:
SELECT day,time_scale,inv_cost_val,inv_retail_val
FROM report_monthly_value
WHERE -- Whatever you need to filter on here
ORDER BY day

And just like angelIII posted, NEVER assume the order will be what you want, add an ORDER BY clause.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:sunny012097
ID: 16980244
I have some reports that use the data in tables and they down't pull the data sorted. I don't want to fix all the reports one by one.
is there any way that I can order the rows in a table with a command?
0
 

Author Comment

by:sunny012097
ID: 16980246
I want to sort the rows somehow without select...
0
 
LVL 26

Accepted Solution

by:
DireOrbAnt earned 1500 total points
ID: 16980261
I guess if you make day the Clustered Index on report_monthly_value, it might order them properly.
As a plus, your queries, if based on date range, will run faster.

Now, you understand that this is not a good practice, might not work, or ordering might change at any time :)
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 16980683
>>I want to sort the rows somehow without select...<<
As DireOrbAnt has pointed out already you can hope that using a Clustered Index will always return the results in the right order and that by the time that changes you have long since moved on to better pastures or you can do it right.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question