• Andrew Diniz (7/3/2012)


    On a serious note, where is a view's meta-data kept and under what circumstances might a view return inconsistent results as a result of base table schema changes?

    The metadata is stored in internal tables that you cannot access directly. You can access them through several object management views, including sys.objects, and sys.views for the view itself, sys.columns for the columns defined in a view, and sys.sql_modules for the definition.

    Here is one of many possible ways to demonstrate inconsistent results after base table schema changes:

    CREATE TABLE dbo.MyTab (IntCol int, CharCol char(1));

    INSERT INTO dbo.MyTab (IntCol, CharCol) VALUES(1, 'a');

    go

    CREATE VIEW dbo.MyView

    AS SELECT * FROM MyTab;

    go

    SELECT * FROM dbo.MyView;

    go

    ALTER TABLE dbo.MyTab DROP COLUMN IntCol;

    ALTER TABLE dbo.MyTab ADD NumCol numeric(5,3);

    go

    UPDATE dbo.MyTab SET NumCol = 1;

    go

    SELECT * FROM dbo.MyView;

    go

    DROP VIEW dbo.MyView;

    DROP TABLE dbo.MyTab;

    go


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/