Calculation on a computed column

  • Hi,

    Does anyone know how to do calculations on a computed column? I'm a novice at this an d I'm not sure if it can be done but this is the code that I'm trying to do:

    CREATE TABLE [dbo].[thu1] (

    [CoID] [int] NULL ,

    [ManID] [int] NULL ,

    [CustID] AS ([CoID] + [ManID]),

    [Test] AS (dbo.thu1.CustID + 1),

    ) ON [PRIMARY]

    GO

    --please help

    From listening comes wisdom^M^M^M+++AT0


    From listening comes wisdom^M^M^M+++AT0

  • Your computed column can be a noncomputed column name, constant, function, variable, and any combination of these connected by one or more operators. So possibly this would work for you:

    CREATE TABLE [dbo].[thu1] (

    [CoID] [int] null,

    [ManID] [int] null ,

    [CustID] AS ([CoID] + [ManID]),

    [Test] AS ([CoID] + [ManID] + 1)

    ) ON [PRIMARY]

    GO

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

  • Thanks for your reply but I'm looking into avoiding listing the fields again and just use the calculated column name with a new calculation. Is this possible?

    Thanks,

    From listening comes wisdom^M^M^M+++AT0


    From listening comes wisdom^M^M^M+++AT0

  • Another possible solution would be to have a insert and update trigger that would update the [TEST] column thu1.CustID + 1 from the inserted table.

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

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

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