'Operand type clash: numeric is incompatible with uniqueiden

Click For Summary
SUMMARY

The error "Operand type clash: numeric is incompatible with uniqueidentifier" occurs in SQL Server when attempting to assign the result of SCOPE_IDENTITY() to a UNIQUEIDENTIFIER variable. The procedure AddPartner is designed to insert a new partner into the Partners table and return the newly assigned GUID. However, SCOPE_IDENTITY() only returns the last identity value generated for any table in the current session and scope, which is not applicable for UNIQUEIDENTIFIER types. To retrieve the last inserted GUID, consider using the OUTPUT clause in the INSERT statement.

PREREQUISITES
  • Understanding of SQL Server stored procedures
  • Familiarity with UNIQUEIDENTIFIER data type
  • Knowledge of the INSERT statement and its OUTPUT clause
  • Experience with SCOPE_IDENTITY() function
NEXT STEPS
  • Research how to use the OUTPUT clause in SQL Server INSERT statements
  • Learn about handling UNIQUEIDENTIFIER data types in SQL Server
  • Explore alternatives to SCOPE_IDENTITY() for retrieving GUIDs
  • Examine best practices for designing SQL Server stored procedures
USEFUL FOR

SQL Server developers, database administrators, and anyone involved in creating or maintaining stored procedures that handle UNIQUEIDENTIFIER types.

SlurrerOfSpeech
Messages
141
Reaction score
11
Could use some help figuring out the error

Operand type clash: numeric is incompatible with uniqueidentifier

which points to the specified line below

Code:
CREATE PROCEDURE AddPartner 
    @name NVARCHAR(50), @email NVARCHAR(254), @new_guid UNIQUEIDENTIFIER OUTPUT
AS
BEGIN
    SET NOCOUNT ON 
    INSERT INTO Partners (name,email) VALUES (@name,@email)
    SELECT @new_guid = SCOPE_IDENTITY() -- specified line
    RETURN
END

The table Partners is of the form

Code:
CREATE TABLE Partners ( 
    id UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID(),
    name NVARCHAR(50) NOT NULL,
    email NVARCHAR(254) NOT NULL, -- 254 is optimal length according to http://stackoverflow.com/questions/1199190/what-is-the-optimal-length-for-an-email-address-in-a-database
    PRIMARY KEY (id)
);

Any help in figuring out this error? What I'm trying to do is return from the procedure the guid that was just assigned to the row that was just inserted.
 
Technology news on Phys.org
SlurrerOfSpeech said:
Could use some help figuring out the error

Operand type clash: numeric is incompatible with uniqueidentifier

which points to the specified line below

Code:
CREATE PROCEDURE AddPartner
    @name NVARCHAR(50), @email NVARCHAR(254), @new_guid UNIQUEIDENTIFIER OUTPUT
AS
BEGIN
    SET NOCOUNT ON
    INSERT INTO Partners (name,email) VALUES (@name,@email)
    SELECT @new_guid = SCOPE_IDENTITY() -- specified line
    RETURN
END

The table Partners is of the form

Code:
CREATE TABLE Partners (
    id UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID(),
    name NVARCHAR(50) NOT NULL,
    email NVARCHAR(254) NOT NULL, -- 254 is optimal length according to http://stackoverflow.com/questions/1199190/what-is-the-optimal-length-for-an-email-address-in-a-database
    PRIMARY KEY (id)
);

Any help in figuring out this error? What I'm trying to do is return from the procedure the guid that was just assigned to the row that was just inserted.
SCOPE_IDENTITY() doesn't work with guid fields.

Here they show how you could get the last inserted guid (haven't tested it).
 
  • Like
Likes   Reactions: SlurrerOfSpeech

Similar threads

Replies
2
Views
2K