Solved

Stored Procedure/View order change

Posted on 2008-10-29
8
251 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

735 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