-1

Goal:
Retrieve the latest guid value in real time after you have inserted the the value in the table

Problem:
Don't know how to do it

Info:
*You can only add a new vale that is address and zip code
*Please take account to that it can be lots of data!

CREATE TABLE [AddressBook]
(
    [testID] [uniqueidentifier] NOT NULL default newid(),
    [address] [nvarchar](50) NULL,
    [zipcode] [nvarchar](50) NULL
)
HelloWorld1
  • 13,688
  • 28
  • 82
  • 145
  • You can use an [`OUTPUT`](https://msdn.microsoft.com/en-us/library/ms177564.aspx) clause to get any data from the rows (Note plural.), e.g. identity column values for newly inserted rows. `OUTPUT` can be used with `INSERT`, `UPDATE`, `DELETE` and `MERGE` and provides access to both before and after values in the case of `UPDATE`. A tool well worth having in your pocket. – HABO Jan 07 '16 at 20:39

1 Answers1

0

You can retrieve the inserted GUID through the Insert Stored Procedure like this sample:

CREATE PROCEDURE [dbo].[spUpdateBookmark]
    @BookmarkGuid [uniqueidentifier] = null,
    @otherfield nvarchar(256),
    @NewBookmarkGuid [uniqueidentifier] OUTPUT
WITH EXECUTE AS CALLER
AS
BEGIN
        SET @BookmarkGuid = NewID()
        INSERT INTO Bookmarks
            (BookmarkGuid,
             otherfield)
            Values
            (@BookmarkGuid,
             @otherfield)
    SET @NewBookmarkGuid = @BookmarkGuid
END

Then when you call the procedure you add a parameter for output value.

Dr. Aaron Dishno
  • 1,859
  • 1
  • 29
  • 24
  • In this context I cannot havea predefined guid value before inserting it, not in this context. – HelloWorld1 Jan 07 '16 at 16:40
  • basically i do the same as this: http://stackoverflow.com/questions/810829/best-way-to-get-pk-guid-of-inserted-row so you are saying the stored procedure cant create the NewID()? – Dr. Aaron Dishno Jan 07 '16 at 17:06