Solved

Stored Procedure/View order change

Posted on 2008-10-29
8
248 Views
Last Modified: 2012-05-05
I have a stored procedure that creates a pivot table with data from a view in SQL Sever 2000. Neither the sp or the view has changed in months. However for some reason the order in which the data is being returned from the stored procedure has just changed - this is causing huge problems as the output is copied straight to an excel spreadsheet and now its all messed up - the data is the same it is just the order of the data that has changed
Any idea what factors could influence the an order change in a sp/ view where the input data is exactly the same. Indexes is perhaps on thing that I need to look at - can u think of anything else??
0
Comment
Question by:mydo76
8 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 22836113
On what columns are your data sorted?  What are those columns' data types?  What is different about
the output now vs the output in the past?
0
 

Author Comment

by:mydo76
ID: 22836700
one input parameter of the stored procedure is a list of months e.g. '200700', '200801',
'200802', '200803', '200804', '200805' and creates a pivot table where the input yyyymm list are the column names and fiills them with data. eg
'200700', '200801', '200802', '200803', '200804', '200805',
123          111          100          5000       3000        1245.1 (I have to check the type)

this is out to an excel spreadsheet - the return data from the sp should be and was in the fact in this order
'200700', '200801', '200802', '200803', '200804', '200805',
123          111          100          5000       3000        1245.1

but its coming out now as --

'200802' '200805',   '200700', '', '200803', '200804', '200801'
100          1245.1     123               5000       3000        111

ie the data is correct its just the order that is incorrect.
I need to check the colums the data is sorted on etc tomorrow
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 22837136
>>Any idea what factors could influence the an order change in a sp/ view where the input data is exactly the same. <<
The lack of an appropriate ORDER BY clause would explain it.  However without seeing the actual stored procedure, there is no tellin.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 22837139
I should have also added that unless you are using an ORDER BY clause the order of the data is not guaranteed.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 5

Expert Comment

by:ursangel
ID: 22841529
Yes its True that without proper order by clause, you cannot have a guarantee on the order in which the result is retrieved. SQL server has its on internal method of retrieving the records from the database. May be all these time, the order was what you ver expected, but since it has changed now, make it use ORDER BY Clause.
0
 

Author Comment

by:mydo76
ID: 22842641
I fixed this myself !! It was a problem with a temporary table - when I inserted the data to a permanent table it rectified the problem!
0
 

Accepted Solution

by:
mydo76 earned 0 total points
ID: 22842668
I should also add that I put a clustered index on the appropiate field! Thanks for all the suggestions!
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 22846525
>>I should also add that I put a clustered index on the appropiate field!<<
All you have done is mask the problem.  Until you add an ORDER BY clause you have no guarantee that the results will be returned in the same order. Period.
0

Featured Post

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.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

910 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

20 Experts available now in Live!

Get 1:1 Help Now