Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Alias / CTE alternative Rate Topic Display Mode Topic Options
Author
 Message
 Posted Friday, March 22, 2013 6:16 PM
 SSC Rookie Group: General Forum Members Last Login: Monday, August 19, 2013 8:08 PM Points: 43, Visits: 566
 More often than not I am wrong about my logic towards database problem solving. Hence, I ask your opinion on this logic:Problem:1. Alias cannot be reference to a calculated field in a stored procedure2. CTE's can be a bit ... so so... to deal with.3. You have to type each calculation, over and over and over again if one calculation depends on another.My solution:Create a physical temporary table that stores the data.Update each column of the temp data table using a single stored procedure one column at a time.Example:employees over the age of 60 do not contribute to insurance scheme... hence ... I would have something like this...employee name date of birth Age Earnings Insurance Contribution net pay where age is a calculated field based on date of birth and insurance contribution is also a calculated field based on age and net pay is based on earnings less insurance contribution!My solution is simple:have the above as a temp table....Update the age fieldupdate the insurance contributionupdate the net pay (earnings - ic)run my report from that table... then delete the temp data.Can anyone advise what the disadvantages of using this approach will be? My main concern is speed...Generally its a theory.... say you have 10 calculated fields each dependent on another.... Comments welcomed.Thanks.
Post #1434554
 Posted Saturday, March 23, 2013 12:42 AM
 SSC-Insane Group: General Forum Members Last Login: Yesterday @ 5:16 PM Points: 23,522, Visits: 37,745
 Without seeing the table structure, data, the different calculations that would be completed and what the data would look like when process was completed (the expected results), it is really hard to provide any concrete advise here.Personally, I would look for a set-based way to accomplish the task in one pass if at all possible. Break it down if needed, particularly if the divide and conquer would help performance as well as data integrity.
Post #1434575
 Posted Saturday, March 23, 2013 1:17 AM
 SSC-Insane Group: General Forum Members Last Login: Monday, November 21, 2016 11:03 AM Points: 20,009, Visits: 18,255
 Generally, I'd opt for a single update statement. If that means retyping the calculations for dependent columns - so be it. You create a stored procedure and your done with it. Jason AKA CirqueDeSQLeilI have given a name to my pain...MCM SQL Server, MVPSQL RNNRPosting Performance Based Questions - Gail Shaw
Post #1434580
 Posted Saturday, March 23, 2013 6:45 AM
 SSC Rookie Group: General Forum Members Last Login: Monday, August 19, 2013 8:08 PM Points: 43, Visits: 566
 Thanks for the reply. I guess the only way to find out is to actually try it. Gonna let the forum know what my results are ... Speed wise, i figured an update stored procedure shouldn't take more than 5 seconds to run on 20,000 records. If this works, then surely it's a good tips to use the temp table as a work around for calling alias in stored procedures ...
Post #1434603

 Permissions