Hard work with GUID. Help me!

Discussion in 'SQL Server' started by joecole, Sep 10, 2008.

  1. joecole

    joecole New Member

    Sep 10, 2008
    Likes Received:
    Trophy Points:
    I created two tables:

    Table1 has fields: author_id (uniqueidentifier), author_name (char).

    Table2 : book_name (char), author_id (the foreign key from Table1).

    I inserted into Table1 a record that its author_id was generated by NEWID() function. After that, I inserted into Table2 a record which had author_id was the same as the record in Table1.

    To do that, I wrote down the value of author_id from Table1 and I carefully typed it into author_id (Table2).

    How could I do to make life easier.

    Thank U.
  2. xpi0t0s

    xpi0t0s Mentor

    Aug 6, 2004
    Likes Received:
    Trophy Points:
    Senior Support Engineer
    If you store the return value from NEWID() somewhere, then you can insert into Table1 and Table2 from the same variable. Essentially just using a variable to replace your scratchpad.

    If NEWID() is a database function and you performed INSERT INTO TABLE1 VALUES(NEWID(), 'name') then you'd replace this with something like SELECT NEWID() FROM DUAL into a host variable :V, then INSERT INTO TABLE1 VALUES:)V,'name) and INSERT INTO TABLE2 VALUES('book',:V).

    Or you could combine the first two with something like INSERT INTO TABLE1 VALUES(NEWID(), 'name') RETURNING AUTHOR_ID INTO :V, if your database supports RETURNING.

    Exact syntax will depend on what database you're using (specify author and complete version number if you want more detailed followup) and what language and API you're using to access the database.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice