SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Alias / CTE alternative


Alias / CTE alternative

Author
Message
sdhanpaul
sdhanpaul
SSC-Enthusiastic
SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)

Group: General Forum Members
Points: 109 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 procedure
2. 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 field
update the insurance contribution
update 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.
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)SSC Guru (94K reputation)

Group: General Forum Members
Points: 94193 Visits: 38955
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.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66235 Visits: 18570
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 CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

sdhanpaul
sdhanpaul
SSC-Enthusiastic
SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)

Group: General Forum Members
Points: 109 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 ...
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search