Alex
Alex

Reputation: 2468

How to check if computed column is persisted?

How to check if computed column is persisted? (MS SQL Server)

Upvotes: 6

Views: 4926

Answers (2)

Stefan Steiger
Stefan Steiger

Reputation: 82196

Necromancing.
If you need all columns, or just the computed columns, or just the persisted computed columns, you can do it with one script:

SELECT * 
FROM sys.columns AS sysc

LEFT JOIN sys.computed_columns AS syscc 
    ON syscc.object_id = sysc.object_id 
    AND syscc.name = sysc.name 

WHERE (1=1) 
AND sysc.is_computed = 1 -- optional 
AND is_persisted = 1  -- optional 

Upvotes: 0

Andrew
Andrew

Reputation: 27294

Computed column attributes are available in sys.computed_columns.

select * from sys.computed_columns where is_persisted = 1

is_persisted = 1 for the column being persisted, 0 otherwise.

You can link this back to sys.tables via the object_id e.g.

select t.name, c.name
from sys.tables t
inner join sys.computed_columns c on c.object_id = t.object_id
where c.is_persisted = 1

And change your where clause to include the table name / field name as appropriate to your scenario.

Upvotes: 11

Related Questions