Store calculations in a table or use computed columns?

  • the devil is in the details, but here's my two cents:

    use computed columns or a view which does the computation for you whenever possible.

    i don't know if you've ever tried it, but storing a formula in a field, and then trying to execute the value of the field to get the results is a real pain in the... whatever.

    try it with a simple example...

    declare @MyCalculation varchar(30)

    SET @MyCalculation ='SELECT 1 + 2+ 3'

    EXEC (@MyCalculation)

    --or worse COLUMNA + COLUMNB

    see if you can get the results back...it's a pain.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thanks Lowell, I'll start working on using computed columns.

    If anyone else pictures an even better way when reading this post, please share your ideas 🙂

  • As Yogi Bera used to say, "When you come to a fork in the road, take it.". If you can suscessfully create a computed column, try to make it a "persisted" computed column.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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