Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is UNIQUEIDENTIFIER an auto-generated number when inserting values in a table?

Tags:

sql-server

I have an error when loading a procedure telling me

Cannot insert the value NULL into column 'requestID', table 'MCAST.a01.tbl_enrollmentRequests'; column does not allow nulls. INSERT fails.

Now requestID is a UNIQUEIDENTIFIER type of variable. Is UNIQUEIDENTIFIER an auto generated number or not? Below is a sample of my code where you can see requestID.

CREATE PROCEDURE [a01].[usp_auditAcceptRequest]
    (@AccountID UNIQUEIDENTIFIER, 
     @GroupID UNIQUEIDENTIFIER, 
     @Reason NVARCHAR(45)
    )
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO [a01].[tbl_enrollmentRequests] (requestDate, groupID, accountID)
    VALUES (SYSDATETIMEOFFSET(), @GroupID, @AccountID)

    DECLARE @RequestID UNIQUEIDENTIFIER 

    SET @RequestID = (SELECT requestID 
                      FROM [a01].tbl_enrollmentRequests 
                      WHERE groupID = @GroupID AND accountID = @AccountID)

    INSERT INTO [a01].[tbl_enrollmentAudits] (entryDate, requestID, groupID, accountID, accepted, reason)
    VALUES (SYSDATETIMEOFFSET(), @RequestID, @GroupID, @AccountID, 1, @Reason)

    DELETE FROM [a01].[tbl_enrollmentRequests]
    WHERE requestID = @RequestID
END;
GO

Here is where I am implementing the above procedure

BEGIN
DECLARE @AccountID UNIQUEIDENTIFIER;
DECLARE @GroupID UNIQUEIDENTIFIER;

(SELECT @AccountID = accountID 
FROM [a01].[tbl_userAccounts] WHERE accountUsername='saraht');

(SELECT @GroupID = groupID FROM [a01].[tbl_groups] WHERE groupName LIKE '%Foo%');

EXECUTE [a01].[usp_addRequest] @AccountID, @GroupID;
END;
GO

Thanks for your help !!

like image 772
BakingCake Avatar asked Apr 02 '16 16:04

BakingCake


1 Answers

A uniqueidentifier is a normal column, and if you want to have a automatically assigned value you need to add a default to the column. Typically the functions used for the default are newid() or newsequentialid().

Edit based on the posted table definition; you could use this:

CREATE TABLE [a01].[tbl_enrollmentRequests](
  requestID UNIQUEIDENTIFIER PRIMARY KEY DEFAULT (NEWID()), 
  requestDate DATETIMEOFFSET NOT NULL, 
  groupID UNIQUEIDENTIFIER REFERENCES [a01].[tbl_groups] (groupID) NOT NULL, 
  accountID UNIQUEIDENTIFIER REFERENCES [a01].[tbl_userAccounts] (accountID) NOT NULL
);

That being said, you can also pre-generate a uniqueidentifier and assign that to a variable in the stored procedure prior to insertion, since the generated GUID can be assumed not to collide with any existing GUID. The benefit of this is that you know the id of the inserted row even without retrieving it from an OUTPUT clause.

A notice on performance: a significant number of rows with a clustered primary key of random GUIDs (as generated bynewid()) are a performance issue, since the inserts will cause many page splits to occur due to the randomness. The newsequentialid() function pretty much completely resolves the performance problem, but it makes the generated GUIDs guessable, so that this can only be used when "random" IDs are not required.

like image 118
Lucero Avatar answered Oct 04 '22 06:10

Lucero