{"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> Creating Sample Data:<\/em><\/p>\n Check Option:<\/strong> Example:<\/p>\n Lets try to inset\/Update data in view vw_TempWithOutCheckOption.<\/p>\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 As we are trying to insert\/Update data which would not be part of the view, we are getting the error: Encryption:<\/strong><\/p>\n Encryption can be used when you want to hide the definition of the View.<\/p>\n The definition of the view that is created with encryption is not present in sys.syscomments.<\/p>\n SchemaBinding:<\/strong> It does not allow to drop the table dbo.tempForView as the view is referencing it and it is created with SCHEMABINDING.<\/p>\n VIEW_METADATA:<\/strong>
\nCHECK OPTION
\nENCRYPTION
\nSCHEMABINDING
\nVIEW_METADATA<\/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
\nCheck option ensures that the modification of data in the view will come up in the view again.<\/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
\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
\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
\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\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
\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
\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
\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