sqlsql-serverinsertuniqueidentifiernewid

NEWID() is generating the same value for all records in a relation


DECLARE @GUID uniqueidentifier 
SET @GUID = NEWID()

INSERT INTO [MetaData Dummy].dbo.xxxx (someid, somename, LastUpdated, RecordSource)
SELECT @GUID, TempCol, GETDATE(), xxx
FROM AP_DevIDs

Using the code above I am trying to insert a value into each record of the attribute someid, which is of type uniqueidentifier. However, using the NEWID() function I am getting the same value in every record, so is obviously not unique.

Any help is appreciated, cheers


Solution

  • Because you bound value to variable you get always the same value, you need to generate it per row. Use:

    INSERT INTO [MetaData Dummy].dbo.xxxx (someid, somename, LastUpdated, RecordSource)
    SELECT NEWID(), TempCol, GETDATE(), xxx
    FROM AP_DevIDs