Computed Column cannot be Persisted

前端 未结 4 1319
自闭症患者
自闭症患者 2020-12-29 21:46

I have a custom function, and I am trying to created a persisted column using this function.

It is giving me following error.

Computed column

4条回答
  •  悲哀的现实
    2020-12-29 22:42

    Create a non-computed column of appropriate datatype. Create an Insert trigger (and an Update trigger if data will change). You can then persist the function output in a column.

    ALTER TABLE SomeTable ADD FormattedSSN VARCHAR(11)
    GO
    
    CREATE TRIGGER dbo.TR_I_SomeTable ON  dbo.SomeTable AFTER INSERT
    AS 
    BEGIN
        SET NOCOUNT ON;
    
        update s
        set s.FormattedSSN = dbo.FormatSSN()
        from SomeTable AS s
            join inserted i on i.id = s.id
    
    END
    GO
    

提交回复
热议问题