• OCTom (7/21/2010)


    It's a great question and something that needed research on my part. Thanks for the question.

    Has anyone used computed columns and why?

    Thanks.

    Frequently, and for general ease-of-use reasons mostly.

    Say for (a very simple) example you have FirstName and LastName columns, and often need to produce a results set that contains a FullName field of the format FirstName + ' ' + LastName.

    You can either:

    - Use a view, but that has the disadvantage that you need to rewrite all queries to use the view instead of the table.

    - Do it in the select statement each time you need the FullName, but that has the disadvantage that you might have to repeat code in lots of places.

    - Create the FullName column and keep it populated with a trigger on the other two columns, but that has an overhead for INSERTs and UPDATEs and perhaps a little for management.

    - Use a computed column. Doesn't affect any existing queries, doesn't affect inserts or updates (unless persisted I think, but that's another story) and is a one-time-only operation to set up.

    - There may be yet other alternatives of course, but I can't think what they might be off the top of my head.

    Horses for courses as they say.

    Duncan