To auto generate values in column based on values from other columns

  • I have a table with 15,000 rows. I have created a new column on the same table. I would like the new column to have the value

    (column A + '-'+ Column B ). Please help me with the SQL code guys. Thanks in advance

    ...Kumar

  • Unless you have a compelling need to do otherwise, that should be done with either a view or a calculated column.

  • Hi Pam,

    I am Learning , Therefore it is not that i need to do that. I wanted to know if that was possible. Will the values auto populate if i create a computed column ? I actually am able to get the values as required by using a view as you suggested. Thank You.

    ...Kumar

  • Depends upon what you mean by "auto populate". Here's an explanation from BOL:

    "Unless otherwise specified, computed columns are virtual columns that are not physically stored in the table. Their values are recalculated every time they are referenced in a query. The SQL Server 2005 Database Engine uses the PERSISTED keyword in the CREATE TABLE and ALTER TABLE statements to physically store computed columns in the table. Their values are updated when any columns that are part of their calculation change. By marking a computed column as PERSISTED, you can create an index on a computed column that is deterministic but not precise."

  • Pam,

    Is it that if i create a computed column and mark it as persisted, the computed values in the columns will be physically stored . I was looking for some solution like this. Thanks a lot for the advice/suggestion . Do you have a link to any e-learning web page where they have details on how to generate a computed column and mark it as persisted. Can we create a computed column on a existing table, and will the values for the existing rows be generated ?

    ...Kumar

  • While it's not something that I have tested, the documentation appears to say that the data would be persisted in the computed column when the root/source columns' data changes.

    Books Online (help for SQL Server) is really the best source. Just look up computed columns.

Viewing 6 posts - 1 through 5 (of 5 total)

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