Solved

How to sort while inserting

Posted on 2006-06-25
7
224 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
7 Comments
 
LVL 142

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now