Solved

How to sort while inserting

Posted on 2006-06-25
7
265 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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 500 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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

729 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