Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Database and Reporting > are stored procedures good or bad?


Reply
 
Thread Tools Display Modes
  #1  
Old 09-08-2004, 07:20 AM
msmeth's Avatar
msmeth msmeth is offline
Senior Contributor
 
Join Date: Sep 2003
Posts: 814
Question are stored procedures good or bad?


Hi everyone. I have used previously created stored procedures, just calling them from my VB6 applications, but I've never really written one. Well, I wrote one, but it wasn't that intricate. Now I have to write some more and I've been looking on the web for tips and all that and I came across some sites that said stored procedures are bad. Can you guys shed some light on this for me? I was under the impression that they're more efficient than dynamic SQL queries, but now I'm confused....
Reply With Quote
  #2  
Old 09-08-2004, 07:26 AM
Harkon's Avatar
Harkon Harkon is offline
Junior Contributor
 
Join Date: Feb 2004
Location: Greece
Posts: 290
Default

to my knowledge they are 'good things' too.
can you place some reasons or link as for why they are bad?
__________________
Give me liberty or give me death,
I'll fight till my last breath
Reply With Quote
  #3  
Old 09-08-2004, 08:12 AM
msmeth's Avatar
msmeth msmeth is offline
Senior Contributor
 
Join Date: Sep 2003
Posts: 814
Default

Quote:
Originally Posted by Harkon
to my knowledge they are 'good things' too.
can you place some reasons or link as for why they are bad?
This is one: http://weblogs.asp.net/fbouma/archiv.../18/38178.aspx
Reply With Quote
  #4  
Old 09-08-2004, 08:50 AM
Merrion's Avatar
Merrion Merrion is offline
Ultimate Contributor

* Guru *
 
Join Date: Sep 2001
Location: Dublin, Ireland
Posts: 1,828
Default

If you _only_ have one application connecting to the database and have _total_ control over changes to it then it is OK to use on the fly sql generation - but in what we in the real world find is that stored procs are good.
Reply With Quote
  #5  
Old 09-08-2004, 11:20 AM
HardCode's Avatar
HardCode HardCode is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Feb 2004
Location: New Jersey
Posts: 3,338
Default

I stopped reading his article after he stated, "...the stress of maintaining a lot of stored procedures, to write another stored procedure for each thing you want to do, is gone. Dynamic SQL is the future. (Dynamic SQL is generated on the fly by a generic piece of code which gets various data as input and generates a parametrized query from it."

Anyone savvy enough with Transact-SQL can do the same thing with a stored procedure. I hate nothing more than to concatenate a 9349839 character long string to form the SQL to run, with all of the fun ' " & sMyName & "');" etc, etc.

Maybe he should read about SQL Injection attacks before he bashes stored procedures?

*In my best Richard Dawson voice* Survey saysssssssssss *BING* SPs = GOOD
__________________
DON'T CLICK HERE

Useful forum tags: [VB][/VB], [CODE][/CODE], [HTML][/HTML]
Reply With Quote
  #6  
Old 09-08-2004, 12:10 PM
loquin's Avatar
loquin loquin is offline
Google Hound

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,386
Default

Allowing access to updating the data through a stored procedure allows you to encapsulate the database, and to make it more "object oriented." In effect, the SPs are your exposed methods.

In addition, you can control access to the tables by the SP's themselves, as they may be granted table access, where the user would not.

SP's also provide a convenient means to encapsulate various business rules that would be too abstract to enforce via relational integrity.

Finally, SP's DO reduce network traffic. In a busy network environment, with potentially hundreds or even thousands of clients, you want to avoid the possibility of upgrading network speeds to support a database application when it scales up. Your CIO would frown on you if you created an app that proved to be a necessity, but you had to install gigabit network hardware just to support the full number of clients needed because of ineffecient traffic requirements.
__________________
Lou
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown
Reply With Quote
  #7  
Old 09-08-2004, 12:34 PM
msmeth's Avatar
msmeth msmeth is offline
Senior Contributor
 
Join Date: Sep 2003
Posts: 814
Default

Okay I'm glad to see all these responses. Now I can continue in the way I wanted to do this application

Can I ask you guys too, if it's best to use SPs for this or should I use dynamic queries. The application is basically a giant data entry thing, with the users entering data into a 24 column grid. They don't always enter something into every column. Should I use a stored procedure for the inserts?
Reply With Quote
  #8  
Old 09-08-2004, 01:04 PM
loquin's Avatar
loquin loquin is offline
Google Hound

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,386
Default

That depends.

How many clients are you looking at in the fully deployed system? If more than a 10-20, SP's would be nice.

How many joined tables are targeted? If more than one table is joined, then SPs are nice; you pass the data set; it takes care of updating all the related tables within a transaction & returns success or failure by raising an error.

Should the table be secure from clients directly accessing it? If so, SP's are a necessity.

Are there many business rules, and/or are these rules subject to change? If yes, encapsulating the rules in stored procedures is a convenient way to implement the changes without re-distributing client apps.
__________________
Lou
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown
Reply With Quote
  #9  
Old 09-08-2004, 02:07 PM
msmeth's Avatar
msmeth msmeth is offline
Senior Contributor
 
Join Date: Sep 2003
Posts: 814
Default

Quote:
Originally Posted by loquin
That depends.

How many clients are you looking at in the fully deployed system? If more than a 10-20, SP's would be nice.

How many joined tables are targeted? If more than one table is joined, then SPs are nice; you pass the data set; it takes care of updating all the related tables within a transaction & returns success or failure by raising an error.

Should the table be secure from clients directly accessing it? If so, SP's are a necessity.

Are there many business rules, and/or are these rules subject to change? If yes, encapsulating the rules in stored procedures is a convenient way to implement the changes without re-distributing client apps.
1. There shouldn't be more than 10 clients.
2. Only two joined tables are targeted.
3. I'd like it to be secure from direct access.
4. What do you mean by business rules?

What about stuff like data verification (like temperature should be a valid number)? Should I take care of that kinda thing in the SP or in my VB code?
Reply With Quote
  #10  
Old 09-08-2004, 03:03 PM
loquin's Avatar
loquin loquin is offline
Google Hound

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,386
Default

Quote:
I'd like it to be secure from direct access.
Then all the other points are moot. You should use stored procedures to update the tables, and views to retrieve the data.

Quote:
what do you mean by business rules?
Well, maybe one would be If Employee Type = 'Exempt' then Salary is fixed, else overtime pay rules apply. You could either put the rules in your application, which would mean that if the rule ever changed, your application would have to be updated and re-distributed, or, you could code the rule into the stored procedure(s) for adding or altering an employee record. Then, if/when the rule ever changed, you would simply change it in the SP, and all the client apps would "keep on ticking."
__________________
Lou
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown
Reply With Quote
  #11  
Old 09-09-2004, 02:18 PM
msmeth's Avatar
msmeth msmeth is offline
Senior Contributor
 
Join Date: Sep 2003
Posts: 814
Default

Quote:
Originally Posted by loquin
Then all the other points are moot. You should use stored procedures to update the tables, and views to retrieve the data.

Well, maybe one would be If Employee Type = 'Exempt' then Salary is fixed, else overtime pay rules apply. You could either put the rules in your application, which would mean that if the rule ever changed, your application would have to be updated and re-distributed, or, you could code the rule into the stored procedure(s) for adding or altering an employee record. Then, if/when the rule ever changed, you would simply change it in the SP, and all the client apps would "keep on ticking."
Okay cool. I don't think there are any business rules, but I haven't really looked at the data yet. I highly doubt it though. Thanks a lot for the insight. I don't know how to use views so expect a post or two from me about those
Reply With Quote
  #12  
Old 09-13-2004, 11:41 AM
msmeth's Avatar
msmeth msmeth is offline
Senior Contributor
 
Join Date: Sep 2003
Posts: 814
Default

Alright I'm working with views now. Basically, can you tell me what the benefits of them are? The tables I'm using don't join or anything.

And, how do I execute a view from VB6?
Reply With Quote
  #13  
Old 09-13-2004, 05:29 PM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

You started another thread regarding the benefits of view, please refer to that thread .

Regarding selecting records, you should be able to do it, just as you would a table. You can run a SELECT or UPDATE against the view as it was a table. A view is another data object within your sql server. The same would apply with stored procedures..you can reference the view as you would a table object.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->