79491324

Date: 2025-03-07 05:19:11
Score: 0.5
Natty:
Report link

Why the NVARCHAR column in the base table is made a VARCHAR type in the view ?

It's because you have first created table and view and then after creating view you altered column size in table.

Any schema change in base table not reflects automatically in the view.

How can I have it in the view as NVARCHAR ?

You have two options:

  1. If schema change is intended and you want to reflect in view then execute "sp_refreshview" stored procedure immediately after column changes:
EXEC sp_refreshview V_ACC_POL
  1. if schema change is not intended which is done by some another developer and you want to prevent this kind of unintended changes then create view with schema binding option which will prevent others by modifying table schema:
ALTER VIEW [dbo].[V_ACC_POL]
WITH SCHEMABINDING
AS
SELECT 
    [ID]
    ,[Name]
FROM
    [dbo].[ACC_POL]

This will give below error if someone tries to update table schema:

Msg 5074, Level 16, State 1, Line 12
The object 'V_ACC_POL' is dependent on column 'Name'.
Msg 4922, Level 16, State 9, Line 12
ALTER TABLE ALTER COLUMN Name failed because one or more objects access this column.
Reasons:
  • Blacklisted phrase (0.5): How can I
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): Why the
  • Low reputation (0.5):
Posted by: Harsh Varde