Sql views vs stored procs

  • Yes, I know they are not the same thing but why would you use one over the other in a given circumstance?

    TIA,

    Bill

     

     

  • proc more like a function, can do many things, better security from sql injection, encapsulation of functions, etc.

    the view can do similar security, but at a grosser level. Only functions as a table. Updates can only be to one table, two statements in a proc could update two tables.

    What are you trying to do?

  • A view is a derived table. ( in relational terms )

    A procedure is some written code of arbirary complexity to perform some task.

    Also procedures can have parameters.

    If you just need a "view" of some data use a view.

    If you are performing a task use a sp.

    In sql server there is a third option: udf's that return a table.

    e.g.

    Putting together a report:

    option 1 - use a view

    option 2 - use a proc.

    I would go 2.

    Why:

    The proc takes parameters to filter the user selection criteria.

    With a view the client would have to build a dynamic where clause.

    The proc can contain extra logic such as logging users, and execution time etc..

    Just som quick thoughts.

    /rockmoose


    You must unlearn what You have learnt

  • Good thoughts, Rockmoose and Steve.

    We have a team of developers that use views and stored procs in the same DB. I don't understand, and I will ask them tomorrow, why they find views so "attractive". Views have always seemed less capable than procs to me.

  • Ok if were to say something positive about views, it might be this.

    They Do provide an additional "abstraction" layer between the database and the client.

    In this layer security and formatting of data can take place.

    It might also hide some complexity of the sql for the developers.

    But this "abstraction" layer comes with a cost, and that is added complexity in the system,

    this will probably add just as many, or more problems than it solves.

    This is all my personal opinion of course 😉

    /rockmoose


    You must unlearn what You have learnt

  • It is a Zen thing in some ways, but you can't really encapsulate much business logic in a view. Hide some joins or whatever, but no real hard core code. I find that we use (at my day job) procs over views by probably a 10 to 1 ration or more.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply