Connecting Tech Pros Worldwide Help | Site Map

Creating SQL Stored Procedures

  #1  
Old April 20th, 2007, 07:52 AM
Newbie
 
Join Date: Apr 2007
Posts: 14
Hi all,

Here is some information which could help people who want to create stored procedures and execute them in their program.

You can create stored procedures in two ways:
  • Through front end
  • or writing procedure from backend SQLServer.

Back End
First we'll cover how to write a stored procedure in back end.
The following example is how to create a store procedure for MSSql Server:
  • Open enterprise manager,
  • Expand the SQL Server Group,
  • Expand the Server,
  • Select "Databases" folder,
  • Expand the database the store procedure will be written for,
  • Right click "Stored Procedures",
  • Select "New Stored Procedure..."

This will open a dialogue box that will let you create a new Stored Procedure.
In this dialogue box you will enter the commands that will be executed when this store procedure is used.

The following is an example of a store procedure that will add an entry into the "Student" table:
Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE AddStudent @Sno VARCHAR(10),@Sname VARCHAR(20),@Saddress VARCHAR(50) 
  2.  AS
  3. Begin
  4. INSERT INTO  STUDENT(Sno ,Sname ,Saddress ) values (@Sname ,@Sname,@Saddress)
  5. End
In this example, "AddStudent " is the procedure name and the parameters needed to execute procedure are: "@Sno", "@Sname", "@Saddress". "Sno","Sname","Saddress" are the column names for student table.

Using The Store Procedure In Code
After you create your store procedure in your database, you can use it in your code.

The following is an example of how to store student information into the database (using the store procedure created in the steps above) when the "Savebutton" is clicked.

Expand|Select|Wrap|Line Numbers
  1. Private Sub SaveButton_Click() Handles SaveButton.Click
  2.      Dim conn As SqlConnection = New SqlConnection("data Source=yourserver/systemno;uid="userID";pwd="password";initial catalog=dbname")
  3.      Try
  4.         conn.Open()
  5.         Dim strcmd As SqlCommand = New SqlCommand("stu", conn)
  6.         strcmd.CommandType = CommandType.StoredProcedure
  7.         strcmd.Parameters.Add("@sno", TextBox1.Text)
  8.         strcmd.Parameters.Add("@sname", TextBox2.Text)
  9.         strcmd.Parameters.Add("@saddress", TextBox3.Text)
  10.         strcmd.ExecuteNonQuery()
  11.         conn.Close()
  12.      Catch ex As Exception
  13.  
  14.      End Try
  15.     End Sub
  16. End Class
  17.  
This code will take the values from asp page/form and then stored the values in backend SQLDB.

Hope this will help you.
Jyo!

Last edited by Frinavale; March 9th, 2009 at 04:04 PM. Reason: Major edit on Jyothi's original post



  #2  
Old April 20th, 2007, 12:37 PM
Newbie
 
Join Date: Mar 2007
Location: delhi india
Posts: 18

re: Creating SQL Stored Procedures


hi ,
ya this is good method to writ stored procedure
  #3  
Old April 20th, 2007, 01:06 PM
Newbie
 
Join Date: Apr 2007
Posts: 14

re: Creating SQL Stored Procedures


Quote:
Originally Posted by vivekshrivastava51
hi ,
ya this is good method to writ stored procedure
Thanks! For your comment
  #4  
Old April 20th, 2007, 03:42 PM
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North :)
Posts: 4,940

re: Creating SQL Stored Procedures


Hi jyothi1105!

This is a great post.
Thank you so much for providing us with this resource!

-Frinny
  #5  
Old April 20th, 2007, 03:56 PM
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,362

re: Creating SQL Stored Procedures


Quote:
Originally Posted by Frinavale
Hi jyothi1105!

This is a great post.
Thank you so much for providing us with this resource!

-Frinny
Frin,

Do you want to format this original post a little better and put it in "Frinny's tip o' the week" thread and we can start a sticky thread for all your (and others') tips o' the week?
  #6  
Old April 20th, 2007, 06:43 PM
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North :)
Posts: 4,940

re: Creating SQL Stored Procedures


Quote:
Originally Posted by RedSon
Frin,

Do you want to format this original post a little better and put it in "Frinny's tip o' the week" thread and we can start a sticky thread for all your (and others') tips o' the week?
This sounds like a great idea!
  #7  
Old January 15th, 2008, 05:16 AM
Newbie
 
Join Date: Jan 2008
Posts: 2

re: Creating SQL Stored Procedures


Great!
Thank you for posting your codes and knowledge. This will help us..
  #8  
Old February 26th, 2008, 07:20 AM
Newbie
 
Join Date: Feb 2008
Posts: 3

re: Creating SQL Stored Procedures


hi
like this stored procedure concepts...
hw can we store the datas into MS ACCESS files...
and hw can we retrive that.. using c#.....
  #9  
Old February 26th, 2008, 03:14 PM
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North :)
Posts: 4,940

re: Creating SQL Stored Procedures


Quote:
Originally Posted by prabur
hi
like this stored procedure concepts...
hw can we store the datas into MS ACCESS files...
and hw can we retrive that.. using c#.....
Hi Prabur,

You are going to have to use ADO.NET to connect to your MS Access database files using C#. After you have researched ADO.NET and attempted to solve your problem, please post any question or problems in the .NET forum to receive help.

Cheers!

-Frinny
  #10  
Old March 2nd, 2008, 12:20 PM
Member
 
Join Date: Feb 2007
Location: Qatar
Posts: 43

re: Creating SQL Stored Procedures


Really very usefull resource
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error Message Retreival in SQL Stored Procedures valh answers 0 June 25th, 2007 10:58 PM
Creating variables in sql stored procedures jyothi1105 answers 1 May 7th, 2007 12:34 PM
DWB and SQL Stored Procedures fayez answers 0 September 5th, 2006 03:45 PM
Can I use MS-SQL Stored Procedures through an ODBC connection? Lyle Fairfield answers 1 November 12th, 2005 04:04 PM