Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Adding a composite record to 2 tables Expand / Collapse
Author
Message
Posted Sunday, October 21, 2012 3:13 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, October 29, 2012 1:20 AM
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
Post #1375152
Posted Monday, October 22, 2012 1:43 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 11:44 AM
Points: 6,236, Visits: 7,380
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
Post #1375289
Posted Monday, October 22, 2012 3:28 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, October 29, 2012 1:20 AM
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
Post #1375313
Posted Monday, October 22, 2012 7:14 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, August 18, 2014 8:36 AM
Points: 2,836, Visits: 5,066
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1375411
Posted Monday, October 22, 2012 9:45 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 2:39 PM
Points: 1,932, Visits: 3,161
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.
Post #1375543
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse