I am confused about a few points:
What is the difference between a stored procedure and a view?
When should I use stored procedures, and when should I use views, in SQL Server?
Do views allow the creation of dynamic queries where we can pass parameters?
Which one is the fastest, and on what basis is one faster than the other?
Do views or stored procedures allocate memory permanently?
What does it mean if someone says that views create a virtual table, while procedures create a materials table?
Please let me know about more points, if there are any.
Plenty of info available on the web like this
Here is a good summary:
A Stored Procedure:
A View:
A view is a simple way to save a complex
SELECT
in the database.A store procedure is used when simple SQL just isn't enough. Store procedures contain variables, loops and calls to other stored procedures. It's a programming language, not a query language.
Views are static. Think of them as new tables with a certain layout and the data in them is created on the fly using the query you created it with. As with any SQL table, you can sort and filter it with
WHERE
,GROUP BY
andORDER BY
.The depends on what you do.
The depends on the database. Simple views just run the query and filter the result. But databases like Oracle allow to create a "materialized" view which is basically a table which is updated automatically when the underlying data of the view changes.
A materialized view allows you to create indexes on the columns of the view (especially on the computed columns which don't exist anywhere in the database).
I don't understand what you're talking about.
Mahesh is not quite correct when he suggests that you can't alter the data in a view. So with patrick's view
I CAN update the data ... as an example I can do either of these ...
or
You can't INSERT to this view as not all of the fields in all of the table are present and I'm assuming that PROFILE_ID is the primary key and can't be NULL. However you can sometimes INSERT into a view ...
I created a view on an existing table using ...
THEN
and
Both the INSERT and the DELETE worked in this case
Obviously you can't update any fields which are aggregated or calculated but any view which is just a straight view should be updateable.
If the view contains more than one table then you can't insert or delete but if the view is a subset of one table only then you usually can.
First you need to understand Both are different things. Stored procedures are best used for INSERT-UPDATE-DELETE statements. and Views are used for SELECT statements. and you should use both.
In views you cannot alter the data.Some databases have updatable Views where you can use INSERT-UPDATE-DELETE on Views.@Patrick is correct with what he said, but to answer your other questions a View will create itself in Memory, and depending on the type of Joins, Data and if there is any aggregation done, it could be a quite memory hungry View.
Stored procedures do all their processing either using Temp Hash Table e.g #tmpTable1 or in memory using @tmpTable1. Depending on what you want to tell it to do.
A Stored Procedure is like a Function, but is called Directly by its name. instead of Functions which are actually used inside a query itself.
Obviously most of the time Memory tables are faster, if you are not retrieveing alot of data.