Go4Expert

Go4Expert (http://www.go4expert.com/)
-   SQL Server (http://www.go4expert.com/forums/sql-server-forum/)
-   -   Hard work with GUID. Help me! (http://www.go4expert.com/forums/hard-guid-help-t13697/)

joecole 10Sep2008 19:38

Hard work with GUID. Help me!
 
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.

xpi0t0s 13Sep2008 04:58

Re: Hard work with GUID. Help me!
 
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.


All times are GMT +5.5. The time now is 13:09.