Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Scope_identity()
Message
From
10/10/2003 01:20:36
 
 
To
09/10/2003 22:40:41
General information
Forum:
ASP.NET
Category:
ADO.NET
Miscellaneous
Thread ID:
00837243
Message ID:
00837265
Views:
33
Mike,

Keith and Steve have given you good answers, but if I may add one more suggestion and that is to use an OUTPUT parameter in your Stored Proc instead of returning a key value. In fact, we use the same SP for both inserts and updates, defining an INSERT when the primarykey is NULL or 0, and an UPDATE otherwise:
CREATE PROCEDURE MyTable_Put
    @MyPK int = NULL OUTPUT,
    @parm2 char(2) = NULL,
    @parm3 char(3) = NULL
AS
    IF (@MyPK = 0 OR @MyPK IS NULL)
    BEGIN
        INSERT INTO MyTable (MyColumn2, MyColumn3)
            SELECT @parm2, @parm3

        SELECT @MyPK = SCOPE_IDENTITY()
    END
    ELSE
        UPDATE MyTable
        SET MyColumn2 = ISNULL(@parm2, MyColumn2),
            MyColumn3 = ISNULL(@parm3, MyColumn3)
        WHERE MyPKColumn = @MyPK
Then, in your DataAccess method, you just insert the row into the Parent table first, get the newly added row's PK by using the Parameters collection, stick that into the FK in the child table and then insert the child row. And be sure to wrap the whole thing up in a transaction.

~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform