patternsqlModerate
Return the uniqueidentifier generated by a default on insert
Viewed 0 times
uniqueidentifiergeneratedtheinsertreturndefault
Problem
Goal
Retrieve the latest guid value in real time after you have inserted the value in the table
Problem
Don't know how to do it
Info
Table
Retrieve the latest guid value in real time after you have inserted the value in the table
Problem
Don't know how to do it
Info
- The code should only specify new values for address and zipcode
- There can be lots of data in the table
Table
CREATE TABLE [AddressBook]
(
[testID] [uniqueidentifier] NOT NULL default newid(),
[address] [nvarchar](50) NULL,
[zipcode] [nvarchar](50) NULL
)Solution
I think you are looking for output
uniqueidentifier may not be the most efficient id here but this is an answer to the stated question
DECLARE @MyTableVar table([testID] [uniqueidentifier]);
INSERT [AddressBook] ([address], [zipcode])
OUTPUT INSERTED.[testID] INTO @MyTableVar
VALUES (N'address', N'zipcode');
--Display the result set of the table variable.
SELECT [testID] FROM @MyTableVar;
GOuniqueidentifier may not be the most efficient id here but this is an answer to the stated question
Code Snippets
DECLARE @MyTableVar table([testID] [uniqueidentifier]);
INSERT [AddressBook] ([address], [zipcode])
OUTPUT INSERTED.[testID] INTO @MyTableVar
VALUES (N'address', N'zipcode');
--Display the result set of the table variable.
SELECT [testID] FROM @MyTableVar;
GOContext
StackExchange Database Administrators Q#125516, answer score: 16
Revisions (0)
No revisions yet.