Home All Groups Group Topic Archive Search About

How do you get the PK during an INSERT?

Author
12 Aug 2005 7:00 PM
Michael Kellogg
I'm doing a two-step process as I insert records from a form.  I first add
quote "header" info into the QUOTES table, then I want to add the multi-
line quote "details" from a grid into a QUOTE_DETAILS table.  The link is
QUOTES.QUOTE_ID.

I'd like to use a SPROC to accomplish this.  How can I insert a record into
QUOTES, get the newly-generated primary key, then use that for the FK as I
insert records into the QUOTE_DETAILS table?

--
Michael

Author
12 Aug 2005 7:09 PM
JT
If the primary key is an identity column, then the scope_identity() function
will return the last generated value for your session.

Show quote
"Michael Kellogg" <mkell***@WEDELIVERcc3.com> wrote in message
news:Xns96B07A279572BmkelloggWEDELIVERcc3@207.46.248.16...
> I'm doing a two-step process as I insert records from a form.  I first add
> quote "header" info into the QUOTES table, then I want to add the multi-
> line quote "details" from a grid into a QUOTE_DETAILS table.  The link is
> QUOTES.QUOTE_ID.
>
> I'd like to use a SPROC to accomplish this.  How can I insert a record
> into
> QUOTES, get the newly-generated primary key, then use that for the FK as I
> insert records into the QUOTE_DETAILS table?
>
> --
> Michael
Author
12 Aug 2005 7:11 PM
Narayana Vyas Kondreddi
It depends on how the primary key is being generated. Are you using an
IDENTITY column as primary key? Or a GUID? In case of IDENTITY column, see
@@IDNTITY and SCOPE_IDENTITY() in SQL Server Books Online.

In case of a GUID, see:
http://vyaskn.tripod.com/retrieve_guid_value_like_identity.htm

If something else, let us know.
--
HTH,
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @ http://vyaskn.tripod.com/



Show quote
"Michael Kellogg" <mkell***@WEDELIVERcc3.com> wrote in message
news:Xns96B07A279572BmkelloggWEDELIVERcc3@207.46.248.16...
> I'm doing a two-step process as I insert records from a form.  I first add
> quote "header" info into the QUOTES table, then I want to add the multi-
> line quote "details" from a grid into a QUOTE_DETAILS table.  The link is
> QUOTES.QUOTE_ID.
>
> I'd like to use a SPROC to accomplish this.  How can I insert a record
> into
> QUOTES, get the newly-generated primary key, then use that for the FK as I
> insert records into the QUOTE_DETAILS table?
>
> --
> Michael
Author
12 Aug 2005 7:21 PM
Michael Kellogg
"Narayana Vyas Kondreddi" <answer***@hotmail.com> wrote:

> It depends on how the primary key is being generated. Are you using an
> IDENTITY column as primary key? Or a GUID? In case of IDENTITY column,
> see @@IDNTITY and SCOPE_IDENTITY() in SQL Server Books Online.
>
> In case of a GUID, see:
> http://vyaskn.tripod.com/retrieve_guid_value_like_identity.htm
>
> If something else, let us know.

Identity column, yes.  Thanks, that works great!

--
Michael Kellogg
Author
12 Aug 2005 9:02 PM
--CELKO--
>> I'm doing a two-step process as I insert records [sic] from a form. <<

Let's get back to the basics of an RDBMS. Rows are not records and
certainly NOT lines on a form; fields are not columns; tables are not
files; there is no sequential access or ordering in an RDBMS.  The form
has fields; your table has columns and the two of them can have toally
different structures.

>>  I first add quote "header" info into the QUOTES table, then I want to add the multi-
line quote "details" from a grid into a QUOTE_DETAILS table.  <<

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications.  It is very hard to debug code when you do not let us
see it.

>> I'd like to use a SPROC to accomplish this.  How can I insert a record sic] into
QUOTES, get the newly-generated primary key, then use that for the FK
as I insert records into the QUOTE_DETAILS table? <<

Completely wrong mental model!!!  BY DEFINITION a relational key is a
subset of attributes of an entity; it is discovered or assigned by an
outside authoriuty/  It is not created by putting something into
physical storage.  Sequential record numbers, pointers or other
physical locators in a file system (not an RDBMS) are created by
insertion.

If you decide to use the proprietary, non-relational, unauditable
IDENTITY columns, be suyre to tell the trolls in accounting and legal
about it.  This si the way that Newbie kludge systems to death.

AddThis Social Bookmark Button