views:

60

answers:

1

How can I make Sybase's database engine return an unsorted list of records in non-numeric order?

~~~

I have an issue where I need to reproduce an error in the application where I select from a table where the ID is generated in sequence, but the ID is not the last one in the selection.

Let me explain.

ID    STATUS
_____________
1234   C
1235   C
1236   O

Above is 3 IDs. I had code where these would be the results of a

select @p_id = ID from table where (conditions).

However, there wasn't a clause to check for status = 'O' (open). Remember Sybase saves the last returned record into a variable.

~~~~~

I'm being asked to give the testing team something that will make the results not work. If Sybase selects the above in an unordered list, it could appear in ascending order, or, if the database engine needs to change blocks of stored data or something technical magic stuff, the order could be messed up. The original error was when the procedure would return say 1234 instead of 1236.

Is there a way that I can have a 100% guarantee that Sybase will search over a block of data and have to double back, effectively 'breaking' the ascending search, and returning not the last record, but any other one? (all records except the maximum will end up erroring, because they are all 'Closed')

edit: I want some sort of magical SQL code that will make sure things don't search the table in exactly numeric order. Ideally I'd like to not have to change the procedure, as the test team want to see the exact same procedure breaking (as easy as plonking a order by id desc would fudge the results)

+2  A: 

If you don't specify an order, there is no way to guarantee the return order of the results. It will be however the index is built - and can depend on the order of insertion, the type of index, and the content of index keys.

It's generally a bad idea to do those sorts of singleton SELECTs. You should always specify a specific record with the WHERE clause, or use a cursor, or TOP n or similar. The problem comes when someone tries to understand your code, because some databases when they see multiple hits take the first value, some take the last value, some take a random value (they call that "implementation-defined"), and some throw an error.

Is this by any chance related to 1156837? :)

lavinio
Apart from changing the select order, is there any other way? Ideally I need to be able to change something outside the procedure where this logic is being executed, so it can be 'properly tested'.And yes, this is related to 1156837.. but this time I need to make it break.
glasnt
To the best of my knowledge, there is no external way to force this behavior. The variables behind the behavior are hidden - not deliberately, but because it is behavior what comes from a misuse of the language.
lavinio
One more comment - IF you could get it to happen, you could **save** that database, and just restore it each time to run the test. However, if **anything** changed - the data, or even the installation of the Sybase engine due to a patch - then all bets are off.
lavinio
I was guessing as much.Thanks for that lavinio. I'll just have to explain to the test team yet again that I can't make it break for them :)
glasnt