Results 1 to 6 of 6

Thread: Putting Logic in stored procedure

  1. #1

    Thread Starter
    Member upstream's Avatar
    Join Date
    Jun 2007
    Location
    Philippines
    Posts
    48

    Lightbulb Putting Logic in stored procedure

    hi everyone,

    i was developing a windows app. and im putting business logic in the stored procedures if applicable, e.g instead of getting data in the database and process it in the application, i just get the data that the stored procedure has already processed.

    question is, is this a good practice? or it is still better to process data within the application level?

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Putting Logic in stored procedure

    I have had 10 years of really great success having nearly 100% of the business logic in my stored procedures.

    Releasing an enhancement that only requires a SPROC change is truly a good time!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Putting Logic in stored procedure

    It's a mixed bag... I've done a lot of BL in the SPs, and so has slamany... and it works well for what it is... but it works best when it's set-based... loops and large data processing on individual rows can lead to problems. I tend to put it where it makes sense... sometimes I find it easier to put data together in SQL sometimes... like aggregates... so, eh.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: Putting Logic in stored procedure

    I've found that it's good practice to either put as much logic in your stored procedures as possible or none at all. Anything in between results in a mixed design which is confusing and difficult to maintain.
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Putting Logic in stored procedure

    There are different views on this, and often the individual's opinion depends on their background. Someone who comes from a DB background will generally want as much logic in the database as possible, while those coming from a high-level language background will want the opposite. Certainly, databases are optimised for performing certain tasks so performing those tasks in the database can lead to performance gains, especially if it reduces the amount of data that gets moved from the database to the application. Another advantage is that the logic is available to any client that accesses the database, e.g. you can access data in an application, a scheduled export and a web report can all make use of the same logic when executing the same sprocs.

    That said, putting business logic in the database seems to go directly against the separation of concerns that is the aim of many design patterns. You can also put the logic into a DLL and use it from multiple clients that way. There are certain things that a database will be worse at than applications too, so putting that logic into a database will result in a performance hit. I certainly agree with ntg is that you really should take a hybrid approach, but if you try to put individual logic where it will be executed most efficiently can lead to just that. For that reason, I would tend to recommend against putting your business logic into sprocs. I would agree with szlamany that upgrading an app by running a script is appealing, but tools like ClickOnce make upgrading multiple clients much less of a chore so it may not be as big an advantage as it was.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Member upstream's Avatar
    Join Date
    Jun 2007
    Location
    Philippines
    Posts
    48

    Re: Putting Logic in stored procedure

    Thanks everyone!

    I will probably use techgnome approach. for single row return and does not require loops, i will just put the logic in the stored procedure, other than that i'll put the logic in the application level. I will just properly document my source code so it wont be confusing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width