ios85
ios85

Reputation: 2134

Get data from Last Inserted Row

I am trying to figure out how I would get the firstname and lastname from the INSERT statement that I just used. It created the row, and I want to use the First Name and the Last Name to use in the next insert statement, How would I go about doing this?

Declare @idnt int

INSERT INTO dbo.Contacts (ContactFirstName,ContactLastName,ContactCompany) VALUES ('Richard','Burns',NULL);

SELECT @idnt = SCOPE_IDENTITY()

INSERT INTO dbo.Activity (ContactID, ActivityDate, ActivityName, ActivityNote, ActivityOwner)
VALUES (@idnt, '12/13/2011 10:55AM', 'Contact ' + @fname + " " + @lname + ", was automatically added by the system', 'Contact was automatically added by this system', 'System')

Upvotes: 1

Views: 1617

Answers (3)

Eric
Eric

Reputation: 95133

Use output!

INSERT INTO dbo.Activity (
    ContactID, 
    ActivityDate, 
    ActivityName, 
    ActivityNote, 
    ActivityOwner
)
SELECT 
    ContactID, 
    GETDATE(), 
    'Contact ' + ContactFirstName + ' ' + ContactLastName, 
    'Contact was added by the system.', 
    'System'
FROM (
    INSERT INTO dbo.Contacts (ContactFirstName,ContactLastName,ContactCompany) 
    OUTPUT inserted.ContactID, inserted.ContactFirstName, inserted.ContactLastName
    VALUES ('Richard','Burns',NULL)
) x

For more on the magic that is output, see here.

You can also use a trigger on the table to make it automagic rather than having to insert it manually each time. It uses the inserted/deleted tables, as well.

You would do that with this:

CREATE TRIGGER Contact_LogActivity ON dbo.Contacts FOR INSERT AS
INSERT INTO dbo.Activity (
    ContactID, 
    ActivityDate, 
    ActivityName, 
    ActivityNote, 
    ActivityOwner
)
SELECT
    ContactID,
    GETDATE(), 
    'Contact ' + ContactFirstName + ' ' + ContactLastName, 
    'Contact was added by the system.', 
    'System'
FROM
    Inserted

Now, whenever any row gets inserted into Contacts, it logs it into Activity. For more on triggers, see here.

Upvotes: 4

daniloquio
daniloquio

Reputation: 3902

I think you are looking for an insert with a select statement. Try this:

    INSERT INTO dbo.Activity (ContactID, ActivityDate, ActivityName, ActivityNote, ActivityOwner)
    SELECT @idnt, '12/13/2011 10:55AM', 'Contact ' + ContactFirstName + ' ' + ContactLastName + ', was automatically added by the system', 'Contact was automatically added by this system', 'System'
    FROM Contacts
    WHERE ContactId = @idnt

Upvotes: 0

Nonym
Nonym

Reputation: 6299

Using the identity caught by SCOPE_IDENTITY(), query back the table and grab the ContactFirstName and ContactLastName into your declared variables: @fname and @lname respectively:

...
SELECT @idnt = SCOPE_IDENTITY()

SELECT
    @fname = ContactFirstName
  , @lname = ContactLastName
FROM dbo.Contacts 
WHERE YOUR_IDENTITY_COLUMN_HERE = @idnt

INSERT INTO dbo.Activity ...

Replace YOUR_IDENTITY_COLUMN_HERE with the name of the identity column from which you assumed to grab SCOPE_IDENTITY() from and test..

Upvotes: 0

Related Questions