Creating SQL Stored Procedures

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jyothi1105
    New Member
    • Apr 2007
    • 14

    Creating SQL Stored Procedures

    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:
    [Code=sql]
    CREATE PROCEDURE AddStudent @Sno VARCHAR(10),@Sn ame VARCHAR(20),@Sa ddress VARCHAR(50)
    AS
    Begin
    INSERT INTO STUDENT(Sno ,Sname ,Saddress ) values (@Sname ,@Sname,@Saddre ss)
    End[/Code]

    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.

    [Code=vbnet]
    Private Sub SaveButton_Clic k() Handles SaveButton.Clic k
    Dim conn As SqlConnection = New SqlConnection(" data Source=yourserv er/systemno;uid="u serID";pwd="pas sword";initial catalog=dbname" )
    Try
    conn.Open()
    Dim strcmd As SqlCommand = New SqlCommand("stu ", conn)
    strcmd.CommandT ype = CommandType.Sto redProcedure
    strcmd.Paramete rs.Add("@sno", TextBox1.Text)
    strcmd.Paramete rs.Add("@sname" , TextBox2.Text)
    strcmd.Paramete rs.Add("@saddre ss", TextBox3.Text)
    strcmd.ExecuteN onQuery()
    conn.Close()
    Catch ex As Exception

    End Try
    End Sub
    End Class
    [/code]
    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; Mar 9 '09, 03:04 PM. Reason: Major edit on Jyothi's original post
  • vivekshrivastava51
    New Member
    • Mar 2007
    • 18

    #2
    hi ,
    ya this is good method to writ stored procedure

    Comment

    • jyothi1105
      New Member
      • Apr 2007
      • 14

      #3
      Originally posted by vivekshrivastav a51
      hi ,
      ya this is good method to writ stored procedure
      Thanks! For your comment

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Hi jyothi1105!

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

        -Frinny

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          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?

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            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!

            Comment

            • vidz
              New Member
              • Jan 2008
              • 2

              #7
              Great!
              Thank you for posting your codes and knowledge. This will help us..

              Comment

              • prabur
                New Member
                • Feb 2008
                • 3

                #8
                hi
                like this stored procedure concepts...
                hw can we store the datas into MS ACCESS files...
                and hw can we retrive that.. using c#.....

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  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

                  Comment

                  • firozfasilan
                    New Member
                    • Feb 2007
                    • 42

                    #10
                    Really very usefull resource

                    Comment

                    Working...