{"id":273,"date":"2013-04-30T15:00:01","date_gmt":"2013-04-30T15:00:01","guid":{"rendered":"https:\/\/sqlserverlearner.azurewebsites.net\/2013\/04\/30\/all-about-sql-server-views\/"},"modified":"2013-04-30T15:00:01","modified_gmt":"2013-04-30T15:00:01","slug":"all-about-sql-server-views","status":"publish","type":"post","link":"https:\/\/sqlserverlearner.com\/2013\/04\/30\/all-about-sql-server-views\/","title":{"rendered":"All about SQL Server Views"},"content":{"rendered":"

Advanced topics in SQL Server Views:<\/strong>
\nCHECK OPTION
\nENCRYPTION
\nSCHEMABINDING
\nVIEW_METADATA<\/p>\n

Creating Sample Data:<\/em><\/p>\n

\n--DROP TABLE tempForView\nCREATE TABLE tempForView(\nID INT\n);\nGO\n\nINSERT INTO tempForView\nSELECT 1\nUNION\nSELECT 2\nUNION\nSELECT 3;\nGO\n<\/pre>\n

Check Option:<\/strong>
\nCheck option ensures that the modification of data in the view will come up in the view again.<\/p>\n

Example:<\/p>\n

\nCREATE VIEW vw_TempWithOutCheckOption\nAS\nSELECT * fROM tempForView WHERE ID > 0\nGO\n\nCREATE VIEW vw_TempWithCheckOption\nAS\nSELECT * fROM tempForView WHERE ID > 0\nWITH CHECK OPTION;\nGO\n<\/pre>\n

Lets try to inset\/Update data in view vw_TempWithOutCheckOption.<\/p>\n

\nUPDATE vw_TempWithOutCheckOption SET ID = -1 WHERE ID = 1\nGO\n\nINSERT INTO vw_TempWithOutCheckOption(ID)\nSELECT -2\nGO\n\nSELECT * FROM vw_TempWithOutCheckOption\nGO\n<\/pre>\n

The above query runs and the data will be inserted\/Updated in the underlying table but will not come up in the view.<\/p>\n

Now when we try to insert update data in the view vw_TempWithCheckOption <\/p>\n

\nUPDATE vw_TempWithCheckOption SET ID = -2 WHERE ID = 2\nGO\n\nINSERT INTO vw_TempWithCheckOption(ID)\nSELECT -3\nGO\n\nSELECT * FROM vw_TempWithCheckOption\nGO\n<\/pre>\n

As we are trying to insert\/Update data which would not be part of the view, we are getting the error:
\n
\nMsg 550, Level 16, State 1, Line 2
\nThe attempted insert or update failed because the target view either specifies WITH CHECK OPTION or spans a view that specifies WITH CHECK OPTION and one or more rows resulting from the operation did not qualify under the CHECK OPTION constraint.
\nThe statement has been terminated.
\n<\/font><\/p>\n

Encryption:<\/strong><\/p>\n

Encryption can be used when you want to hide the definition of the View.<\/p>\n

\nCREATE VIEW vw_TempWithEncryption\nWITH ENCRYPTION\nAS\nSELECT * fROM tempForView WHERE ID > 0;\nGO\n\nCREATE VIEW vw_TempWithOutEncryption\nAS\nSELECT * fROM tempForView WHERE ID > 0;\nGO\n\n\nSELECT TEXT fROM sys.syscomments WHERE ID = OBJECT_ID('vw_TempWithEncryption')\nSELECT TEXT fROM sys.syscomments WHERE ID = OBJECT_ID('vw_TempWithOutEncryption')\n<\/pre>\n

The definition of the view that is created with encryption is not present in sys.syscomments.<\/p>\n

SchemaBinding:<\/strong>
\nWhen a view is created with schema binding then it does not allow DDL modifications to the underlying tables which affect the view definition.<\/p>\n

\nCREATE VIEW vw_TempWWithSchemaBinding\nWITH SCHEMABINDING\nAS\nSELECT ID fROM dbo.tempForView WHERE ID > 0;\nGO\n\nDROP TABLE dbo.tempForView\n<\/pre>\n

It does not allow to drop the table dbo.tempForView as the view is referencing it and it is created with SCHEMABINDING.<\/p>\n


\nMsg 3729, Level 16, State 1, Line 1
\nCannot DROP TABLE ‘dbo.tempForView’ because it is being referenced by object ‘vw_TempWWithSchemaBinding’.
\n<\/font><\/p>\n

VIEW_METADATA:<\/strong>
\nwhen the applications accessing SQL Server request browse-mode metadata SQLServer basically provides the details of the TABLES from which the columns in the views are built.
\nBut when a view is created with this option then SQL Server does not provide the base tables of the columns in the view.<\/p>\n

More info can be found in MSDN:
\nhttp:\/\/msdn.microsoft.com\/en-us\/library\/ms187956.aspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

Advanced topics in SQL Server Views: CHECK OPTION ENCRYPTION SCHEMABINDING VIEW_METADATA Creating Sample Data: –DROP TABLE tempForView CREATE TABLE tempForView( ID INT ); GO INSERT INTO tempForView SELECT 1 UNION SELECT 2 UNION SELECT 3; GO Check Option: Check option ensures that the modification of data in the view will come up in the view…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-273","post","type-post","status-publish","format-standard","hentry","category-sql-server-basics"],"_links":{"self":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/comments?post=273"}],"version-history":[{"count":0,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/273\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/media?parent=273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/categories?post=273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/tags?post=273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}