Alias / CTE alternative

  • 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.

  • 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.

  • 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[/url]
    Learn Extended Events

  • 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 ...

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply