Wednesday 29 August 2007

A free script to create CRUD procedures.

Recently, both at my job, as well as while working on the site, I found myself having to write countless CRUD stored procedures in order to facilitate retrieving and storing data in my tables. The majority of these reflected an exact replication of the database table model (i.e. updating all columns), and I found writing those stored procedures to be a real burden. It was boring, repetitive and error prone work (especially the UPDATE procedures, where one has to replicate columns three times (once as input variables (which includes data types), once as columnnames, and once again as variables (but this time without the columnnames).

Being the lazy guy I am, I decided I would automate the matter, and write a script to automatically create the procedures for me. Before I started, I decided I would make a few starting assumptions:

1) I wanted the SELECT procedure to be able to accept either an ID-number or a Name (assuming that like me, you called the column something involving '%Name') for an individual row, or neither, in which case the whole table should be returned.
2) I wanted to be able to have the DELETE procedures to be optional. I achieved this by using a bit-flag and IF statements to see if it ought to be created or not.
3) I wanted the user to be able to specify certain columns to exclude. In the database I'm working on for the site, there are numerous audit columns to keep track of when data is entered or modified, and by who. Naturally, this is not required in all of the procedures, so I wanted a mechanism for the end-user to include or exclude these columns.


The result of my work is the script below. I've briefly tested the generated procedures against my own database, and there, they work like a charm. Perhaps others who run more specialised or customised databases will be able to come up with unexpected errors etc. If so, please do let me know.

Of course, the script will not cover 100% of anybody's specific wishes. In fact, I'm quite sure that some of the procedures will need to be customised to suit your needs, but you will find this script will enable you to start with at least some foundation in place.

I'm releasing this script under the Creative Commons Attribution Non-Commercial No Derivatives license, which means you are free to download and share the script, provided you mention and link back to me. However, you are not allowed to change or commercially use the script. I might change this to a less restrictive license in the future, however for now, I want to be able to evaluate comments etc first. This is the first time I ever released anything under a license, so any comments regarding that are more than welcome.

A list of things I have figured I could enhance the script with:

1) Add error handling to the stored procedures.
2) Add support for schemas in SQL Server 2005.
3) Add paging support to the select procedures, by adding two additional parameters.

Also, I might later on decide to automatically generate .NET custom classes based on the exact database tables.

Click here to download the script.

One last comment, should you decide to alter the generated stored procedures to suit your specific needs, do remember to rename the new procedure. The script will automatically drop its standard procedures, in order to always reflect the latest table schemas.


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 License.

5 comments:

Anonymous said...

Peter,

I've seen you post a few requests on the wrox forum (re the beerhouse architecture) and thought that i'd mention a link to the flixon template library. this is something that i've been involved in the final stages of and may be of interest to you. basically, it's a templated application that outputs the whole class stucture (BLL and DAL) of your tables in the same format as the beerhouse, as well as creating basic CRUD aspx pages which can be used either to test the data or as a starting point for that 'data's use within your website.

I now use it exclusively and have found it to be invaluable in getting the main class coding done in an efficiant and foolproof fashion. if you want to take a look (and use the app - it's freeware), you can download it from:

http://www.flixon.com/site-generator

give it a whirl, i think it'll be a nice augmentation to your CRUD app.

jimibt

Anonymous said...

tried to run your script but get a thousand errors, beginning:

Server: Msg 170, Level 15, State 1, Line 60
Line 60: Incorrect syntax near 'max'.

any ideas on what might be causing this? many thanks...

Peter Schmitz said...

Hiya,

The max indicator is new to SQL Server 2005. I'm guessing you're running the script on SQL Server 2000.

The limitation there is the max length for a varchar value is 4000 (top of my head, don't pin me down on this). If you want to store more than that, you'll have to start using string contatonation, which can get quite dirty.

Therefore I ended up picking the max nominator. You can always substitute with varchar(4000), but you might have to use more than one of those.

Let me know if that helped? :)

Cheers,

Peter

Anonymous said...

Hello, i run this script and i´ve got this list of errors:

Msg 134, Level 15, State 1, Procedure usp_INSERT_MSreplication_options, Line 6
The variable name '@optname' has already been declared. Variable names must be unique within a query batch or stored procedure.
Msg 137, Level 15, State 2, Procedure usp_INSERT_MSreplication_options, Line 33
Must declare the scalar variable "@value".
Msg 134, Level 15, State 1, Procedure usp_UPDATE_MSreplication_options, Line 6
The variable name '@optname' has already been declared. Variable names must be unique within a query batch or stored procedure.
Msg 137, Level 15, State 2, Procedure usp_UPDATE_MSreplication_options, Line 24
Must declare the scalar variable "@value".
Msg 134, Level 15, State 1, Procedure usp_INSERT_spt_fallback_db, Line 6
The variable name '@xserver_name' has already been declared. Variable names must be unique within a query batch or stored procedure.
Msg 137, Level 15, State 2, Procedure usp_INSERT_spt_fallback_db, Line 37
Must declare the scalar variable "@xdttm_ins".
Msg 134, Level 15, State 1, Procedure usp_UPDATE_spt_fallback_db, Line 6
The variable name '@xserver_name' has already been declared. Variable names must be unique within a query batch or stored procedure.
Msg 137, Level 15, State 2, Procedure usp_UPDATE_spt_fallback_db, Line 26
Must declare the scalar variable "@xdttm_ins".
Msg 102, Level 15, State 1, Procedure usp_SELECT_spt_fallback_dev, Line 35
Incorrect syntax near ')'.
Msg 102, Level 15, State 1, Procedure usp_DELETE_spt_fallback_dev, Line 25
Incorrect syntax near ')'.
Msg 134, Level 15, State 1, Procedure usp_INSERT_spt_fallback_dev, Line 6
The variable name '@xserver_name' has already been declared. Variable names must be unique within a query batch or stored procedure.
Msg 137, Level 15, State 2, Procedure usp_INSERT_spt_fallback_dev, Line 41
Must declare the scalar variable "@xdttm_ins".
Msg 134, Level 15, State 1, Procedure usp_UPDATE_spt_fallback_dev, Line 6
The variable name '@xserver_name' has already been declared. Variable names must be unique within a query batch or stored procedure.
Msg 137, Level 15, State 2, Procedure usp_UPDATE_spt_fallback_dev, Line 28
Must declare the scalar variable "@xdttm_ins".
Msg 134, Level 15, State 1, Procedure usp_INSERT_spt_fallback_usg, Line 6
The variable name '@xserver_name' has already been declared. Variable names must be unique within a query batch or stored procedure.
Msg 137, Level 15, State 2, Procedure usp_INSERT_spt_fallback_usg, Line 39
Must declare the scalar variable "@xdttm_ins".
Msg 134, Level 15, State 1, Procedure usp_UPDATE_spt_fallback_usg, Line 6
The variable name '@xserver_name' has already been declared. Variable names must be unique within a query batch or stored procedure.
Msg 137, Level 15, State 2, Procedure usp_UPDATE_spt_fallback_usg, Line 27
Must declare the scalar variable "@xdttm_ins".
Msg 134, Level 15, State 1, Procedure usp_INSERT_spt_monitor, Line 6
The variable name '@lastrun' has already been declared. Variable names must be unique within a query batch or stored procedure.
Msg 137, Level 15, State 2, Procedure usp_INSERT_spt_monitor, Line 43
Must declare the scalar variable "@cpu_busy".
Msg 134, Level 15, State 1, Procedure usp_UPDATE_spt_monitor, Line 6
The variable name '@lastrun' has already been declared. Variable names must be unique within a query batch or stored procedure.
Msg 137, Level 15, State 2, Procedure usp_UPDATE_spt_monitor, Line 29
Must declare the scalar variable "@cpu_busy".
Msg 134, Level 15, State 1, Procedure usp_INSERT_spt_values, Line 6
The variable name '@name' has already been declared. Variable names must be unique within a query batch or stored procedure.
Msg 137, Level 15, State 2, Procedure usp_INSERT_spt_values, Line 33
Must declare the scalar variable "@number".
Msg 134, Level 15, State 1, Procedure usp_UPDATE_spt_values, Line 6
The variable name '@name' has already been declared. Variable names must be unique within a query batch or stored procedure.
Msg 137, Level 15, State 2, Procedure usp_UPDATE_spt_values, Line 24
Must declare the scalar variable "@number".

Im running sql server 2005... can you help me??

Thks

Anonymous said...

the problem in the script is in line 120 to 123

it should be this

+ CAST(sc.xprec AS [varchar])
+ ','
+ CAST(sc.xscale AS [varchar])
+ ')'