Click here to Skip to main content
Click here to Skip to main content

Stored Procedure Generator For SQL Server

, 18 May 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
This is a stored procedure generator for SQL Server

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:

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:

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:

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:

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.

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)

Share

About the Author

Mahesh Bailwal
Software Developer (Senior)
India India
Developer

Comments and Discussions

 
GeneralMy vote of 5 PinprofessionalAssil13-Dec-14 15:07 
QuestionGood one,just about to build my own then find this Pinmemberwebdriverca5-Dec-14 7:25 
AnswerRe: Good one,just about to build my own then find this PinmemberMahesh Bailwal6-Dec-14 21:57 
GeneralVery helpful Pinmemberranjitsahani28-Sep-14 21:39 
QuestionAdd select, select all, paging (using fetch next) and delete statements Pinmembercrownus26-Jul-14 3:04 
AnswerRe: Add select, select all, paging (using fetch next) and delete statements PinmemberMahesh Bailwal27-Jul-14 0:01 
GeneralRe: Add select, select all, paging (using fetch next) and delete statements Pinmembercrownus29-Jul-14 3:53 
GeneralRe: Add select, select all, paging (using fetch next) and delete statements PinmemberMahesh Bailwal29-Jul-14 5:23 
GeneralMy vote of 5 PinmemberBSRK20-May-14 22:52 
GeneralRe: My vote of 5 PinmemberMahesh Bailwal21-May-14 1:39 
QuestionAvoid the sp_ prefix PinmemberJuan__Perez20-May-14 21:30 
AnswerRe: Avoid the sp_ prefix PinmemberMahesh Bailwal20-May-14 21:55 
SuggestionVery usefull tool PinmemberJanvdKruyk20-May-14 7:57 
GeneralRe: Very usefull tool PinmemberMahesh Bailwal20-May-14 21:58 
GeneralGreat illustration PinmemberTonBill16-May-14 8:33 
GeneralRe: Great illustration PinmemberMahesh Bailwal21-May-14 1:40 
QuestionBravo !! PinmemberKaushal1516-May-14 2:44 
AnswerRe: Bravo !! PinmemberMahesh Bailwal16-May-14 2:51 
GeneralMy Generation PinmemberMuhammad Umair15-May-14 19:39 
GeneralRe: My Generation PinmemberMahesh Bailwal15-May-14 19:47 
GeneralGreat Work PinprofessionalJagbir Saini14-May-14 2:13 
GeneralRe: Great Work PinmemberMahesh Bailwal21-May-14 1:40 
GeneralMy vote of 3 PinprofessionalSunasara Imdadhusen14-May-14 1:54 
GeneralRe: My vote of 3 [modified] PinmemberMahesh Bailwal14-May-14 2:12 
GeneralRe: My vote of 3 PinprofessionalSunasara Imdadhusen14-May-14 2:23 
GeneralRe: My vote of 3 [modified] PinmemberMahesh Bailwal14-May-14 2:54 
GeneralRe: My vote of 3 PinprofessionalSunasara Imdadhusen15-May-14 3:43 
GeneralRe: My vote of 3 [modified] PinmemberMahesh Bailwal15-May-14 5:27 
GeneralRe: My vote of 3 PinprofessionalSunasara Imdadhusen15-May-14 19:33 
GeneralRe: My vote of 3 PinmemberMahesh Bailwal16-May-14 0:40 
GeneralMy vote of 5 PinmemberWhiteOsoBDN13-May-14 1:31 
GeneralGreat Idea and work PinmemberAwais Malik12-May-14 23:26 
GeneralRe: Great Idea and work PinmemberMahesh Bailwal12-May-14 23:51 
QuestionGreat wok +5 Pinmemberkoolprasad200312-May-14 21:10 
AnswerRe: Great wok +5 PinmemberMahesh Bailwal12-May-14 22:06 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 19 May 2014
Article Copyright 2014 by Mahesh Bailwal
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid