I'm using SQL Server 2005/2008 R2
My SQL script is:
UPDATE MyView
SET MyColumn = 'My new value'
WHERE ID = 7
The error message:
Msg 271, Level 16, State 1, Line 1
The column "MyColumn" cannot be modified because it is either a computed column or is the result of a UNION operator.
I know that MyColumn
is a computed column from the base table (like when FullName
is computed from LastName + ', ' + FirstName
)
My aim is to catch the error so the script keeps executing.
I tried the following script with no luck. The error is not caught:
BEGIN TRY
UPDATE MyView
SET MyColumn = 'My new value'
WHERE ID = 7
END TRY
BEGIN CATCH
-- Error occurred while updating view. The script will keep running
END CATCH
I checked the following scripts trying to solve the problem, with no luck:
Script 1
SELECT is_computed
FROM sys.columns c, sys.views v
WHERE c.object_id = v.object_id
AND v.name = 'MyView'
The previous script returns 0 for all results (which is not correct, one of the column is a computed column)
Script 2:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'MyView'`
Also I couldn't find any field from the returned script that has something to do with 'computed column' or 'result of a union' operation.