Create & Modify Views

Source: Internet
Author: User

First, build a view
IF object_id ('sales.ordertotalsbyyear'V' is not NULL    DROP VIEW sales.ordertotalsbyyear; GO

Example one:

CREATE VIEWSales.ordertotalsbyyear withSCHEMABINDING,--when modifying the table or view used to generate the current view, modifications are not allowed once the current view is affected (causing the view to become invalid). Encryption--encryption, cannot be edited (encryption level is not high) as    SELECT   Year(o.orderdate) asOrderYear,SUM(Od.qty) asQty fromSales.orders asOJOINSales.orderdetails asOd onOd.orderid=O.orderidGROUP  by  Year(OrderDate);GO

Example two:

CREATE VIEWDbo.viewtestindexinfo as    SELECT DISTINCTEmployees.*     fromEmployeesJOINSales onEmployees.EmployeeID=Sales.employeeidWHERETitle= 'Sales Person' with CHECK OPTION   --If there is a where statement in the view, it is possible to update the table with view to modify the row outside the Where condition, which is used to force the changed content to match the Where condition
Second, view the view in the database
--to explore view metadata using T-SQL, you can query the Sys.views catalog view: UseTSQL2012;GOSELECTNameobject_id, principal_id, schema_id, type fromsys.views;--You can also query the INFORMATION_SCHEMA. TABLES system view, but it's slightly more complex:SELECTschema_name, TABLE_NAME, Table_type frominformation_schema. TABLESWHERETable_type= 'VIEW';
Third, modify the view
ALTER VIEWSales.ordertotalsbyyear withSCHEMABINDING as    SELECTO.shipregion, Year(o.orderdate) asOrderYear,SUM(Od.qty) asQty fromSales.orders asOJOINSales.orderdetails asOd onOd.orderid=O.orderidGROUP  by  Year(OrderDate), o.shipregion;GO
Iv. Modifying records through views
-- adding records through views  If the view has the check option, the inserted data must follow the WHERE condition insert  vemployees          SELECT  3  ,                'xxx'  ,                '  xx'

Create & Modify Views

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.