Monday, March 26, 2012

multiple insert

hi friends,

i am having a problem here. I am using msde and i have a database with 3 tables.

Here is what i want to do.

1- I want to insert posted data to table 1 ( i can do this step)

2- I want to insert posted data to table 2 ( i can do this step, too)

3- I want to insert the primary keys of the rows inserted to table 1 and table 2 , to table 3 . I dont know anything about stored procedures . Can i do this in my code-behind using C# ? If you can show me the way i will be glad.
Thanks in advance

Using a stored procedure would be best. Assuming that you'reusing IDENTITY columns for your primary keys, your basic logic will besomething along the lines of:

DECLARE @.tbl1ID INT
DECLARE @.tbl2ID INT
INSERT Tbl1
(column list)
VALUES
(values)
SET @.tbl1ID = SCOPE_IDENTITY()

INSERT Tbl2
(column list)
VALUES
(values)
SET @.tbl2ID = SCOPE_IDENTITY()
INSERT Tbl3
(Tbl1ID, Tbl2ID)
VALUES
(@.tbl1ID, @.tbl2ID)
|||

thank you adam for you quick reply,
stored procedure way seems not that difficult but i dont know anythink about them. Where do you write the stored procedures, how do you run them , how do you call them from aspx.cs file, how do you reach the output variables from aspx.cs file ?
if you can tell me the steps i will be glad.
thanks in advance

|||I think you need to Google for some SQL Server introductoryarticles. Try www.sqlteam.com and www.sqlservercentral.com. It's a bit much to discuss in a forum post.

No comments:

Post a Comment