Stored Procs, Packages, Views...Pah!
-
...or just use Parameters instead - it makes the code easier to read as a bonus...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Could you supply example code of how to use parameters with an IN clause, where the number of items in the in clause is not known until runtime? I've never seen a solution to that one...
-
Could you supply example code of how to use parameters with an IN clause, where the number of items in the in clause is not known until runtime? I've never seen a solution to that one...
Not without thinking about it for a while! But if you are doing things like that, you should know what you are doing anyway rather than just suck-it-and-see which most seem to try. :laugh:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
1. Developers DB skills beyond the absolute basics are poor. Attitude seems to be...DB? Pff How hard can that be? It's just SQL right? That's why a development team should hire GOOD developers. Anyone that say this is IMHO NOT a 'good developer'. 2. Most who can write PL/SQL or T/SQL have no concept of what quality code is. Indeed I don't think they see their PL/SQL as code, and they don't feel obliged to observe ANY rules of craftsmanship. That's why you should hire GOOD DBAs. And by Good I don't mean 'can write a query with joins and that does not use Select * all over the place. I mean a real DBA. I would agree that many don not feel they are writing code, but add to that the fact that ANY company that has ANY change management or review process SHOULD be running these resources through a code review process. 3. Development and Debugging tools are poor at best. I don't know if I agree here. I have seen some pretty good tools. I am getting partial to Toad lately, and the SQL Enterprise manager does some decent performance monitoring metrics. The large gap I see when things like web services must run multiple queries against disparate Dbs from different vendors and do aggregation before returning result sets, but that is just something inherent in using multiple platforms like I run up against all th times (IE: Mixing data from DB2, SQL and Oracle SPs). 4. Most clients seem confused about how to integrate DB development into their overall development process. And seem reluctant to heed advice on how to do so. Again, people problem that can be solved (IE: The idiots need to be fired) and not really a technology issue. 5. There can and often is a turf war between developers and DBA's that make a difficult situation worse. I hear that :) but again, people problem. I seem to remember hearing a discussion VERY similar to this way way back in the days when OOP was 'invented' and people started to discuss the n-tier model and how UI side developers were never going to 'get' how their design impacts the overall use of the app so code jockeys should just be allowed to write it all. :) Man, what comes around goes around :) But I DO understand it... been knee deep in it... lived through it...
Ray Cassick wrote:
That's why a development team should hire GOOD developers.
Yes. Of course. As I've said, no matter what the tool ultimately it comes down to the people. But I'm coming from the perspective of 14 years of being brought into existing projects, or starting new projects in existing teams. I don't have the option (generally) of getting the team and the infrastructure the way I like. I am seriously considering throwing my hat at it and I have spoken to a few companies about joining them full time, but I'll only do it if I know I'm joining somewhere that is doing software properly (or at least seriously trying to hit that ideal). I've reached the point where I'll take the pay cut if I have to in order to work with good guys and gals. In the meantime I'm faced with debugging our profession as best I can. Boy, as a profesion we sure do suck. Right now I'm faced with the problems that aren't even the fault of the developers surrounding me. There is nobody to fire because the code that's screwing me up now comes from a Vendor, and no, dropping the vendor isn't an option either. And Yes, even if my notion of keeping developers away from the DB for as long as possible were to take hold in this company it wouldn't fix the problem of Vendors shipping bad DB Code. But it would be a start. We need to make a start. The Packages and Procs and Views that surround a Database are an API. They need to be given the same respect as any API. They are perceived as something less than an API. Something more pliable than an API. I so often hear the excuse that you should put logic into stored procs because Stored Procs are easier to change than code. THEY ARE NOT. They are exactly as difficult to change as code. You have the same difficulties of keeping backward compatibility of interfaces. I would contend that the "belief" that the DB is easier to change than code is a big part of the problem. I'm not saying you should't use Procs packages etc. I'm just saying that it shouldn't be the no-brainer people seem to want it to be. Particularly when you or your team are no-brainers. -Rd
Hit any user to continue.
-
Not without thinking about it for a while! But if you are doing things like that, you should know what you are doing anyway rather than just suck-it-and-see which most seem to try. :laugh:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
You might be thinking for a while. As far as I'm aware, it's not possible.
-
Warning: The Following is a Rant. From time to time I get into the religious 'To Stored Proc or not To Stored Proc' argument. Let me confess right now. As much as I appreciate all the pro's of Stored Procs, I still prefer to not use them when it can be avoided. It may be a technically inferior solution but I still prefer to create functions and subs in my VB or C# code that perform the equivalent task. I have finally decided that the reason for my bias is that the greatest Code Horrors I deal with now are badly written Packages and Stored Procs etc. It's a bloody nightmare. And to top it all, I regularly have to deal with the fact that the features needed to debug aren't installed, or they're running some cut down version of a DBMS. So I'm debugging this rats maze of code using techniques that I haven't used since I wrote Basic on my Sinclair Spectrum. Plug in Values. Run. Check Log File. Nothing Happened. Plug in Values. Run. Check Log File. Something unexpected happened. repeat to fade. Right now I'm trawling through the PL/SQL code of a very large globally known company. Honestly I've decided that what I'm looking at is a cast off from that team of monkeys that are working on the complete works of Shakespeare. I also regularly have to deal with Databases that contain hundreds of functions, procs and views where nobody knows if they are needed anymore but everybody is terrified to modify or remove any of them. I see companies who spawn a new copy every time a proc needs to be changed (just in case) and use that. I see companies who have no Gold version of their DB. When they need a DB either for test or for a new Production site, they just copy an existing production site. As for version control. It seems DB Objects live in some abstract zone, like International waters that aren't covered by the treaties that cover version control. This is no way to live. Thanks for letting me get that off my chest. Now back to the rats maze. -Richard
Hit any user to continue.
Sql debug is always a problem no matter you use stored proc or straight sql statement in code. I see several advantages of using stored proc. It has faster execution. It is safe (against sql injection). Data/business logic can be modified in a stored proc alone without re-compliling the main application as long as the returned data columns are the same.
TOMZ_KV
-
Richard A. Dalton wrote:
I have finally decided that the reason for my bias is that the greatest Code Horrors I deal with now are badly written Packages and Stored Procs etc.
But THIS is the problem, not really the 'concepts' of stored procedures or packages. The same can be said about regular code and writing shared libraries and using objects. Poorly written anything is junk. You can’t blame the tool or the concept really. Blame the user of the concept/tool = YES Blame the reviewer of the designer = YES Blame the reviewer of the implementation = YES You can use the best tools on the best platform using the best agreed upon methodologies and still write code that is ugly.
It's not the tool that's used, it's the tool that uses it!
==================================== Transvestites - Roberts in Disguise! ====================================
-
Could you supply example code of how to use parameters with an IN clause, where the number of items in the in clause is not known until runtime? I've never seen a solution to that one...
-
You can't use parameters with an IN clause. You would have to use something similar to "WHERE ((col1 = @parm1) OR (col1 = @parm2)) ..." which is easy enough to build at runtime.
True, but it rules out stored procedures...
-
Sql debug is always a problem no matter you use stored proc or straight sql statement in code. I see several advantages of using stored proc. It has faster execution. It is safe (against sql injection). Data/business logic can be modified in a stored proc alone without re-compliling the main application as long as the returned data columns are the same.
TOMZ_KV
Tomz_KV wrote:
It has faster execution.
Not so much. Certainly not as much of a performance improvement as some would have you believe. There are situations where perhaps doing work on the DB server saves trips over the wire. In those cases yes. But for side execution of the same query, I wouldn't let performance influence me.
Tomz_KV wrote:
It is safe (against sql injection).
The old SQL Injection argument is an iteresting one. As I said above I believe at least 80% (probably more) of apps can get by quite nicely without the benefits that Stored Procs etc bring. If you are building an app where SQL injection might be an issue you are absolutely in the 20% (or less) category. My point on this thread is about what should be the Default Model. I absolutely have no problem with people who need the features of a DBMS and know how to use them doing so.
Tomz_KV wrote:
Data/business logic can be modified in a stored proc alone without re-compliling the main application as long as the returned data columns are the same
Well that's just an abstraction layer. You can build abstraction layers any way you want. A DLL, a web service, whatever. Generally speaking even if you use stored procs you shouldn't have business logic in there anyway. So really what we're talking about is abstracting away the Database structure, which you should be doing in any case. -Richard
Hit any user to continue.
-
Sql debug is always a problem no matter you use stored proc or straight sql statement in code. I see several advantages of using stored proc. It has faster execution. It is safe (against sql injection). Data/business logic can be modified in a stored proc alone without re-compliling the main application as long as the returned data columns are the same.
TOMZ_KV
Tomz_KV wrote:
It has faster execution.
Not necessarily. The execution plan for the stored procedure is not determined dynamically, so may not be appropriate for the query as executed against the current data.
Tomz_KV wrote:
Data/business logic can be modified in a stored proc alone without re-compliling the main application as long as the returned data columns are the same.
Why is that an advantage? Why is changing a stored procedure to implement a logic change "better" than changing compiled-to-exe code to implement a logic change?
-
Could you supply example code of how to use parameters with an IN clause, where the number of items in the in clause is not known until runtime? I've never seen a solution to that one...
You could try using a table variable to essentially perform the operation. Add the values to the table variable and join on the table. :-D
-
You could try using a table variable to essentially perform the operation. Add the values to the table variable and join on the table. :-D
Or you could just use some standard SQL, with properly escaped parameters, which was my original point. Much simpler to develop and debug.
-
True, but it rules out stored procedures...
You could use table value parameters to pass multiple values in one parameter, I don't use stored procedures for most projects though. Unless I am going to be performing the same query from 2 different applications or the query is extremely complex I always use paramaterized queries.
-
Or you could just use some standard SQL, with properly escaped parameters, which was my original point. Much simpler to develop and debug.
-
Why not?
-
Tomz_KV wrote:
It has faster execution.
Not necessarily. The execution plan for the stored procedure is not determined dynamically, so may not be appropriate for the query as executed against the current data.
Tomz_KV wrote:
Data/business logic can be modified in a stored proc alone without re-compliling the main application as long as the returned data columns are the same.
Why is that an advantage? Why is changing a stored procedure to implement a logic change "better" than changing compiled-to-exe code to implement a logic change?
Electron Shepherd wrote:
Why is changing a stored procedure to implement a logic change "better" than changing compiled-to-exe code to implement a logic change?
For a in-house program, if the developer is not avaialble for making changes and re-compiling, a database guy could easily achieve the same goal by modifying the storedproc. This may not apply to a commercial program but happens frequenly for a home-grown program.
TOMZ_KV
-
Electron Shepherd wrote:
Why is changing a stored procedure to implement a logic change "better" than changing compiled-to-exe code to implement a logic change?
For a in-house program, if the developer is not avaialble for making changes and re-compiling, a database guy could easily achieve the same goal by modifying the storedproc. This may not apply to a commercial program but happens frequenly for a home-grown program.
TOMZ_KV
Tomz_KV wrote:
For a in-house program, if the developer is not avaialble for making changes and re-compiling, a database guy could easily achieve the same goal by modifying the storedproc
But, if the logic is in the stored procedure, the reverse is also true: For a in-house program, if the DBA is not avaialble for making changes, a developer could easily achieve the same goal by making changes and re-compiling Why is one better than the other? The main disadvantage I can see with your approach is that an executable that has not changed starts behaving differently. That can lead to some unnecessary bug reports, when there is no "obvious reason" for the change in behaviour
-
Different DBMS's have different escape characters and it's just not a nice way of doing it. Paramaterized queries were created for a reason.
ScottM1 wrote:
Different DBMS's have different escape characters
True. They also have different stored procedure syntax. Are you suggesting that's a good reason not to use stored procedures?
-
Electron Shepherd wrote:
Why is changing a stored procedure to implement a logic change "better" than changing compiled-to-exe code to implement a logic change?
For a in-house program, if the developer is not avaialble for making changes and re-compiling, a database guy could easily achieve the same goal by modifying the storedproc. This may not apply to a commercial program but happens frequenly for a home-grown program.
TOMZ_KV
Tomz_KV wrote:
For a in-house program, if the developer is not avaialble for making changes and re-compiling, a database guy could easily achieve the same goal by modifying the storedproc.
This is NOT a valid reason for choosing one technology over another. Changing logic at the database level is a BIG deal. This isn't something you should be roping someone into simply because you can't find a handy developer. And hey! it's in the DB so a DBA should be able to handle it. Does the DBA actually understand the intent of the original code? Or the full implications of changing it? Or is he JUST FOLLOWING ORDERS? This is exactly what I'm talking about. We treat a DB implemented API like it's some slow cousin from the country that can be pushed around and manipulated by anyone who can boot up TOAD. It's Code dammit. It deserves the same amount of planning, control, respect and fear that VB or C# or C++ code commands. The technical aspects of making a change to some VB isn't really all that different to changing a Stored Proc. Sure, deployment "might" require a little more effort, depending on your set up. Technically making the change and deploying it isn't the bit that causes problems. It's the unforseen side effects of the change that will have you at your desk cursing at 8pm on a Friday night when your family is at home waiting for you. And if it's your app that's breaking, the DBA that was just following orders isn't going to be by your side on Friday Night. -Rd
Hit any user to continue.