Sql server computed field

  • Create table #Test

    (

    emp_id int

    emp_Birth_Date

    emp_Age int AS datediff(d,emp_Birth_Date, getdate())

    )

    ....this computed field cannot work....i need the field to automatically calculate the differece and retains number as results.....any help.

    thanks.

  • What do you mean by 'cannot work'? Is it giving an error? Is it not giving the values you want? Something else?

    Calculated columns are not stored, they're calculated when the column is queried.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • giving an error message (Incorrect syntax near the keyword 'AS')

  • giving an error message (Incorrect syntax near the keyword 'AS')

  • You don't specify a data type when creating a computed column. The data type depends on the result of the expression.

    Create table #Test (

    emp_id int,

    emp_Birth_Date DATETIME,

    emp_Age AS datediff(d,emp_Birth_Date, getdate())

    )

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

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

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