Cursors, Foiled Again!

While researching an article I came across a piece at http://www.simple-talk.com/sql/t-sql-programming/cursors-and-embedded-sql/. Basically the author says "embedded SQL" is bad -- meaning developers should never put SQL in their code. Nor should they use ORM tools to generate SQL for them.

Instead, they should access everything they need through stored procedures. I have mixed feelings about this. On one hand, you have to give table-access permissions to users and then deal with the resulting security risks sounds very control-freakish to me. On the other hand, I agree that embedded code can be bad because if you change the database model in any way, you have to rewrite the procedural code that relies on the existence of the previous model.

And of course, stored procedures also help make your code more modular. But this article basically advocates that database administrators really need to do a lot of heavy coding into the database.

(The title of this post is just something that came to me when I read the article, because the author's opinions were sparked by a cursor gone bad. (cursors gone wild?) )

LKa Viyuda, I'm confused.

LKa Viyuda,

I'm confused. The article wasn't written by a MySQL DBA, it was written by a Microsoft SQL Server DBA. As well, I don't believe that all DBAs that use one type of database are ignorant.

"The author’s arguments

"The author’s arguments against constraints for referential integrity and uniqueness are just plain ignorant."

Hey, it's an MySQL user ... what do you expect? :).

There are very good reasons

There are very good reasons for moving SQL code to stored procedures. The whole conceptual view is one of those... but only in extreme cases (and everyone is right, you'd still end up re-writing all the stored procedures, etc.)

1) Security - because SQL Server has the concept of ownership chaining, you can carefully control how the data is accessed via stored procedures. This means not having to give access to base tables. If the application does pass-through authentication, that means the end user has to have access to the base tables. And that means they can connect via a source other than the application. There are ways around this, such as the use of app roles, however, if you're on SQL Server 2000, this really kills connection pooling. With stored procedures, you can force John Doe to only delete one row at a time. Without, John Doe can connect with Access and delete the whole table with one bad query. You can go the "service account" road fro the app, but that opens the non-repudiation/auditing box up, so there are certainly trade-offs here.

2) Execution Plan Reuse - SQL Server is more lenient when it comes to stored procedures as far as keeping execution plans in cache. In a very busy server, ad hoc queries are going to have their plans flushed a whole lot faster than a stored procedure execution plan. In addition. SQL Server does a whole lot better with parameterization in stored procedures than in ad hoc queries. Just a small change can cause SQL Server to require a new execution plan. In addition, when stored procedures are used, SQL Server is case insensitive with respect to storing the object in the procedure cache. This is NOT the case with ad hoc queries. So if you have SELECT Column1 FROM Table1 and SELECT Column1 FROM TABLE1 being passed by the application, that's two separate execution plans in the procedure cache.

The assertions in that

The assertions in that article are too absolute to have any hope of being true.

It is true that many developers lack (and need) more education in RDBMS modeling and SQL language. But it's wrong to believe that if we just keep the OO riffraff out of our precious schema then all problems will be solved.

The argument that using stored procedures protects you from schema changes is a weak argument. It's often true that schema changes are accompanied by changes to application logic, and changes to the interface of the SP. So you end up needing to change application code anyway.

Putting db access code into your application can be a good thing:

- IDE tools are better at application coding than they are at SP coding. Sure there are SP coding tools, but they're less prevalent and I would argue less capable than application coding tools.

- In most projects, application code is more likely than SP code to be managed under source control.

- You can use add query logging and profiling to an application. Some DBMS software does this, but it's typically configured server-wide, not per-application, and the log is written in some central place regardless of what client is executing the query.

Regarding modularity, we can achieve this with proper OO design. If you design your app with better separation of responsibilities, you can reduce the spaghetti code syndrome when you alter your schema. Create a class that models a specific domain in your business logic, and the implementation of this class maps operations to the physical storage model. It's called encapsulation.

The author's arguments against constraints for referential integrity and uniqueness are just plain ignorant. They were not added to protect against mistakes of procedural developers; constraints are a normal part of a physical data model. Simply programming in SP's will not magically protect against bogus data changes, if we do away with the constraints.

I suppose Phil Factor's next article will argue against equipping autos with brakes, because if we accelerate perfectly so that we coast to a stop at the end of our journey, we don't need them. And the real problem is bad drivers who don't know how to manage their vehicle's acceleration in this manner.

I read TFA, and I disagree

I read TFA, and I disagree with most of it.

- It is written with the assumption that the one that manages the database is also the one writing all those SP's. I have never seen that - both in Oracle as well as in the MS SQL projects I have been involved with, DBA's were all about backups, security, allocation of servers for the db etc. and the developers were doing all the coding.

- When using SP's you are essentially moving your code from the application to the DB. Supposedly this would yiuld mode modularity and insulation from schema changes. Well, that's just balony. If you have SP's and the schema changes, you *still* have to rewrite the SP's. And as for more modularity, well, "the application" sounds monolithic but in reality application programming languages offer far more possibilities for modularity then the DB SP dialects. So if your SQL is sprinkled throughout the application, then that's the application programmer's fault. Nothing to do with location of the code, just organization.

- ORM's: one of the main reasons for using those is DB independency. Another one is productivity. There is no need at all to shudder at the code generated by those things. Maybe if you want to get to the utter edge of DB performance you are better of writing your own stuff, but I certainly don't see how SP programming would enter that picture.

- "Checks on foreign keys and uniqueness are excellent in development but are scarcely necessary if creating, updating and deleting are done through well-tested stored procedures. And, they will certainly slow data throughput in a hard-working production system."

So what is he proposing? That checking referential integrity with SP's would somehow be faster than letting the DB handle it? C'mon, I don't believe it for one second - regardless of the DB platform.

There is actually one thing where I agree, and that is that cursors usually aren't necessary (although some DB's can actually handle very large resultsets better through a cursor than through a direct SELECT - unfortunately - MySQL cursors don't work like that)

kind regards,

Roland

I wouldn't necessarily agree

I wouldn't necessarily agree that database administrators need to do a lot of heavy coding... It just means that you need some SQL Developers in your shop to do the database development stuff. Production DBAs, Development DBAs, SQL Developers... there are often fine lines or no lines between them.

But applications can often benefit when they have someone who is more focused on the nuances of the database and it's language. Don't get me wrong, I know there are lot of great application developers out there who are excellent with SQL and know a lot about data modeling and architecture. But chances are those folks are even better at something else (otherwise they'd be DBAs first and application developers later). Let them do what they are hired to do and hire someone else do to the heavy lifting with your SQL code.

But I think the real problem is that true SQL Developers might be pretty hard to find.

This approach (everything in

This approach (everything in stored procs) appears to be typical for many MSSQL apps. The extreme example is building the entire app in T-SQL and just calling the "main" procedure from the "application".

Take Albuterol Inhaler
Order Inhaler Albuterol
Buy Albuterol Online no Prescription
Online Buy Albuterol Without a Prescription
Purchase Online Without a Prescription Albuterol
Online Buying Aldactone
Purchase Online Without a Prescription Aldactone
Aldactone for Acne
Buying Aldactone Legally
Buy Tablets Aldactone Online
Cheapest Buy Allopurinol
Purchase Allopurinol Online no Prescription
Buy Without a Prescription Allopurinol
Buy Allopurinol Free Delivery
Legally Order Allopurinol
Buy Anafranil Without a Prescription
Buy Anafranil Pills
Buy Anafranil Medication
Buying Without a Prescription Anafranil Online
Take no Prescription Anafranil
Buying Atarax Overnight Delivery
Purchase Atarax Free Delivery
Alternative Purchase Atarax
Purchase Atarax by Phone
Legally Purchase Atarax
Order Avodart Without a Prescription
Tablets Order Avodart
Pills Buying Avodart
Buy Avodart Medication
Tablets Purchase Avodart
Alternative Purchase Bactrim
Take Bactrim Next Day Delivery
Online Buying Bactrim
Cheapest Buy Bactrim
Purchase Bactrim Without a Prescription
Buy Celecoxib Overnight Delivery
Purchase Cheapest Celecoxib
Tablets Buy Celecoxib
Buying Without Prescription Celecoxib
Order Online Without a Prescription Celecoxib
Take Cipro Legally
Purchase Cipro by Phone
Purchase Online no Prescription Cipro
Buy Pills Cipro
Order Cipro COD
Purchase Clomid Serophene Generic
100 Clomid Serophene
Cheapest Buy Clomid Serophene
Order Clomid Serophene by Phone
Take Clomid Serophene Medication
Buying Dapsone
Purchase Dapsone COD
Buy Dapsone Free Delivery
Take Dapsone Next Day Delivery
Order Cheapest Dapsone
Generic Buying Desyrel
Order Cheap Desyrel
Take Desyrel Next Day Delivery
Buy Desyrel Tablets
Buy Desyrel Cheapest
Buy Diflucan Medication
Buy Diflucan Pills
Take Diflucan Pills
Purchase no Prescription Diflucan
Order Cheapest Diflucan
Buying Dilantin Next Day Delivery
Dilantin Phenytoin
Buying Dilantin Overnight Delivery
Legally Purchase Dilantin
Purchase Dilantin Alternative
Purchase Diovan Pills
Buy Diovan Pills
Order no Prescription Diovan
Purchase Online Without Prescription Diovan
Buy Cheap Diovan
Buy Doxycycline Medication
Order Doxycycline Without a Prescription
Take Doxycycline Daily Dose
100 Doxycycline
Buying Doxycycline
Buy Elavil COD
Buy Elavil Medication
Order Elavil Generic
Order Elavil Pills
Purchase Elavil Drug
Order Generic Erythromycin
Buy Erythromycin Medication
Buy Erythromycin Without a Prescription
Purchase Online Without a Prescription Erythromycin
Buying Erythromycin Overnight Delivery
Order no Prescription Estrace
Buying Without Prescription Estrace
Take Estrace Drug
Purchase Estrace Alternative
Order Generic Estrace
Order Furosemide Pills
Buy Alternative Furosemide
Order Pills Furosemide
Take Furosemide Medication
Order Cheapest Furosemide
Buy Glucophage Pills
Tablets Order Glucophage
Take Glucophage Legally
Purchase no Prescription Glucophage
Why Take Glucophage
Purchase Imitrex Free Delivery
Buying Imitrex Without Prescription
Buying Without a Prescription Imitrex Online
Buy Cheapest Imitrex
Imitrex Generic
Buying Pills Inderal
Buy Inderal no Prescription
Purchase Inderal COD
Alternative Purchase Inderal
Purchase no Prescription Inderal
Order Lasix no Prescription
Purchase Lasix COD
Purchase Lasix Without a Prescription
Buying Lasix Without Prescription
Tablets Purchase Lasix
Buy no Prescription Levaquin
Buy Levaquin Next Day Delivery
Take no Prescription Levaquin
Purchase Levaquin Generic
Buy Alternative Levaquin
Buying Lexapro no Prescription
Generic Buying Lexapro
Purchase Lexapro COD
Purchase Lexapro no Prescription
Tablets Purchase Lexapro
Buy Lipitor Free Delivery
Order Lipitor no Prescription
Online Buy Lipitor Without a Prescription
Tablets Buying Lipitor
Generic Purchase Lipitor
Purchase Lisinopril Free Delivery
Purchase Lisinopril Alternative
Take Lisinopril Without Prescription
Online Order Lisinopril Without Prescription
Tablets Buying Lisinopril
Alternative Order Methotrexate
Purchase Methotrexate COD
Why Take Methotrexate
Purchase Methotrexate Online Without Prescription
Purchase Methotrexate Online no Prescription
Cheapest Buy Nizoral
Order Nizoral Free Delivery
Purchase Nizoral by Phone
Alternative Order Nizoral
Purchase Nizoral Drug
Take Nolvadex Without Prescription
Cheapest Order Nolvadex
Take Nolvadex Drug
Purchase Online no Prescription Nolvadex
Purchase Nolvadex Medication
Nortriptyline Antidepressant
Buy Tablets Nortriptyline Online
When to Take Nortriptyline
Purchase Nortriptyline Pills
Buying Without Prescription Nortriptyline
Tablets Buy Ortho Tri-Cyclen
Order Ortho Tri-Cyclen Generic
Take no Prescription Ortho Tri-Cyclen
Purchase Ortho Tri-Cyclen COD
Take Ortho Tri-Cyclen Without Prescription
Purchase Online Without a Prescription Premarin
Pills Buying Premarin
Tablets Buying Premarin
Order Premarin COD
Tablets Buy Premarin
Buy Tablets Proscar Online
Buy Tablets Proscar
Buy no Prescription Proscar Online
Purchase Proscar Alternative
Purchase Cheapest Proscar
Buy Risperdal by Phone
Order Risperdal Medication
Generic Order Risperdal
Purchase Risperdal Online no Prescription
Buy Risperdal Next Day Delivery
Take Synthroid Free Delivery
Take Synthroid Pills
Buy Without a Prescription Synthroid
Purchase Synthroid Alternative
Take Synthroid Legally
Generic Order Topamax
Order Online Without a Prescription Topamax
Take no Prescription Topamax
Order Cheap Topamax
Buy Topamax Without a Prescription
Valtrex Acyclovir
Pills Purchase Valtrex
Generic Buying Valtrex
Online Buy Valtrex Without a Prescription
Order Valtrex COD
Purchase Online no Prescription Zithromax
Buy Zithromax Next Day Delivery
Purchase Zithromax Pills
Buy Zithromax Free Delivery
Buying Without a Prescription Zithromax Online