What is the difference between a stored procedure and a view?

后端 未结 10 1878
星月不相逢
星月不相逢 2020-12-04 04:59

I am confused about a few points:

  1. What is the difference between a stored procedure and a view?

  2. When should I use stored procedures, and whe

相关标签:
10条回答
  • 2020-12-04 05:41

    A SQL View is a virtual table, which is based on SQL SELECT query. A view references one or more existing database tables or other views. It is the snap shot of the database whereas a stored procedure is a group of Transact-SQL statements compiled into a single execution plan.

    View is simple showcasing data stored in the database tables whereas a stored procedure is a group of statements that can be executed.

    A view is faster as it displays data from the tables referenced whereas a store procedure executes sql statements.

    Check this article : View vs Stored Procedures . Exactly what you are looking for

    0 讨论(0)
  • 2020-12-04 05:42

    A view represents a virtual table. You can join multiple tables in a view and use the view to present the data as if the data were coming from a single table.

    A stored procedure uses parameters to do a function... whether it is updating and inserting data, or returning single values or data sets.

    Creating Views and Stored Procedures - has some information from Microsoft as to when and why to use each.

    Say I have two tables:

    • tbl_user, with columns: user_id, user_name, user_pw
    • tbl_profile, with columns: profile_id, user_id, profile_description

    So, if I find myself querying from those tables A LOT... instead of doing the join in EVERY piece of SQL, I would define a view like:

    CREATE VIEW vw_user_profile
    AS
      SELECT A.user_id, B.profile_description
      FROM tbl_user A LEFT JOIN tbl_profile B ON A.user_id = b.user_id
    GO
    

    Thus, if I want to query profile_description by user_id in the future, all I have to do is:

    SELECT profile_description FROM vw_user_profile WHERE user_id = @ID
    

    That code could be used in a stored procedure like:

    CREATE PROCEDURE dbo.getDesc
        @ID int
    AS
    BEGIN
        SELECT profile_description FROM vw_user_profile WHERE user_id = @ID
    END
    GO
    

    So, later on, I can call:

    dbo.getDesc 25
    

    and I will get the description for user_id 25, where the 25 is your parameter.

    There is obviously a lot more detail, this is just the basic idea.

    0 讨论(0)
  • 2020-12-04 05:42
    1. A VIEW is a dynamic query where you can use a "WHERE"-Clause
    2. A stored procedure is a fixed data selection, which returns a predefined result
    3. Nor a view, nor a stored procedure allocate memory. Only a materialized view
    4. A TABLE is just one ENTITY, a view can collect data from different ENTITIES or TABLES
    0 讨论(0)
  • 2020-12-04 05:45

    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.

    1. 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 and ORDER BY.

    2. The depends on what you do.

    3. 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).

    4. I don't understand what you're talking about.

    0 讨论(0)
提交回复
热议问题