Click here to Skip to main content
Licence 
First Posted 8 Aug 2005
Views 26,970
Bookmarked 42 times

Stored procedures generator

By | 8 Aug 2005 | Article
Generator for SQL Server stored procedures.
 
Part of The SQL Zone sponsored by
See Also

Introduction

The project presented serves for generating stored procedures for Insert, Update and Delete in a specified table.

Description

The job we most often have to do during writing queries is writing the basic stored procedures for manipulating data on the existing tables in a database. It is perhaps the step causing the most number of syntax errors. The application in this article helps avoid such errors. You just have to fill the required fields and your stored procedures are ready. Of course, the generated queries with some modifications can be used further in your code or server.

How to use

  1. Download the project to your computer.
  2. Compile/Run it.
  3. Enter the table name and number of columns.
  4. Press the button Reload.
  5. Fill the fields for primary keys, column names, data types, sizes and Allow Nulls.
  6. Before pressing the button Generate Queries, check: if there is at least one column with primary key and all columns aren't primary keys. This is important for the Update and Delete query to be generated.
  7. Press the Generate Queries button.

Example

Fig.1. The window appears after running the project.

After filling the textboxes for table name and number of columns, click on Reload button:

Fig.2. Fields for table columns are generated.

Next, after filling the column table data and clicking on Generate Queries button:

Fig.3. Queries are created.

You can now copy them and use in your application simply by clicking on the Copy to Clipboard button, below each textbox.

Commenting the code

Let's see first the code executed on clicking the Reload button.

try
{
  for(int i = 0; i<no; i++) colList[i].Dispose();
    no = Convert.ToInt32(txtColumnNo.Text);
  colList = new ucColumn[no];
  for(int i = 0; i<no; i++)
  {
    colList[i] = new ucColumn();
    colList[i].Location = new System.Drawing.Point(8, 90+i*28);
    colList[i].Size = new System.Drawing.Size(432, 21);
    this.Controls.Add(colList[i]);
  }
}
catch(Exception ex)
{
  MessageBox.Show("Please type valid integer for number of columns.", 
                  "Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
}

At the beginning, the number typed for No. of columns is converted to an integer, which is used for declaring and creating the array of controls dynamically. The value for control distance step is carefully selected - 90+i*28 -corresponding to its dimension.

The code to for query generation follows, e.g. for Insert query:

if(checkvalidity()=="")
{
  if((no > 0) && (txtTableName.Text != ""))
  {

Above code checks if the sizes for column types, number of columns and table name are given properly.

txtInsertQuery.Text = 
    "CREATE PROCEDURE dbo.sp_Insert_" + txtTableName.Text;
txtInsertQuery.Text += "\r\n(";
if(no>1)
{
  for(int i=0;i<no-1;i++)
    txtInsertQuery.Text += "\r\n @"+colList[i].txtColumn.Text + 
         " "+colList[i].cmbType.Text+
         createtype(colList[i].txtSize.Text)+ 
         allownull(colList[i].cmbNull.Text)+",";
    txtInsertQuery.Text += "\r\n @"+colList[no-1].txtColumn.Text + 
         " "+colList[no-1].cmbType.Text + 
         createtype(colList[no-1].txtSize.Text)+
         allownull(colList[no-1].cmbNull.Text);
}
else
{
  txtInsertQuery.Text += "\r\n @"+colList[0].txtColumn.Text+ 
         " "+colList[0].cmbType.Text+
         createtype(colList[0].txtSize.Text)+
         allownull(colList[0].cmbNull.Text);
}
txtInsertQuery.Text += "\r\n)";

Then comes creation of the header of the procedure and the declaration of variables.

txtInsertQuery.Text += 
  "\r\nAS\r\nINSERT INTO "+txtTableName.Text+"(";
if(no>1)
{
  for(int i=0;i<no-1;i++)
    txtInsertQuery.Text += colList[i].txtColumn.Text+", ";
  txtInsertQuery.Text += colList[no-1].txtColumn.Text+")";
}
else
{
  txtInsertQuery.Text += colList[0].txtColumn.Text+")";
}
txtInsertQuery.Text += "\r\nVALUES(";

if(no>1)
{
  for(int i=0;i<no-1;i++)
    txtInsertQuery.Text += "@"+colList[i].txtColumn.Text+", ";
  txtInsertQuery.Text += "@"+colList[no-1].txtColumn.Text+")";
}
else
{
  txtInsertQuery.Text += "@"+colList[0].txtColumn.Text+")";
}
txtInsertQuery.Text += "\r\nGO";

And the query for inserting is thus created.

The code for Update and Delete is similar with some additional requirements.

Happy querying!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Kujtim Hyseni

Web Developer

Albania Albania

Member

Kujtim Hyseni

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNice Shot..... PinmemberDomingo M. Asuncion20:27 27 Mar '07  
GeneralA complete solution Pinmemberchris Liang9:15 30 Aug '05  
GeneralRe: A complete solution - a free one! PinmemberThomas Schittli3:11 28 Sep '05  
GeneralRe: A complete solution - a free one! PinmemberStixoffire29:37 9 Jun '06  
Generalvery good Pinmembermoonxp16:29 8 Aug '05  
GeneralRecommendation PinmemberJames Taylor7:54 8 Aug '05  
GeneralIn the next version PinmemberKOMTEL_NET20:19 8 Aug '05  
GeneralRe: In the next version PinmemberStixoffire29:39 9 Jun '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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 8 Aug 2005
Article Copyright 2005 by Kujtim Hyseni
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid