hi!
one column of one table values is inserted in second table ata time.
i am taking two tables. 1st table contain id this is autoidentity and address1,mobileno etc.
and 2nd table contain id address1,mobile.
my aim is insert record into two tables. please give a query?
thanks
Loading
VasanthPosted Apr 10, 2010, 8:09 AM
CREATE PROCEDURE dbo.InsertTwoTables
@address1 nvarchar(500),
@mobileno nvarchar(50),
AS
BEGIN TRY
BEGIN TRANSACTION -- Start the transaction
Set Nocount On
DECLARE @ID int
INSERT INTO Table1(Address, MobileNo) VALUES (@address1, @mobileno)
SELECT @ID=@@IDENTITY
INSERT INTO ImageInAlbum(FKID, Address, MobileNo) VALUES (@ID, @address1, @mobileno)
-- If we reach here, success!
COMMIT
END TRY
BEGIN CATCH
-- Error
IF @@TRANCOUNT > 0
ROLLBACK
-- Raise an error
DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int
SELECT @ErrMsg = ERROR_MESSAGE(),
@ErrSeverity = ERROR_SEVERITY()
RAISERROR(@ErrMsg, @ErrSeverity, 1)
END CATCH
Hope it helps out , please mark as answer.
ajay rajuPosted Apr 10, 2010, 7:25 AM
Thank u for giving a Quick Reply,
Peviously i am using like ur code.
i take two tables in sql server one for saving contact details and another for saving company details.
my aim is when user click Save Details button.that values are goes to two tables.
Please tell how to write a Stored procedure code for insert values into Two tables at a time.and stored procedure is also only one.
Thnaks.
Dipa AhujaPosted Apr 10, 2010, 2:55 AM
See this:
static string connstring = "your connection string of dbase"
SqlConnection obj;
SqlDataAdapter da;
DataSet ds;
ProfileCommon p;
obj = new SqlConnection(connstring);
protected void btnadd_Click(object sender, EventArgs e)
{
//insert into table 1
obj.Open();
SqlCommand comm = new SqlCommand("insert into table1 (addr,mob) values (@addr,@mob)" obj);
comm.Parameters.AddWithValue("@addr",txttitle.Text.ToString());
comm.Parameters.AddWithValue("@mob",txtmob.Text.Tostring()));
comm.ExecuteNonQuery();
obj.Close();
//insert into table 1
da=new SqlDataAdapter("Select * from table1",connstring);
ds = new DataSet();
da.Fill(ds);
//extract last inserted record from table 1
int cnt = ds.Tables[0].Rows.Count;
int id =Convert.ToInt32(ds.Tables[0].Rows[cnt - 1]["id"].ToString());
//insert into table 2
obj.Open();
SqlCommand comm2 = new SqlCommand("insert into table 2 (id,addr) values (@id,@addr)" obj);
comm2.Parameters.AddWithValue("@id",id);
comm.Parameters.AddWithValue("@addr",txttitle.Text.ToString());
comm2.ExecuteNonQuery();
obj.close();
}
ajay rajuPosted Apr 10, 2010, 12:39 AM
i am taking 2nd table ID is int. My aim is First table AutoIdentity value is goes to Second table's ID column. Please Give a Query of that.
i am not using any datacontrols. i am taking two tables in sqlserver. and in my asp.net application i take textboxes to insert values into
database. if i click insert button that values are goes two tables. it must in second table id is equal to first table Auto identity column,
Thank u.
Dipa AhujaPosted Apr 9, 2010, 1:40 PM
are you using any data control like gridview or datalist and using the sqldatasource for insertion or code behind?
n which data you are inserting for addres, and mobile (Are you using texbox value?)
and the 2nd table's id field is also autoidentity?