SQL refresh View

Source: Internet
Author: User
The view stores metadata, columns, security, dependencies, and other information. If we change the architecture of the basic table, it is not directly reflected in the view. After the architecture is changed, it is a good habit to use sp_refreshview stored procedure to refresh view metadata;

The view stores metadata, columns, security, dependencies, and other information. If we change the architecture of the basic table, it is not directly reflected in the view. After the architecture is changed, it is a good habit to use sp_refreshview stored procedure to refresh view metadata;

For example, we created a table T1 and a T1 view V1, then changed T1, and then looked at the V1 results:
Create Table T1 first:
The Code is as follows:
IF OBJECT_ID ('T1 ') IS NOT NULL
Drop table T1
Create table T1 (col1 INT, col2 INT)
Insert into T1 (col1, col2) VALUES (1, 2)
GO

Then create the T1 view V1:
The Code is as follows:
Create view V1
AS
SELECT * FROM T1


In practice, avoid using * in the SELECT statement in the view. This is only a demonstration. If you query view V1, the following results are displayed:


Next, we will add a column col3 to table T1:
The Code is as follows:
Alter table T1 ADD col3 INT

Then you can query view V1 again. Do you want to query three columns or columns? The answer is two columns. The change in T1 architecture does not affect the view metadata. At this time, if we need to refresh the view V1, we can use the EXEC sp_refreshview V1 command to query it again, the V1 result is three columns.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.