简体   繁体   中英

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')

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 .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM