Adding a composite record to 2 tables

  • 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

  • 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[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • 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

  • 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[/url]

  • 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.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply