views:

854

answers:

17

Possible Duplicate:
Inline SQL vs Stored Procedures

I have 100% ad hoc sql through out my application. A buddy of mine recommended that I convert to stored procedures for the extra performance and security. This brought up a question in my mind, besides speed and security is there any other reason to stick with ad hoc sql queries?

A: 

Ad-hoc queries give you flexibility in your application logic, but you almost always pay a price on performance.

If you are concerned with performance, I'd probably agree with your friend that you should look into stored procs, or some more static form of query, to allow the database to "pre-optimize" the query, or allow the caching layer (if one exists) to potentially cache query results.

If you generate the query on the fly every time, the database will most likely not be able to help you at all with performance.

Andy White
In general, saying that stored procedures are faster than ad-hoc SQL is not the best guidance. See for example http://www.codinghorror.com/blog/2010/03/compiled-or-bust.html
Andomar
+16  A: 

There's nothing about stored procedures that makes them magically speedier or more secure. There are cases where a well-designed stored proc can be faster for certain types of tasks, but the reverse is also true for ad hoc SQL.

Code the way you find most productive.

"Make it right before you make it faster." -- Brian Kernighan

Bill Karwin
Bill, i'm going to call you on that. There is one thing that makes store procedures faster than Ad-Hoc SQL and that is that they are pre-compiled. There is no compile time involved in calling a store proc as there is with using ad-hoc SQL. That said, the difference is minimal. You are right however in saying that there is no security difference. Parameterised queries = store procs in security
TerrorAustralis
@Dave: The compilation plan for both ad-hoc SQL and stored procedures is cached. Calling a procedure the first time is as slow as calling ad-hoc SQL for the first time. Plans for procedures are more general; but like Bill says, that can be a good or a bad thing, depending on the circumstances
Andomar
My point is that one cannot claim that using stored procs is better or worse as a general rule. There are a lot of exception cases either way, so we just have to accept that there's no silver bullet.
Bill Karwin
And based on the query, your index might help or it might not. SQL Server can also make bad decisions at query planning time..
lb
There can be security differences if you want to assign permissions only to stored procedures...but i don't see this as either a plus or negative to the argument
davidsleeps
A: 

My answer might be slightly off topic, but anyways:

It could be useful when working with views. Hibernate (that you are not using) for instance, has pretty bad support for views. When I need to query a view with it, I always use raw SQL, since that is the only thing that works.

Lars Andren
A: 
  1. You don't need to write a stored procedure
  2. Generally ad-hoc SQL is fast enough. You can use parametrized queries to increase speed.
  3. You can compile string based SQL to "compiled" SQL, and then execute that, which is much faster.
  4. Typically the performance bottleneck for SQL is the queries not any of the above.
Parameterised queries dont increase speed, they merely increase security. Also, the time taken to compile the SQL IS the difference between a stored procedure and ad-hoc SQL, so if you are compiling the SQL you are only doing the work that the database would do anyway when it receives your query, only the DB does it faster :)
TerrorAustralis
Umm, not quite. 1. Parameterized SQL is generally faster because at the very least you are not doing string concatenation, and likely causes 2. The database is always doing SQL compilation. If you use that resultant compiled SQL, it doesn't need to re-apply optimization's or check to see if the string you passed it matches an ealier query.
On point 2, true. I thought you were talking about an external query compiler, not using the databases compiled SQL. @1, i havent tried the string concatenation vs parameterisation in a profiler, but the performance gain would be so tiny you wouldnt notice it, but i would imagine the overhead involved in using/setting parameters is greater than a properly built string concatenation. Might try this out...
TerrorAustralis
@Dave: SQL Server caches ad-hoc queries only for the exact same variable values. Parameterized queries are saved for any variable value. In my experience, this gives a considerable speed benefit. There's even a setting to have SQL Server parameterize your query, for the lazy dev http://blog.sqlauthority.com/2009/03/21/sql-server-2008-optimize-for-ad-hoc-workloads-advance-performance-optimization/
Andomar
+1  A: 

Portability, when you need to switch to a new SQL server, and don't have access to the old one.

I've worked on a few projects where the original developer wouldn't give access to the server, after he was fired, so we had to re-write many queries ourselves because they were locked up in stored procedures that we didn't have permission to view. If all the queries were in the source code, it would have made it a lot easier.

Robert
@SmilingRob: That DB admin is lucky they didn't work for the City of San Francisco: http://www.tomsguide.com/us/networking-hacking-wifi-hack,news-6621.html. Also, table definitions and stored procedures should be treated like any other source code and managed in version control.
Eric J.
If you keep the scripts to create your stored procedures in source control, this isn't a problem. If the only place you keep the code to create your stored procedures is on your SQL server, you could be in a world of hurt if the server went down for any reason.
smencer
And quite how did he manage that? Take him to court...
Paddy
+11  A: 

If you are not writing stored procedures, investigate parameterized queries. If you build the SQL yourself including parameter concatenation, you're inviting a SQL injection attack.

Eric J.
I've heard that you need to create a TYPE for ultimate elegance.
lb
lb: I heard that for true nirvana, you have to create a soap service that delivers XML trhough an object relational mapper, so you can query the XML in the application with XPath, or better XQuery. But I also heard that at the end of the rainbow, you might find a pot of gold, and come to think of it, I actually never found one :((
Roland Bouman
+1  A: 

Truthfully, SQL injection can be prevented by Parameterising your queries (look into ODBCParameters for instance) and your queries can be constructed such that these parameters cannot be SQL injected. For instance...

DECLARE @param varchar(50)
SELECT @param = ?
SELECT * FROM TABLE WHERE NAME = @param

is a safe method of doing internal queries with ODBC parameters. However, there are some advantages to using Stored Procedures:

  1. Complex SQL with multiple functions or cursors can screw up if used in an ad-hoc manner as some ODBC drivers don't know how to handle multiple query requests
  2. It separates the business logic from the database calls. This allows you (the application developer) to know nothing about the structure of the database and still develop your application while a dedicated DBA or SQL developer can fine tune the SQL.
  3. Stored procedures are pre-compiled, so over many repetitions they will be faster, but ONLY if they are called extremely frequently (i.e. a monitoring program)

When all is said and done, its a design decision. Don't take portability into account, you can just have the SQL dev give you the scripts to apply the store procs and run them on installation of the program anyway :)

Hope this helps

TerrorAustralis
+6  A: 

There are a couple myths related to this topic that you should disabuse yourself of:

Myth 1: Stored procedures are pre-compiled
http://scarydba.wordpress.com/2009/09/30/pre-compiled-stored-procedures-fact-or-myth/

Myth 2: Ad Hoc SQL queries do not reuse execution plans: http://scarydba.wordpress.com/2009/10/05/ad-hoc-queries-dont-reuse-execution-plans-myth-or-fact/

IMHO procs have the edge when you absolutely need to lock down the database. In these situations, you can use an account that only has rights to execute stored procedures. Additionally, they can provide a layer of abstraction between your app and the database from the DBA perspective.

Likewise, dynamic SQL is better in situations where the query may need to change some and be... well... dynamic. Or if you know you have to port to multiple databases.

Both are just as safe in regards to SQL injection as long as all user inputted values are parameterized.

Daniel Auger
ScaryDBA vs MSDN...http://msdn.microsoft.com/en-us/library/aa174792(SQL.80).aspxi quote: stored procedures were a way to partially precompile an execution plan. At the time the stored procedure was created, a partially compiled execution plan was stored in a system table. Executing a stored procedure was more efficient than executing an SQL statement because SQL Server did not have to compile an execution plan completely, it only had to finish optimizing the stored plan for the procedure.
TerrorAustralis
@Dave: Have you actually read that article? The text you are quoting refers to v6, an ancient version of SQL Server. For newer onese: "SQL Server 2000 and SQL Server 7.0 do not save a partially compiled plan for stored procedures when they are created. A stored procedure is compiled at execution time, like any other Transact-SQL statement."
Andomar
Read that, i guess it depends which DB you are working on. Most of my experience is with older databases.
TerrorAustralis
Never contradict the ScaryDBA ;).
Daniel Auger
If you still work with database platforms released 15 years ago, the debate about stored procedures vs. ad-hoc queries is the least of your problems.
Isaac Cambron
+23  A: 

SQL Server caches the execution plans for ad-hoc queries, so (discounting the time taken by the first call) the two approaches will be identical in terms of speed.

In general, the use of stored procedures means taking a portion of the code needed by your application (the T-SQL queries) and putting it in a place that is not under source control (it can be, but usually isn't) and where it can be altered by others without your knowledge.

Having the queries in a central place like this may be a good thing, depending upon how many different applications need access to the data they represent. I generally find it much easier to keep the queries used by an application resident in the application code itself.

In the mid-1990's, the conventional wisdom said that stored procedures in SQL Server were the way to go in performance-critical situations, and at the time they definitely were. The reasons behind this CW have not been valid for a long time, however.

Update: Also, frequently in debates over the viability of stored procedures, the need to prevent SQL injection is invoked in defense of procs. Surely, no one in their right mind thinks that assembling ad hoc queries through string concatenation is the correct thing to do (although this will only expose you to a SQL injection attack if you're concatenating user input). Obviously ad hoc queries should be parameterized, not only to prevent the monster-under-the-bed of a sql injection attack, but also just to make your life as a programmer generally easier (unless you enjoy having to figure out when to use single quotes around your values).

Update 2: I have done more research. Based on this MSDN white paper, it appears that the answer depends on what you mean by "ad-hoc" with your queries, exactly. For example, a simple query like this:

SELECT ID, DESC FROM tblSTUFF WHERE ITEM_COUNT > 5

... will have its execution plan cached. Moreover, because the query does not contain certain disqualifying elements (like nearly anything other than a simple SELECT from one table), SQL Server will actually "auto-parameterize" the query and replace the literal constant "5" with a parameter, and cache the execution plan for the parameterized version. This means that if you then execute this ad-hoc query:

SELECT ID, DESC FROM tblSTUFF WHERE ITEM_COUNT > 23

... it will be able to use the cached execution plan.

Unfortunately, the list of disqualifying query elements for auto-parameterization is long (for example, forget about using DISTINCT, TOP, UNION, GROUP BY, OR etc.), so you really cannot count on this for performance.

If you do have a "super complex" query that won't be auto-parameterized, like:

SELECT ID, DESC FROM tblSTUFF WHERE ITEM_COUNT > 5 OR ITEM_COUNT < 23

... it will still be cached by the exact text of the query, so if your application calls this query with the same literal "hard-coded" values repeatedly, each query after the first will re-use the cached execution plan (and thus be as fast as a stored proc).

If the literal values change (based on user actions, for example, like filtering or sorting viewed data), then the queries will not benefit from caching (except occasionally when they accidentally match a recent query exactly).

The way to benefit from caching with "ad-hoc" queries is to parameterize them. Creating a query on the fly in C# like this:

int itemCount = 5;
string query = "DELETE FROM tblSTUFF WHERE ITEM_COUNT > " + 
        itemCount.ToString();

is incorrect. The correct way (using ADO.Net) would be something like this:

using (SqlConnection conn = new SqlConnection(connStr))
{
    SqlCommand com = new SqlCommand(conn);
    com.CommandType = CommandType.Text;
    com.CommandText = 
        "DELETE FROM tblSTUFF WHERE ITEM_COUNT > @ITEM_COUNT";
    int itemCount = 5;
    com.Parameters.AddWithValue("@ITEM_COUNT", itemCount);
    com.Prepare();
    com.ExecuteNonQuery();
}

The query contains no literals and is already fully parameterized, so subsequent queries using the identical parameterized statement would use the cached plan (even if called with different parameter values). Note that the code here is virtually the same as the code you would use for calling a stored procedure anyway (the only difference being the CommandType and the CommandText), so it somewhat comes down to where you want the text of that query to "live" (in your application code or in a stored procedure).

Finally, if by "ad-hoc" queries you mean you're dynamically constructing queries with different columns, tables, filtering parameters and whatnot, like maybe these:

SELECT ID, DESC FROM tblSTUFF WHERE ITEM_COUNT > 5

SELECT ID, FIRSTNAME, LASTNAME FROM tblPEEPS 
    WHERE AGE >= 18 AND LASTNAME LIKE '%What the`

SELECT ID, FIRSTNAME, LASTNAME FROM tblPEEPS 
    WHERE AGE >= 18 AND LASTNAME LIKE '%What the`
    ORDER BY LASTNAME DESC

... then you pretty much can't do this with stored procedures (without the EXEC hack which is not to be spoken of in polite society), so the point is mute.

Update 3: Here is the only really good performance-related reason (that I can think of, anyway) for using a stored procedure. If your query is a long-running one where the process of compiling the execution plan takes significantly longer than the actual execution, and the query is only called infrequently (like a monthly report, for example), then putting it in a stored procedure might make SQL Server keep the compiled plan in the cache long enough for it to still be around next month. Beats me if that's true or not, though.

MusiGenesis
Wow..this is a really good answer. Thanks
Luke101
Yes, excellent answer, thank you!
jscharf
One of the best answers I've read on this site.
Paddy
+2  A: 

I have 100% ad hoc sql through out my application. A buddy of mine recommended that I convert to stored procedures for the extra performance and security.

I would not worry about performance until there are actual pain points. For example, someone is using your application and complains that it's slow. Until you reach that point, your time is best spend improving your application.

In security, you have to balance effort versus risk. If your site doesn't store anything of value, even SQL Injection is a perfectly acceptable risk, as proven by a great number of web sites out there :)

Andomar
A: 

It may depend on who else is using the db. If only one application uses the db, then parametrized queries have the advantage of sitting in the source.

If other applications use the db, then the dba should put common stored procedures in the db.

wisty
I see the point, but I don't agree 100%. Why would a stored procedure be better than an application library that you share across applications?
Roland Bouman
@Roland: Stored procedures are better than an application library if you expect your data will outlast your application platform. Which is true for basically all big companies
Andomar
Andomar, in my opinion, stored procedures are just another appliction platform. The fact that they run inside the database doesn't magically make the closer to the data than proper application programs, even though many people feel that way. In fact, many organizations would be able to get a lot more out of their data if they could relieve themselves of that fallacy.
Roland Bouman
Yeah, it depends on the environment. A big company might standardize on Oracle or SQL Server, and different development groups might use python, .Net, C++ and Java to access the database. You could then go down the path of standard libraries across the languages (yuck), a network service which exposes a cross-platform API (which might be too hard for the dba team), or stored procedures in the db. It should be up to the team looking after the db what option to take, and they are likely to be SQL experts.
wisty
wisty - yes - this particular example is an excellent use case for stored procedures. Absolutely agree.
Roland Bouman
+2  A: 

I can't see when Ad-Hoc queries would give any benefits. Discussing with a friend this same question we found the following thing in favour of stored procedures (apart from the obvious caching/SQL Injection issues):

1) Code readability: If you have some hairy SQL Code embedded in your application, it becomes much harder to read/understand. It is much simpler to have a good Naming Convention to Stored Procedures that says exactly what it does, instead of a lot of code. It is more less the same principles that we try to use when refactoring.

2) Ability to improve your application without to redeploy: If you need to tweak your logic because of bugs or poor performance, having the SQL Code embedded in your application implies that you need to redeploy it (even if your dataset doesn't change). If you have it in a stored procedure, the only thing you need to redeploy is the procedure. Also, it gives the changes to a DBA to do its magic improving the overall performance of the query by working with the procedure. This would be much harder to do if you're working with the embedded version.

3) Network Traffic: If you're passing a lot of SQL Code around you're increasing the size of your message (or RPC calls) being transmitted over the network, which can lead to poor performance due to requests. Specially if many users to the same calls everytime.

I hope this helps.

Cheers, Wagner.

Wagner Silveira
About readability: even if you have a language that does not allow multiline literal strings, you can easily get around this by storing SQL statements in a file and reading that.About redeploy: If you modularize your application code and isolate each distinct piece that communicates with the database , you only have to redeploy that part of the app. Same difference.Rountrips is a fair point.
Roland Bouman
If you're going down to the paing of having your SQL statement stored in a file, why not stored procedures?About redeploying, even if you are isolating each distinct part, you might still have potential issues with assembly versioning etc.Notice I'm not saying is not doable. What I'm saying is that in those specific cases, Stored procedures make your life easier.
Wagner Silveira
Wagner, there's no pain involved. Just a proper separation of resources and logic. What you say about assembly versioning - I am not an expert in this field, but it sounds like you're talking about .NET, am I right? If the application platform is hassling you about those things, you probably chose the wrong platform. You shouldn't be punished for any other than true API changes.
Roland Bouman
When I said pain, what I mean was. If I'm adding the SQL in a separate file, and having to manage the loading of this file in order to execute the SQL query, I would prefer to use stored procedure that gives me the same separation of resources and logic you mentioned, and is native from both the database and the programming language. Of course, I'm assuming .NET with an SQL like database (I've been working too much wit SQL Server, but I'm pretty sure that other database, like MySQL and Postgress might have similar structure in place). BTW, sorry about the late reply. Work got in the way.
Wagner Silveira
+2  A: 

A lot of thins have been said about performance, caching, and security in this thread already, and I won't repeat those points. There are a few things that I haven't read yet in this thread, which is portability issues and rountrips.

  • If you are interested in maximum portability of your application across programming languages, then stored procedures is a good idea: the more program logic you store in the database outside the app, the less you have to recode if you're moving to another framework or language. In addition, the code to call a stored procedure is much smaller than the actual raw SQL itself, so the database interface in your application code will have a smaller footprint.
  • If you need the same logic in multiple applications, then stored procedures are a convenient to have a single definition of that logic that may be re-used by other applications. However, this benefit is oftn exaggerated as you could also isolate that logic in a library that you share across applications. Of course, if the applications are in different languages, then there is true benefit in stored procedures, as it is probably easier to call a procedure through your language's db interface than to link to a library written in another language.
  • If you are interested in RDBMS portability, then stored procedures are likely to become one of your biggest problems. Core featres of all major and minor RDBMS-es are quite similar. The largest differrences can be found in the syntax and available built-in functionality for stored procedures.

Regarding roundtrips:

  • If your have many multi-statement transactions, or in general, functions in your application that require multiple SQL statments, then performance can improve if you put those multiple statements inside a stored procedure. The reason is that calling the stored procedure (and possibly returning multiple results from it) is just a single rountrip. With raw SQL, you have (at least) one roundtrip per SQL statement.
Roland Bouman
A: 

One advantage of stored procedures is that data does not have to be transferred over the network for intermediate calculations. If a lot of calculation results in a single value, then the stored procedure is going to be faster.

SeaDrive
This is true. If you need to create temporary tables, etc. In addition, `EXEC proc_GetCustomersByCategory( 2 )` is shorter than `SELECT c.CustomerID, c.Name, a.Addres... [insert 20 more fields] from Customers as c JOIN CustAddresses as a ON a.AddressID = c.AddressID WHERE c.CustomerCategory = 2`
Atømix
What's all this conversation about network traffic? All queries used as examples here are hardly worth the effort of network optimization. I've seen very few queries that were large enough to cause network congestion.
drachenstern
Examples tend to be small, and most networks are fast, but you wouldn't want to bring back 50,000 rows over a VPN if you only wanted a sum. (I agree with your point in general that worry about minor improvements in efficiency isn't worth the effort, especially as you don't know what's going on the box anyway.)
SeaDrive
A: 

There is no "right" answer. It depends on each situation.

Did anyone mention this? Stored procedures typically return every field and it's unfeasible to create one for each variation of fields that you want. Ad-hoc lets you specify only those you want. However, if you are using any sort of entities (custom objects, EF, etc.) you'll probably be returning all fields anyway.

Nelson
Wait what? that doesn't make any sense. You can control exactly what fields you want returned from a Sproc and you can have as many sprocs as you want. If you're dynamically returning columns based on random whims, then you probably need to do your filtering in the front end code anyways. I think this answer either needs revising or downvoting...
drachenstern
@drachenstern: Maybe I haven't used them enough. What method would you recommend to have ONE stored procedure which lets you specify which fields (ANY variation) to return? Assuming that's not possible (I may be wrong), I know you can have as many sprocs as you want, but do you really want GetEmployeesIDFirstNameLastName(), GetEmployeesIDLastName(), GetEmployeesIDAddress(), GetEmployeesIDFirstNameLastNameAddress(), etc.? I'll admit that in practice you'll usually end up using no more than 2 or 3 variations, such as if you have binary or varchar(max) you don't always want to return.
Nelson
@drachenstern: With LINQ, for example, you can do `var results = from x in table select new { x.ID, x.FirstName }` and it will create an equivalent `select ID, FirstName from table` SQL statement (probably a bit more complex, but you get the point) which only returns the columns you need. With EF, normally you use the whole entities which return all columns (unless you split them up in the model), but you also have this alternative for specific cases. If you have a proper DAL you'll end up with GetEmployeesIDFirstNameLastName() anyway with that LINQ query, but you won't repeat it in an sproc.
Nelson
@Nelson, since I really don't let the users define the fields being requested, I tailor the queries yes, so I always have as many sprocs as anticipated reports. Helps keep things isolated in part, but if I were going to allow the user to choose the columns to display, I would rely on something like LINQ over sprocs. However for dynamic sorting I would just use a sproc. I also wouldn't use LINQ to generate my SQL, just to manipulate the results.
drachenstern
@drachenstern: If you don't use LINQ to generate the SQL, then you're having to transfer extra data over the network and into memory. This is probably a non-issue in most cases. In the few cases it is, you can probably create a new stored procedure to minimize the problem. In my opinion it's just another tool/option which has some pros as well as cons just as anything else. Stackoverflow uses LINQ to generate the SQL (http://meta.stackoverflow.com/questions/7202/is-stackoverflow-still-using-linq-to-sql-as-the-orm) and it works quite well. Not saying it should always or usually be used...
Nelson
In my use case, I don't have a need to give the users the option to add/remove columns, and if I do, then it's much easier to hide/show, than to refetch, if I'm storing the data in a local cache. This would hold true if we were discussing ajax or serverside tech. So, by my use case, this is my preferred method. But since there isn't such a use case here, it's moot. Additionally, I'm fundamentally opposed to using tech from major companies until the third revision (so I know they're committed to a product) which is a personal belief. Boss feels about the same. So no LINQtoSQL in our shop, yet.
drachenstern
@Nelson ~ However, this is all bound to the usage of our particular data. There is a defined requirement for information to be shown to the user. It's mostly due to the verticality of our client market. So since there's no particular need for the client to request more or less data than we're sending, we're fine to predefine exactly the columns that are needed. Were we doing something like a CRM app, we would have a different set of use-case, yes? I think we both agree on the fundamentals here, my initial comment was only "it's possible to define the columns needed, not return a dataset"...
drachenstern
@drachenstern: May I say no LINQtoSQL in your shop ever? Microsoft is already no longer committed to it and has shifted to Entity Framework. DataSets and DataTables are not [usually] recommended any more. .NET may not be recommended in 10 years, but for now they're committed at least. And I think we agree that we agree. :)
Nelson
@Nelson ~ See, had we made a full investment in LinqToSql initially, we would be looking to reverse a great many changes even now. ;-) ~ Or we would be continuing to use unsupported-going-forward technology. Tried and true and brew it yourself works some days, at the expense of more code. Sad but true. ~~ I'm still waiting for WebForms to die a painful death ;) But we're working on the next major rewrite (yes we believe it's a sane thing, given certain structure) shall most likely be MVP/MVC. Granted, we've not begun said rewrite, so time to change of course. ;) ~ Cheers mate.
drachenstern
A: 

Speaking from my experience, I would put an advantageous point for each method.

Point for Ad-Hoc SQL:

There is a case when Ad-Hoc SQL is less painful (initially), which involves large (and dynamic) range of search parameters. Say, for example, you have an Account Search, and a companion Advanced Account Search, which contains 3 times more parameters. Some of the fields are easy (Account #); others may be very painful (a substring search on Address Line 1!) And for the worst, most of the parameters are not required.

This makes performance tuning not trivial. In this case, having so many combinations of parameters, caching of execution plan (my experience is on MS SQL) will backfire. The execution plan for, say, providing Account # and Salesman # only, will potentially be very bad for another set of search, providing Address search and the brands they carry. Ad-Hoc SQL will have the side effect of recompiling based on different sets of parameters provided, thus getting around the execution plan caching problem.

Of course, the above can be done by stored procedure as well, but in order to get around the execution plan caching problem, you will have to launch the recompile command within the stored procedure. One can also argue that the dynamic SQL construction can be put within the stored procedure, but this is just moving the Ad-Hoc SQL into another place; still Ad-Hoc SQL.

Point for Stored Procedure:

One can consider Stored Procedures as API. If you have worked on enterprise environments, chances are there will be different applications doing exactly the same thing. For example, the event table may be inserted from different softwares, including Accounting, Sales, Auditing, Customer Relationship Management, etc. These programs may not be maintained by the same group of people (e.g. different subdivisions), but they will ultimately having access to the same underlying database.

In this case, it would be a source code management nightmare for using Ad-Hoc SQL, because this would result in multiple versions of Ad-Hoc SQL performing the same functionality, in which every version may have different side effects. I am currently dealing with this situation, and it is not fun. Stored Procedures in this case can be reused, thus having a central management of database codes.

TimeSpace Traveller
A: 

There probably won't be a performance benefit, but for maintainability you might want to look into using something like LINQ2SQL, so that you don't have syntax errors in your SQL.

Andrew Lewis