Views Behaviours in MS SQL Server
I am going to explain some abnormal behaviors of the Database Views in MS SQL Server 2008 and will compare the with MYSQL Database....
As per the MSDN the definition of the view is : "A view can be thought of as either a virtual table or a stored query. The data accessible through a view is not stored in the database as a distinct object. What is stored in the database is a SELECT statement. The result set of the SELECT statement forms the virtual table returned by the view. A user can use this virtual table by referencing the view name in Transact-SQL statements the same way a table is referenced".
So if we see the above definition from the MSDN then we can say that views are not stored in the database as table. It is only the reference. But practically it have some different behavior. Lets try the following code in MS SQL Server 2008.
As per the MSDN the definition of the view is : "A view can be thought of as either a virtual table or a stored query. The data accessible through a view is not stored in the database as a distinct object. What is stored in the database is a SELECT statement. The result set of the SELECT statement forms the virtual table returned by the view. A user can use this virtual table by referencing the view name in Transact-SQL statements the same way a table is referenced".
So if we see the above definition from the MSDN then we can say that views are not stored in the database as table. It is only the reference. But practically it have some different behavior. Lets try the following code in MS SQL Server 2008.
USE [master] GO IF EXISTS (SELECT name FROM sys.databases WHERE name = N'TestDatabase') DROP DATABASE [TestDatabase] GO CREATE DATABASE [TestDatabase] GO USE [TestDatabase] GO IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TestView]') AND type in (N'U')) DROP TABLE [dbo].[TestView] GO CREATE TABLE [dbo].[TestView]( [Col1] [int] NULL, [Col2] [varchar](50) NULL, [Col3] [varchar](50) NULL, [Col4] [varchar](50) NULL ) GO INSERT INTO [TestDatabase].[dbo].[TestView]([Col1],[Col2],[Col3],[Col4]) VALUES(1,'val1','val2','val3') INSERT INTO [TestDatabase].[dbo].[TestView]([Col1],[Col2],[Col3],[Col4]) VALUES(2,'val01','val02','val03') INSERT INTO [TestDatabase].[dbo].[TestView]([Col1],[Col2],[Col3],[Col4]) VALUES(3,'val11','val12','val13') INSERT INTO [TestDatabase].[dbo].[TestView]([Col1],[Col2],[Col3],[Col4]) VALUES(4,'val21','val22','val23') GONow creating view on the table TestView with statement Select * from table as follows :
USE [TestDatabase] GO IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[VW_TestView]')) DROP VIEW [dbo].[VW_TestView] GO CREATE VIEW [dbo].[VW_TestView] AS SELECT * FROM dbo.TestView GONow we will select the values from the view as follows:
USE TestDatabase GO SELECT * FROM VW_TestView GOSo think what should be the output? Yes you are right , it will give the same as the select statement on the table. It will return all the columns and rows from the table.See below result from SQL Server 2008
Now let us change one thing, Add one more column to the table and update the value of the column with some data as follows:
USE TestDatabase GO ALTER TABLE TestView ADD Col5 VARCHAR(50) GO UPDATE TestView SET Col5='New Value' GONow Again Run the query to select values from the view as we did earlier. Now think what should be the output of the query? Ideally as per the view definition, It should return all the columns and rows from table or the select statement on the view should be same as the select statement on the table.But the behavior is little bit different here. I don't know why it is so but it returns only those columns which was at the time of creation of the view here it was 4 columns. So the query will return only 4 columns with values. Now think why the fifth column we have added is not coming in the view. It should be there because we have created the view with Select * from the table statement and as per the view definition it stores the query only, So I am considering here that when we created the view with select * from table statement then it might stores the query with column names like Select col1,col2.... etc so that's why it is not selecting the fifth column.But when I queried the database for the view definition actually stored using the below query then result was amazing. It stores the query as it is i.e. select * from table .. see the below query and output
USE TestDatabase GO SELECT TEXT,* FROM SYSCOMMENTS WHERE ID IN(SELECT object_id FROM SYS.VIEWS WHERE NAME ='VW_TestView') GO

Now , lets do the reverse of what we did i.e. remove the one column from the table and then again check for the view. Using the below query
USE [master] GO ALTER TABLE TestView DROP COLUMN Col2 GO SELECT * FROM VW_TestView GO
Now guess what should be the out put of the select * query on view? If you are thinking that it will not return the Col2, then you are wrong. It will return the same result as above. see the below screen shot

USE [master] GO CREATE VIEW VW1_ViewTable AS SELECT Col1,Col3,Col4,Col5 FROM TestView GO SELECT * FROM VW1_ViewTableSo its clear from this step that it will return all the columns specified in the view. Now lets do a experiment, Add one more column to the table and again run select * query on the view, So it again return only those columns which was used in creating the view. Now lets delete one column from the table which was specified in the view. So view should through error when we run select * from view query as follows.

print.

something can be found related to this @ http://www.ssw.com.au/ssw/kb/KB.aspx?KBID=Q1000592
ReplyDelete