Quote:I have a number of situations in which users can have access to only certain rows of a database. The rules covering which rows a user can access are often complex and best managed in procedural code, or at least not easily handled in straight SQL Set-based code. In these situations, placing this logic in an SP (and denying any access to the bare tables) is very helpful.
Actually there is a much easier way to implement row-based security using joins to a security table that has at least values for the SecurityPrinciple and the KeyValue from the table. A simple join to the table, encapsulated in a view and you can use normal access outside of here (generally this is better done on a domain value so it is not terribly tedious, but either way it is done naturally using SQL) It is what one might do on the front end using programmed logic, but far easier to administrate (since security changes!)
Quote:One wild card here is that in some organizations developers are not allowed to manipulate SPs or other objects on the SQL Server directly. In this case, for non-technical reasons, SPs are not a good option.
This is not a wild card, it is a weird case of poor management. If a programmer can generate code "on the fly" using client code, it is ignorant to prevent them doing the same thing on the server in code. Obviously there are some major differences in coding procedures versus functional coding but (and here is the great thing) if the proc takes the right parms, and it returns the right data, it can be optimized. Obviously don't wait until your newbie programmer has written 500 procs using cursors, but it doesn't have to be an us vs them mentality.
Quote:Trust me, even though this hapless programmer is using stored procedures, the application is susceptible to SQL injection attacks.
This is very true. Either method is no better or worse for injection attacks. It just takes some intelligence to deal with it. Check out
http://www.sommarskog.se/dynamic_sql.html for more information
Quote:The important thing to note about this is that there is no “compiling” of SPs in the traditional sense. In some environments (Oracle, I am told, and in a number of other databases as well), SPs really are converted to a different programming language (such as C or C++) and compiled to native code. This is not the case in SQL Server, for SPs or ad-hoc SQL. SPs have a slight advantage in that the execution plan can be cached and found a bit more efficiently.
The compiling issue is a non-starter. The cost of compilation is so minimal as to not be a real concern unless you are executing thousands of times a second. However, the cost of building a query plan is the real cost. The problem with ad hoc access is that unless you are looking at a single table in your query, the plan will not be parameterized. For example run:
set showplan_text on
go
select customers.customerid, orders.freight
from customers
join orders
on customers.customerid = orders.customerid
where customers.customerid = 'vinet'
|--Nested Loops(Inner Join)
|--Clustered Index Seek(OBJECT:([Northwind].[dbo].[Customers].[PK_Customers]), SEEK:([Customers].[CustomerID]='vinet') ORDERED FORWARD)
|--Bookmark Lookup(BOOKMARK:([Bmk1002]), OBJECT:([Northwind].[dbo].[Orders]))
|--Index Seek(OBJECT:([Northwind].[dbo].[Orders].[CustomerID]), SEEK:([Orders].[CustomerID]='vinet') ORDERED FORWARD)
In the plan you will see the literal value 'vinet' in there. This means that this plan is only good if some other user happens to run the exact same query (including spacing and capitalization) with the same literal. Far better to use a procedure because you control the parameter.
Quote:As far as maintenance goes, regardless of whether you use ad-hoc SQL or SPs, an unskilled person will create a maintenance nightmare; a skilled practitioner will do a great job.
Fundamentally this is true. When you need to make a change to a query and it requires a complete recompile of the client code and hence a heavy bit of testing to redeploy to 100 customers (or to 100 users in your organization) it becomes more of a nightmare than it needs to be.
Quote:So, should you use SPs or ad-hoc SQL? The answer is “it depends.” I have placed myself firmly on the side of doing all database access through SPs. I do so knowing that I am not getting any unique security benefits using SPs, knowing that the performance benefits are not as clear cut as I once might have thought (but are still real in some cases), knowing how to leverage SPs to minimize the maintenance load, and understanding that I am more tied to SQL Server than I might be if I were to use ad-hoc SQL. What do you think?
I think you are pretty much right on, though you do get security benefits if you follow through on the entire plan for building the application right, there is a benefit. If you use a proxy account to do security, you are right, the security benefits are far less. Performance is greatly enhanced, but not to a level that you will notice for most applications with smaller numbers of users. As concurrency needs rise however, stored procedures are of great benefit (though SQL Server does a dandy job of assisting the architect who doesnt care.
You are more tied to SQL Server in some ways, but less in others. I am not an expert in any other dialects, but the king of the use standard SQL or you are stupid clearly favors using stored procedures (newsgroup thread: (http://groups.google.com/group/microsoft.public.sqlserver.programming/browse_frm/thread/41b94165a9d7f1ca/840120292a4a1168?lnk=st&q=joe+celko+stored+procedures&rnum=3#840120292a4a1168))
For my original rebuttal to the Bouma article: (http://spaces.msn.com/members/drsql/Blog/cns!1pqLLlQgw9hZKLV1K4eLatKw!273.entry)