I have a query that must return information for an invoice. It is not straight forward though. Some things need to be displayed from the if something else is happening...
For example...
declare @.idintdeclare @.chargeidintset @.id = 1set @.chargeid = 9declare @.ppbit, @.pagesint, @.pagefeemoney, @.pagechargemoneyset @.pp = (select perpagefrom tblmainfeeswhere mainfeesid = @.chargeid)if (@.pp = 1)beginset @.pages = (select pagesfrom tblchargesinnerjoin tblrequeston requestid = fkrequestidwhere requestid = @.id)if (@.pages > 0)beginset @.pagefee = (select perpagefeefrom tblmainfeeswhere mainfeesid = @.chargeid)set @.pagecharge = (@.pages * @.pagefee)endend...select @.pagechargeas PerPageCharge
Or should I do some complex query with only one select?
Its hard for anyone to understand if your question is like this: "Some things need to be displayed from the if something else is happening..."
Give us some sample data and your query and what you are expecting to see out of it.
|||Your right, sorry about that. I slid that one in quick before a meeting...
I will try to explain better.
I have stored procedure that returns data for an invoice, and because Ihate Crystal Reports, I do the most work I can in the stored procedure (formating the text, etc...).
The current query I have involves a 2 complex queries with tons of nested case statements. It is hard to read and does not flow nicely at all, but it works.
I just made enough changes to the database that the stored procedure needs to be rewritten.
I took this opportunity to try and clean up what I wrote prior. I posted a small expample above.
To me, I made it almost "VB" like. Instead of complex select statements with nested logic, I made a variable for each item I needed, and some extra variables for logic. I set each of these variables with it's own select statement, if they passed the logic i had layed out (most of them were initialized with a value). Again, to me, I envisioned each variable being set like a property or a function in VB. I ended up having over 15 simple selects to set variables, and one select at the end to return (get) all of the variables.
The code ended up being much easier to read, and I cut the original code in half, but I greatly increased the amount of select statements. I did some time statistics to see which was faster, and the new query ended up being twice as fast, doing the same work.
My question: Is the way I described and used an accepted way practice? Did I make it some what clearer of what I did or am trying to do?
|||I would post some data and examples, but I don't think it's relevant. I am not looking for how to do something, but the best way to do something.
For example: "Are you stupid? Don't do it like that." or "Yeah, that's fine.
"
No comments:
Post a Comment