GUID and last inserted row.

Posted on 2007-08-02
Last Modified: 2012-06-21

i have a table with a uniqueidentifier(GUID) set to auto assign as it's primary key.
I would like to know the sql query that i can run after an insert statment to get the last row i just inserted?
Question by:cossy74
    LVL 75

    Accepted Solution

    try this way

    declare @Guid uniqueIdentifier
    SELECT @Guid= NEWID()
    INSERT INTO urTable (UNID,  ... )
    SELECT @Guid, ..

    Author Comment

    aneeshattingal: I dynamically create a SqlCommand and execute it. I therefore do not think i can use your solution as i am not  using a stored procedure.
    LVL 75

    Assisted Solution

    by:Aneesh Retnakaran
    you need to proceed in this way, otherwise you can do a select query with all the inserted values in the where part
    LVL 142

    Assisted Solution

    by:Guy Hengel [angelIII / a3]
    I have to agree with aneeshattingal, you need to use NEWID() function explicitely, otherwise you have no way to get the value.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
    Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now