'Operand type clash: numeric is incompatible with uniqueiden

Click For Summary
The error "Operand type clash: numeric is incompatible with uniqueidentifier" occurs because SCOPE_IDENTITY() returns the last identity value generated for any table in the current session, which is a numeric type, not a uniqueidentifier. To retrieve the uniqueidentifier assigned to the newly inserted row in the Partners table, the procedure should use the OUTPUT clause in the INSERT statement. This allows the new GUID to be directly assigned to the @new_guid parameter. The correct implementation would involve modifying the INSERT statement to include the OUTPUT clause to capture the uniqueidentifier. This adjustment resolves the type clash and correctly returns the GUID of the inserted row.
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 SlurrerOfSpeech
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
2
Views
2K