We currently have some reports that query a specific table, but one of our users wants the ordering to be different. I don't want to go through and fix each of the reports, because that's rather a mammoth task, and only one user has a problem here. Is there a way to have SELECT queries on a base table automagically SELECT from a view table instead of the base table based on the user making the query? It would be a bonus if I can also base it on the program making the connection. We're using MSSQL 2008.
ie. SELECT * FROM myTable would instead be SELECT * FROM myView if the user executing the query was [user] and the program making the connection was [reporting software name].
EDIT:
Note: The outer reporting software is an old version of Crystal Reports running some massive ugly queries built by the query builder.
Some sample output would be:
Regular Guy: Other Guy:
ItemCode ItemCode
AAAAAA745 AAAAAA745
AAABBB672 AAACCC657
AAACCC657 BBBAAA766
BBBAAA766 BBBCCC383
BBBBBB838 AAABBB672
BBBCCC383 BBBBBB838
CCCBBB883 CCCBBB883
As we want to order all items with SUBSTRING(ItemCode, 4,3) = 'BBB' at the bottom.
ORDER BYclause? Please tell me the rest of your users are relying on the "default" order that results fromSELECT * FROM tablewith noORDER BYclause - this ordering is coincidental and if you don't tell SQL Server how you want the results ordered, it is free to choose any ordering it wants. While today you might be getting the order you want, you might not tomorrow - or next week - or next month. Statistics updates, service packs, hotfixes, even aRECONFIGUREcan change the plan that is currently being used. – Aaron Bertrand Jun 12 '12 at 0:32ORDER BYclause, but it won't fix the root problem. The reason they're not specifying it in anORDER BYclause is because they want to have a set of item codes appearing together, based on a character sequence in the middle of the string. The reporting software would do this, but I would have to go through every report to change it, and it would be a rather tedious task. – Hotchips Jun 12 '12 at 0:54SUBSTRING(field, x,y) + SUBSTRING(field, 0, z) AS nameor name them differently or somesuch. The report ordering would do the rest (and hopefully not break anything... I wish). – Hotchips Jun 12 '12 at 1:18