When and Why does some one decide that they need to create a View in their database? Why not just run a normal stored procedure or select?
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- What is the best way to cache a table from a (SQL)
- php PDO::FETCH_ASSOC doesnt detect select after ba
The one major advantage of a view over a stored procedure is that you can use a view just like you use a table. Namely, a view can be referred to directly in the
FROM
clause of a query. E.g.,SELECT * FROM dbo.name_of_view
.In just about every other way, stored procedures are more powerful. You can pass in parameters, including
out
parameters that allow you effectively to return several values at once, you can doSELECT
,INSERT
,UPDATE
, andDELETE
operations, etc. etc.If you want a View's ability to query from within the
FROM
clause, but you also want to be able to pass in parameters, there's a way to do that too. It's called a table-valued function.Here's a pretty useful article on the topic:
http://databases.aspfaq.com/database/should-i-use-a-view-a-stored-procedure-or-a-user-defined-function.html
EDIT: By the way, this sort of raises the question, what advantage does a view have over a table-valued function? I don't have a really good answer to that, but I will note that the T-SQL syntax for creating a view is simpler than for a table-valued function, and users of your database may be more familiar with views.
Here is how to use a View along with permissions to limit the columns a user can update in the table.
I usually create views to de-normalize and/or aggregate data frequently used for reporting purposes.
EDIT
By way of elaboration, if I were to have a database in which some of the entities were person, company, role, owner type, order, order detail, address and phone, where the person table stored both employees and contacts and the address and phone tables stored phone numbers for both persons and companies, and the development team were tasked with generating reports (or making reporting data accessible to non-developers) such as sales by employee, or sales by customer, or sales by region, sales by month, customers by state, etc I would create a set of views that de-normalized the relationships between the database entities so that a more integrated view (no pun intended) of the real world entities was available. Some of the benefits could include:
We create view to limit or ristrict from accessing all rows/column in a table.If the owner wants that only specific or limited rows/column needs to be shared,then he will create a view with those column.
I am creating xxx that maps all the relationships between a main table (like Products table) and reference tables (like ProductType or ProductDescriptionByLanguage). This will create a view that will allow me retrieve a product and all it's details translated from its foreign keys to its description. Then I can use an ORM to create objects to easily build grids, combo boxes, etc.
Several reasons: If you have complicated joins, it is sometimes best to have a view so that any access will always have the joins correct and the developers don;t have to remember all the tables they might need. Typically this might be for a financial application where it would be extremely important that all financial reports are based on the same set of data.
If you have users you want to limit the records they can ever see, you can use a view, give them access only to the view not the underlying tables and then query the view
Crystal reports seems to prefer to use views to stored procs, so people who do a lot of report writing tend to use a lot of views
Views are also very useful when refactoring databases. You can often hide the change so that the old code doesn't see it by creating a view. Read on refactoring databases to see how this work as it is a very powerful way to refactor.