Hi Folks,
This article is quite related to View Limitation in SQL Server 2008. Although it's one of the core features of SQL Server, there are many limitations associated with it.
A few that I have encountered are listed below:
- You can't create a parameterized view, in other words you can't create a view with a parameter.
For example:
CreateView vw_OrdersNorthwind
@OrderID int
As
select CustomerID,ShipCity,ShipCountryfrom orders orderby OrderID desc
go
Error: It will give you an incorrect syntax error.
- Views are not based on temporary tables, if we try to create one then it gives us a massage.
An example is as follows:
Step 1: Temp table creation
createtable ##MobileDetails
( ID int NOTNull ,MobileNamenvarchar(50),CompanyName nvarchar (50))
Step 2: Insert a few records into the TempTable:
insertinto ##MobileDetailsvalues (1,'Galaxy S2','Samsung')
insertinto ##MobileDetailsvalues (1,'Nokia Lumia','Nokia')
insertinto ##MobileDetailsvalues (1,'IPhone5','IPhone');
insertinto ##MobileDetailsvalues (1,'Blackberry Z10','Blackberry');
Step 3: Creation of a view on the TempTable.
createview vw_onTempTable
as
select MobileName,CompanyNamefrom ##MobileDetails
go
An error prompted by the SQL Server after running the preceding command.
Msg 4508, Level 16, State 1, Procedure vw_onTempTable, Line 3
Views or functions are not allowed on temporary tables. Table names that begin with '#' denote temporary tables.
- You can't use an order by clause at the time of view creation. Kindly have a look at an example below:
Let's run the following command in the query editor of SQLServer:
CreateView vw_OrdersNorthwind
As
select OrderID,CustomerID,ShipCity,ShipCountryfrom orders orderby OrderID desc
go
It issues the error:
Msg 1033, Level 15, State 1, Procedure vw_OrdersNorthwind, Line 3
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
It clearly specifies to use TOP or FOR XML in your TSQL.
Now I make some changes in the preceding query and run it again.
CreateView vw_OrdersNorthwind
As
selecttop 100 OrderID,CustomerID,ShipCity,ShipCountryfrom orders orderby OrderID desc
goNow select the records from the View and run this query.
Select* from vw_OrdersNorthwind

- All the tables referenced by the view must be in the same database as the view.
- An indexed view must be created with the SCHEMABINDING option. This option prohibits the schema of the base tables from being changed, for example adding or dropping a column.
- If you add any new column to a table tehn it would not be reflected in the View untill you won't run the
EXEC sp_refreshview 'ViewName'.
Don't use Select *, just use a select specific columnnames
It's a best practice to create a view with SCHEMABINDING using this, the base table will not be modified.
- You can't use count (*) in a view creation query, for example:
CreateView vw_OrdersNorthwind
As
--select OrderID,CustomerID,ShipCity,ShipCountry from orders
selectcount(*) from orders
go
It also forces you to supply any column value.
Msg 4511, Level 16, State 1, Procedure vw_OrdersNorthwind, Line 4
Create View or Function failed because no column name was specified for column 1.
Workaround of this issue is use the following syntax:
CreateView vw_OrdersNorthwind
As
--select OrderID,CustomerID,ShipCity,ShipCountry from orders
select count(*) As Total from orders
go
As
--select OrderID,CustomerID,ShipCity,ShipCountry from orders
select count(*) As Total from orders
go
select * from vw_OrdersNorthwind

Sachin KaliaPosted Jul 2, 2013, 7:04 AM
Thanks Lokesh ,for reading an article,while i've already mentioned in an article, how can we use ordey by in view..This is code snippet from my article.It clearly specifies "how to use TOP or FOR XML in your TSQL". CreateView vw_OrdersNorthwind As selecttop 100 OrderID,CustomerID,ShipCity,ShipCountryfrom orders orderby OrderID desc go Now select the records from the View and run this query.Please have a look once again if you get a chance,Thanks.
Lokesh VijPosted Jul 1, 2013, 3:22 AM
SELECT TOP 100 PERCENT col1,col2, col3 FROM TABLE_NAME ORDER BY col1,col2,col3;
Lokesh VijPosted Jul 1, 2013, 3:21 AM
Hey Sachin, I did find Jim's feedback, appropriate. This is what was in my mind while reading your article. Adding on to the the point "that a view cannot have order by clause", as Jim said that it can with a TOP clause. Here is the example of the same. "SELECT TOP 100 PERCENT col1,col2, col3 FROM TABLE_NAME"
Guest UserPosted Jun 30, 2013, 9:43 PM
To say you can't use count(*) in a view is misleading. Someone reading that may think the actual functionality count(*) provides is not usable. The bullet point should really be about column names in a view, not the use of count(*). select intcolumn * 5 from dbo.table will not work for the same reasons count(*) does not work. If you are trying to help people understand hurdles with views you need to point out the true reasons something is not working, not just the symptoms.
Sachin KaliaPosted Jun 30, 2013, 1:11 AM
Jim my friend ,What is wrong in this statement ":You can't use count (*) in view creation query".you are saying its a requirement of view that you need to select column name .I know its a requirement as it states in an error that : Create View or Function failed because no column name was specified for column 1.But if you put "select count(*) from table" in your query it means you are doing something wrong which is not view acceptable,it also means you can't put such TSQL.while we can run such statement most frequently in TSQL in context to SQL. I agree about SCHEMABINDING and prevents you from altering or dropping a column that is used by the view.Now after addinng and droping a column and still i don't select newly added column than what's the worth, offcouse it will work fine ,because there is no impact of newly modified column in the depandent view.I am not concentrating on the best practise.Core focus is on where hurdles may come. Its really pleasure to know your valuable feedback.
Guest UserPosted Jun 29, 2013, 2:57 PM
You can't use count (*) in view creation query --- this is still an invalid statement. All columns in a view require a column name. That's not a workaround, that's a requirement of a view.
Guest UserPosted Jun 29, 2013, 2:55 PM
SCHEMABINDING only prevents you from altering or dropping a column that is used by the view. You can still add and drop columns that are not used by the view. If you view does select * then it is using all columns, even though it is bad practice to do so.
Guest UserPosted Jun 29, 2013, 2:53 PM
All the tables referenced by the view must be in the same database as the view. --- this is just not true. A view can be created over tables in different databases, and even on different servers via linked servers.
Sachin KaliaPosted Jun 29, 2013, 1:50 PM
I really appreciate for your valuable feedback,Pleasure is all mine. Some where I may agree with your statement, however you took most approaches towards different. direction also. 1. You can't create parameterized view, Means you can't create View with parameter. This is correct but can be imitated by creating an inline table valued function.(I. know this is an alternate(workaround) of this, While I was targeting only about View). 2. You can't use order by clause at time of view creation. Kindly have a look into an example below: You contradict this in your article. Answer : Yes it directly stops you to avail the benefits of order by clause in you TSQL , and show the well defined error to overcome on this. 3.Indexed view must be created with the SCHEMABINDING option. This option prohibits the schema of the base tables from being changed e.g. adding or dropping a column. SCHEMABINDING does not prohibit a column from being added to the base table. SCHEMABINDING only prevents a column used in the from from being changed or. dropped.Answer(Somewhere it means base table schema is being changed.adding or dropping a column is also changes into the table and offcourse I would stop you for. amendments) 4.EXEC sp_refreshview 'ViewName' This is only necessary if your view is doing select *, which is bad practice anyway. Answer.Yes we need to execute this query whenever we fire select * command.it doesn't make sense if we are reteriving same count of columns after new addition. Create View vw_OrdersNorthwind. As select * from orders. --select count(*) as COU from orders. go ALTER TABLE Orders. Add NewCol int. GO select * from Orders. if I run the following coomand. select NewCol from vw_OrdersNorthwind. Msg 207, Level 16, State 1, Line 1. Invalid column name 'NewCol'. now I run on demand command: EXEC sp_refreshview 'vw_OrdersNorthwind'. select NewCol as col from vw_OrdersNorthwind. Now it runs. While I made some amendments into existing article whatever required. Would like to say that there is always scope of improvement. Cheers Jim See you soon.
Guest UserPosted Jun 29, 2013, 10:13 AM
Your article contains so many errors and misinformation it's just frightening it can be published. Please do some actual research, check your facts, and verify your findings before posting another article. 1. You can't create parameterized view, Means you can't create View with parameter. This is correct but can be imitated by creating an inline table valued function. 3. You can't use order by clause at time of view creation. Kindly have a look into an example below: You contradict this in your article. You can use the order by clause but to do so you must use the top clause as well. 4. All the tables referenced by the view must be in the same database as the view. This is just wrong. A view can reference tables in other databases, and even in linked servers. The only place where this comes into play is if you schemabind your view, and that is more of a limitation of schemabinding than the view. 5. All the tables referenced by the view must have the same owner as the view. Again, totally incorrect, even with schemabinding. 6. Indexed view must be created with the SCHEMABINDING option. This option prohibits the schema of the base tables from being changed e.g. adding or dropping a column. SCHEMABINDING does not prohibit a column from being added to the base table. SCHEMABINDING only prevents a column used in the from from being changed or dropped. There are many additional rules about indexed views. If you are going to mention indexed views you should mention the additional rules as well. 7. EXEC sp_refreshview 'ViewName' This is only necessary if your view is doing select *, which is bad practice anyway. 8. You can't use count (*) in view creation query .e.g. Again, incorrect. The column requires an alias so the view has a column name to use, but all aggregates are acceptable, unless the view is going to be an indexed view. 9. The AVG, MAX, MIN, VAR, aggregate functions aren't allowed in the SELECT statement Once again, totally incorrect for views. These aggregates are not allowed in indexed views.