Which is better: Ad hoc queries or stored procedures?

Question :

Which is better: Ad hoc queries or stored procedures?,

Answer :

Assuming you can’t use LINQ for whatever reason, is it a better practice to place your queries in stored procedures, or is it just as good a practice to execute ad hoc queries against the database (say, SQL Server for argument’s sake)?


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.
Read More  Best way to draw text with OpenGL and Cocoa?

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.

Read More  Centralizing/controlling arbitrary builds of .NET projects and solutions

That’s the answer Which is better: Ad hoc queries or stored procedures?, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you