Click here to Skip to main content
15,884,177 members
Articles / Web Development / HTML
Tip/Trick

Stored Procedure Generator For SQL Server

Rate me:
Please Sign up or sign in to vote.
4.93/5 (54 votes)
18 May 2014CPOL4 min read 153K   14.2K   62   44
This is a stored procedure generator for SQL Server
Image 1

Introduction

Lately, I have been involved in a project where one of the requirements was to write stored procedures from scratch for inserting and updating data in all the available tables in SQL database. Writing large number of insert and update stored procedure from scratch manually; I find this task very arduous. So I Googled around for some time to find some free tool for generating stored procedures, but I did not find anything free which was close to my requirement. Hence I thought of giving it a try and developing a small utility for the same. Eventually, I was able to come up with a small and very basic utility that generates stored procedure for SQL server tables from scratch.

Background

The utility is developed using C# WPF (MVVM pattern) in .NET Framework 4.0. This utility generates basic insert and update stored procedures, which you can modify according to your requirement. So the only thing this utility does is save your time when you want to write insert and update stored procedure from scratch and you are not willing to do it manually.

Using the Utility

Once you run the attached utility, you get a window as shown in the below figure:

Image 2

As you can see in the above screen, there are four action buttons (Connect To Server, Generate SP, Settings and Copy Generated SP). I will explain the significance of all them one by one.

Connect To Server

This first thing you need to do is to connect to SQL server where you have the tables for which you want to generate stored procedure. You need to enter connection string in the given text box and after that, click on “Connect to Server" button. Once the application is successfully connected to the SQL server, it will show all the tables of database as shown in the below figure:

Image 3

Generate SP

For generating insert stored procedure for a single table, you need to perform the following steps:

  1. Expand the table node for which you want to generate stored procedure.
  2. Expand the node "Insert Fields" under table node.
  3. Now select the fields which you want to include in your insert stored procedure.
  4. Finally click on Generate SP button. Your insert stored procedure will be visible in the Text Box as shown in the below figure:
Image 4

Generate SP

For generating update SP for single table, you need to perform the following steps:

  1. Expand the table node for which you want to generate stored procedure.
  2. Expand the node "Update Fields" under table node.
  3. Select the fields which you want to include in your update stored procedure.
  4. If you want to do conditional update for that expand "Where Clause Fields" which is the last node in update field list and select fields which you want to include in where clause.
  5. Finally click on Generate SP button. Your update stored procedure will be visible in the Text Box as shown in the below figure:

Image 5

Settings

Using settings button, you can customize few things given below:

  • Insert SP Name Prefix: This setting allows you to customize the prefix of insert stored procedure name. The name of insert stored procedure is composed of <insert sp name prefix> + <TableName>
  • Update SP Name Prefix: This setting allows you to customize the prefix of update stored procedure name. The name of update stored procedure is composed of <Update SP Name Prefix> + <TableName>
  • Input Parameter Prefix: This setting allows you to customize the prefix of input parameter name of stored procedure. The name of input parameter is composed of <Input Parameter Prefix> + <FieldName>
  • Where Parameter Prefix: This setting allows you to customize the prefix of "Where Clause" parameter name. The name of where clause parameter is composed of <Where Parameter Prefix> + <FieldName>
  • Error Handling: This setting allows you to enable/disable error handling, if this setting is on then stored procedure will contain Try-Catch block for handling errors.

Image 6

Copy Generated SP

Clicking on "Copy Generated SP" button will copy generated stored procedures in Windows clipboard which you can paste on Notepad or anywhere else using Windows copy (Ctrl +V).

Identity/Timestamp Fields

As we know in SQL server we cannot insert/update identity and timestamp/rowversion fields. So if table contains any identity/timestamp field then that filed we will get excluded from insert/update stored procedure by default.

SQL Server Key Words

The utility wraps the table name/field name inside square brackets [] if it is SQL server key word (like order, form, etc.). The utility maintains the list of SQL server key words inside a lookup file called “SqlKeyWords.txt". In case you find any keyword that causing issue and not getting wrapped inside square brackets, then you can add the SQL key word in SqlKeyWords.txt file to make it work.

References

Working with Checkboxes in the WPF TreeView

Please feel free to ask any questions or give suggestions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Developer

Comments and Discussions

 
QuestionVirus alert Pin
nguyenthaohut4-Nov-21 17:48
nguyenthaohut4-Nov-21 17:48 
QuestionConnectionString Pin
Member 1497239822-Oct-20 2:33
Member 1497239822-Oct-20 2:33 
Suggestionsorted tables Pin
_clickit6-Sep-19 0:51
_clickit6-Sep-19 0:51 
Bugwrong decimal column length Pin
_clickit6-Sep-19 0:47
_clickit6-Sep-19 0:47 
QuestionExcellent project Pin
Tiaan Lombard (TiLO)1-Nov-17 9:36
Tiaan Lombard (TiLO)1-Nov-17 9:36 
PraiseMy Vote 5+ Pin
George Tsiga21-Mar-17 23:33
George Tsiga21-Mar-17 23:33 
PraiseNice Pin
Member 278243526-Oct-15 10:56
Member 278243526-Oct-15 10:56 
QuestionThanks Allot :) Pin
Jhony_Bravo2-Jul-15 0:50
Jhony_Bravo2-Jul-15 0:50 
Generalme likey Pin
Malisk2-Apr-15 10:51
Malisk2-Apr-15 10:51 
GeneralMy vote of 5 Pin
Assil13-Dec-14 14:07
professionalAssil13-Dec-14 14:07 
QuestionGood one,just about to build my own then find this Pin
webdriverca5-Dec-14 6:25
webdriverca5-Dec-14 6:25 
AnswerRe: Good one,just about to build my own then find this Pin
Mahesh Bailwal6-Dec-14 20:57
Mahesh Bailwal6-Dec-14 20:57 
GeneralVery helpful Pin
ranjitsahani28-Sep-14 20:39
ranjitsahani28-Sep-14 20:39 
QuestionAdd select, select all, paging (using fetch next) and delete statements Pin
crownus26-Jul-14 2:04
crownus26-Jul-14 2:04 
AnswerRe: Add select, select all, paging (using fetch next) and delete statements Pin
Mahesh Bailwal26-Jul-14 23:01
Mahesh Bailwal26-Jul-14 23:01 
GeneralRe: Add select, select all, paging (using fetch next) and delete statements Pin
crownus29-Jul-14 2:53
crownus29-Jul-14 2:53 
GeneralRe: Add select, select all, paging (using fetch next) and delete statements Pin
Mahesh Bailwal29-Jul-14 4:23
Mahesh Bailwal29-Jul-14 4:23 
GeneralMy vote of 5 Pin
BSRK20-May-14 21:52
BSRK20-May-14 21:52 
GeneralRe: My vote of 5 Pin
Mahesh Bailwal21-May-14 0:39
Mahesh Bailwal21-May-14 0:39 
QuestionAvoid the sp_ prefix Pin
Juan__Perez20-May-14 20:30
professionalJuan__Perez20-May-14 20:30 
AnswerRe: Avoid the sp_ prefix Pin
Mahesh Bailwal20-May-14 20:55
Mahesh Bailwal20-May-14 20:55 
Yes, you are right about that, I am with you on this. That's why there is an option in settings to change the prefix of stored procedure name. But yes, keeping this thing in mind the default settings should not have "sp" prefix. Thanks for pointing, I will correct that in next update.
SuggestionVery usefull tool Pin
JanvdKruyk20-May-14 6:57
JanvdKruyk20-May-14 6:57 
GeneralRe: Very usefull tool Pin
Mahesh Bailwal20-May-14 20:58
Mahesh Bailwal20-May-14 20:58 
GeneralGreat illustration Pin
TonBill16-May-14 7:33
TonBill16-May-14 7:33 
GeneralRe: Great illustration Pin
Mahesh Bailwal21-May-14 0:40
Mahesh Bailwal21-May-14 0:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.