Click here to Skip to main content
15,880,427 members
Articles / Programming Languages / SQL
Alternative
Tip/Trick

Combining Insert/Update to one Procedure

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
3 Jan 2012CPOL 7.2K   1
CREATE PROCEDURE sp_Insert ( @ID int, @Name char(20), @Mode char(20) ) ASif @Mode='Insert in Employee'Begin insert into Employee(Employee_ID,Employee_Name)values(@ID,@Name)Endelse if @Mode='Insert in Student'Begin insert into...
SQL
CREATE PROCEDURE sp_Insert
	
	(
	@ID int,
	@Name char(20),
	@Mode char(20)
	)
	
AS
if @Mode='Insert in Employee'
Begin
	insert into Employee(Employee_ID,Employee_Name)values(@ID,@Name)
End
else if @Mode='Insert in Student'
Begin
    insert into Student(Student_ID,Student_Name)values(@ID,@Name)
End

	RETURN

============================================================================and on the buttons_click this will be coding...!

C#
private void Employee_Save_Click(object sender, EventArgs e)
       {
           SqlConnection conn = new SqlConnection(" ");
           conn.Open();
           SqlCommand cmd=new SqlCommand("SP_INSERT",conn);
           cmd.CommandType=CommandType.StoredProcedure;
           cmd.Parameters.AddWithValue("@ID",txtEmployeeID);
           cmd.Parameters.AddWithValue("@Name",txtEmployeeName);
           cmd.Parameters.AddWithValue("@Mode","Insert in Employee");
           cmd.ExecuteNonQuery();
           conn.Close();

       }
       private void Student_Save_Click(object sender, EventArgs e)
       {
           SqlConnection conn = new SqlConnection("")
           conn.Open();
           SqlCommand cmd = new SqlCommand("SP_INSERT", conn);
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.Parameters.AddWithValue("@ID", txtStudentID);
           cmd.Parameters.AddWithValue("@Name",txtStudentName);
           cmd.Parameters.AddWithValue("@Mode", "Insert in Student");
           cmd.ExecuteNonQuery();
           conn.Close();
       }

May be there would be difference coding Format...!

License

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


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralWhy do you people insist on an additional 'Mode' the ID valu... Pin
Mycroft Holmes10-Jan-12 19:20
professionalMycroft Holmes10-Jan-12 19:20 

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.