Stored Procedures versus Dynamic SQL - the old debate...Frans Bouma's take... (English)

Stored Procedures versus Dynamic SQL - the old debate...Frans Bouma's take...

Sunday, 07 December 2003

//

2 minute read

Frans Bouma really doesn't see the point of using Stored Procedures over using Dynamic SQL - so Frans, what's your take on such things as Indexed Viewsand User Defined Functions - which are essentially SPs but there's no equivalent for Dynamic SQL, so you're suggesting I should not use these any more? To be honest I'm always a bit reticent to pay much credence to an argument on an approach to Data Access from someone who has a vested interest (I know LLBLGen Pro supports SPs but it does have a strong bias towards Dynamically Generated T-SQL), kinda like MS on the Linux debate...slight vested interest (or former Halliburton CEO on Iraq- but that's a different matter :-)) I do use SPs almost exclusively for Data access, I have no vested interest in one approach or the other, I used to use Dynamic SQL in the bad old days of ASP/VBScript but switched to SPs when I needed more oomph in my data access code. I have a strong preference for manipulating data using Set based logic, yes, for more complex operations I'll bring the data into code but in general, I use the DB for manipulating and then return the smallest amount of data back to code. The reason I do this isn't stubbornness, simply I find it easier to filter and composite my data closest to it's source. I also find some of Frans's arguments a bit lacking, there is an argument for using Dynamic SQL where you have to react to user input for filtering data; reports are a classic example where you might want to find matching data items based on a variable number of inputs - by all means use Dynamic SQL for that, but in 90% of cases I just want to pull data from a DB as quickly and painlessly as possible this is where I'd use SPs. I often use Table variables for compositing and filtering data, treating SPs as set providers and modifying the eventual output resultset to contain only the data I actually need - this would usually require multiple trips to and from the DB (we're talking in the dozens here for each instance) and would be pretty slow, as well as causing heavy network traffic which I can usually ill afford in highly scalable sites. In short what I'm saying is that for my purposes, the 90% Stored Procedures, 10% Dynamic SQL works best - making absolute statements about such things is like commenting on religion, politics or Open Source or Java - I don't believe there is a completely binary argument to be made in any of these areas, there's too many entrenched views - horses for courses is my opinion :-)

logo

© 2025 Scott Galloway — Unlicense — All content and source code on this site is free to use, copy, modify, and sell.