'Operand type clash: numeric is incompatible with uniqueiden

In summary, the conversation is about an error that occurs when trying to use SCOPE_IDENTITY() to return the GUID of the last inserted row in a table. The error is due to an operand type clash between numeric and uniqueidentifier data types. The conversation also mentions the structure of the table and suggests an alternative method for retrieving the last inserted GUID.
  • #1
SlurrerOfSpeech
141
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
  • #2
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

1. What causes an operand type clash?

An operand type clash occurs when there is a mismatch between the data types of two operands in a mathematical or logical operation.

2. Why is numeric incompatible with uniqueidentifier?

Uniqueidentifier is a data type used to store globally unique identifiers, while numeric is a data type used to store numbers. These two data types are incompatible because they serve different purposes and have different storage requirements.

3. How can I fix the operand type clash error?

To fix this error, you can either change the data type of one of the operands to match the other, or convert one of the operands to a compatible data type before performing the operation.

4. Can this error be caused by data entry mistakes?

Yes, data entry mistakes such as entering a string instead of a number or vice versa can cause an operand type clash error. It is important to double check the data types of operands before performing operations on them.

5. Is there a way to prevent this error from occurring?

To prevent this error, it is important to ensure that the data types of operands are compatible before performing any operations. It is also helpful to have a good understanding of the data types used in your database and their respective purposes.

Similar threads

  • Programming and Computer Science
Replies
7
Views
424
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
5
Views
14K
Back
Top