Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Adding a composite record to 2 tables


Adding a composite record to 2 tables

Author
Message
agabb
agabb
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 30
I need to add records to a DB on SQL Server 2005.

Each composite record represents a project in the table PROJECT, coupled with client and agent addresses in the table ADDRESS, with foreign keys Client_addr_id and Agent_addr_id to the primary key Addr_id.

Can someone show me how to add the composite record in one transaction? In particular, how do I get the identity values for the addresses, after adding them?

Andrew
Evil Kraig F
Evil Kraig F
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5715 Visits: 7660
Typically, using either SCOPE_IDENTITY() function or OUTPUT clause on the initial insert.

Can you post the DDL for the tables and an example of what data you're trying to insert? Also, are you inserting via a BCP/SSIS package from a flatfile, or are you going to be typing out the INSERT INTO command for the first table?


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
agabb
agabb
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 30
Please be aware that I'm not used to SQL Server scripts, but perhaps I could use something like the following. (There's quite a bit of guessing here.)

The intention of the following SQL is to create a record in ADDRESS, then add the PROJECT record, referencing the ADDRESS record with its identity.

INSERT INTO [ADDRESS] ([CLIENT_NAME], [ADDRESS1])
VALUES ('ABC CORP', '50 Langridge St.');
INSERT INTO [PROJECT] ([PROJ_NUMBER], [PROJ_NAME], CLIENT_ADDR_ID])
VALUES ('A445566', 'My Project Name', SELECT SCOPE_IDENTITY());
GO
COMMIT

Is this likely to work? How would I modify it if I had to add two ADDRESS records?

Andrew
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3046 Visits: 5478
In short: you can use INSERT with OUTPUT clause.
But, if you could specify which table you are going to load your data from, that would allow to advice you in more detailed way.
We will need your tables DDL. The link at the bottom of my signature leads to article explaining how to post one.

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Gazareth
Gazareth
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2876 Visits: 5382
agabb (10/22/2012)

INSERT INTO [ADDRESS] ([CLIENT_NAME], [ADDRESS1])
VALUES ('ABC CORP', '50 Langridge St.');
INSERT INTO [PROJECT] ([PROJ_NUMBER], [PROJ_NAME], CLIENT_ADDR_ID])
VALUES ('A445566', 'My Project Name', SELECT SCOPE_IDENTITY());


Slight modification for valid T-SQL:

INSERT INTO [ADDRESS] ([CLIENT_NAME], [ADDRESS1])
VALUES ('ABC CORP', '50 Langridge St.');
INSERT INTO [PROJECT] ([PROJ_NUMBER], [PROJ_NAME], CLIENT_ADDR_ID])
VALUES ('A445566', 'My Project Name', (SELECT SCOPE_IDENTITY()));

or:

INSERT INTO [ADDRESS] ([CLIENT_NAME], [ADDRESS1])
VALUES ('ABC CORP', '50 Langridge St.');
INSERT INTO [PROJECT] ([PROJ_NUMBER], [PROJ_NAME], CLIENT_ADDR_ID])
SELECT 'A445566', 'My Project Name', SCOPE_IDENTITY();

However. you're right to question for multiple inserts - this method only works for a single insert.
As Eugene says, for multiple inserts you can use the OUTPUT clause - grab the output from the Address insert into a table variable and then from there into the Project table. Alternatively you could create a trigger on the Address table that would do the same.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search