Monday, March 12, 2012

EXPERT: Stored procedure operand type clash

Im trying to insert a new record into tblMessages...the unique ID generated for MessageID is the value I want to use as a value for the MessageID field when inserting a new record in tblUsersAndMessages

I have the following sp:

-- ================================================

-- Template generated from Template Explorer using:

-- Create Procedure (New Menu).SQL

--

-- Use the Specify Values for Template Parameters

-- command (Ctrl-Shift-M) to fill in the parameter

-- values below.

--

-- This block of comments will not be included in

-- the definition of the procedure.

-- ================================================

SET

ANSI_NULLSON

GO

SET

QUOTED_IDENTIFIERON

GO

-- =============================================

-- Author: <Author,,Name>

-- Create date: <Create Date,,>

-- Description: <Description,,>

-- =============================================

CREATE

PROCEDURE spNewMessage

@.UserIDSender

nvarchar(256),

@.MessageTitle

nvarchar(50),

@.MessageContent

text,

@.MessageType

int,

@.MessageID

uniqueidentifier,

@.UserID

uniqueidentifier

AS

Begin

SetNoCountonDECLARE @.WhateverIDuniqueidentifier

INSERT

INTO tblMessages(UserIDSender,MessageTitle,MessageContent,MessageType)

VALUES

(@.UserIDSender,@.MessageTitle,@.MessageContent,@.MessageType)Select @.WhateverID=@.@.Identity

INSERT

INTO tblUsersAndMessages(MessageID,UserID)

VALUES

(@.WhateverID,@.UserID)

End

GO

And here are my tables:
tblUsersAndMessages
allow nulls
MessageID uniqueidentifier false
UserID uniqueidentifier false
New bit false *default set to ((1))


tblMessages
allow nulls
MessageID uniqueidentifier false *PK default set to (newid())
UserIDSender uniqueidentifier false
MessageTitle nvarchar(50) true
MessageContent text true
SentDateTime datetime false * default set to (getdate())
MessageType int false

Now when I try to add my SP I get this error:
Msg 206, Level 16, State 2, Procedure spNewMessage, Line 22
Operand type clash: numeric is incompatible with uniqueidentifier

What can this be?!?

Try usingSELECT SCOPE_IDENTITY() instead of @.@.IDENTITY. Seehere for why. It might not solve the problem but it's best practice, anyway.|||I've read the site you provided, but I cant seem to find out HOW I can use the value returned by select SCOPE_IDENTITY()...
Could you provide me with an example?

Thanks!|||Hi. You can us it as follows:

SET @.WhateverID = SCOPE_IDENTITY()

It may not solve your problem, but it's something you should do anyway. Good luck!|||

That's because SCOPE_IDENTITY is used to the lastIDENTITY value inserted into an IDENTITY column in the same scope, so it will return a numberic value, rather than uniqueidentifier. So we have to retrive the UNIQUEIDENTIFIER of the last inserted row from the table, for example:

ALTER PROCEDURE spNewMessage @.UserIDSenderuniqueidentifier, @.MessageTitle nvarchar(50),
@.MessageContent text, @.MessageType int, @.MessageID uniqueidentifier,
@.UserID uniqueidentifier

AS

Begin

Set NoCount on

DECLARE @.WhateverID uniqueidentifier
INSERT

INTO tblMessages(UserIDSender,MessageTitle,MessageContent,MessageType)
VALUES

(@.UserIDSender,@.MessageTitle,@.MessageContent,@.MessageType)

Select @.WhateverID=MessageID FROM tblMessages ORDER BY SentDateTime ASC
INSERTINTO tblUsersAndMessages(MessageID,UserID)
VALUES(@.WhateverID,@.UserID)
End

GO

No comments:

Post a Comment