Which is better: Ad hoc queries or stored procedures? [closed]

In my experience writing mostly WinForms Client/Server apps these are the simple conclusions I’ve come to:

Use Stored Procedures:

  1. For any complex data work. If you’re going to be doing something truly requiring a cursor or temp tables it’s usually fastest to do it within SQL Server.
  2. When you need to lock down access to the data. If you don’t give table access to users (or role or whatever) you can be sure that the only way to interact with the data is through the SP’s you create.

Use ad-hoc queries:

  1. For CRUD when you don’t need to restrict data access (or are doing so in another manner).
  2. For simple searches. Creating SP’s for a bunch of search criteria is a pain and difficult to maintain. If you can generate a reasonably fast search query use that.

In most of my applications I’ve used both SP’s and ad-hoc sql, though I find I’m using SP’s less and less as they end up being code just like C#, only harder to version control, test, and maintain. I would recommend using ad-hoc sql unless you can find a specific reason not to.

Leave a Comment