When should I use stored procedures?

前端 未结 19 2551
眼角桃花
眼角桃花 2020-12-04 10:53

When should I be using stored procedures instead of just writing the logic directly in my application? I\'d like to reap the benefits of stored procedures, but I\'d also lik

19条回答
  •  孤城傲影
    2020-12-04 11:17

    Complicated database queries for me tend to end up as stored procs. Another thought to consider is that your database might be completely separate and distinct from the application. Lets say you run an Oracle DB and you essentially are building an API for other application developers at your organization to call into. You can hide the complicated stuff from them and provide a stored proc in its place.

    A very simple example:

    registerUser(username, password)
    

    might end up running a few different queries (check if it exists, create entries in a preference table, etc) and you might want to encapsulate them.

    Of course, different people will have different perspectives (a DBA versus a Programmer).

提交回复
热议问题