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_NULLSONGO
SET
QUOTED_IDENTIFIERONGO
-- =============================================
-- 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
uniqueidentifierAS
Begin
SetNoCountonDECLARE @.WhateverIDuniqueidentifierINSERT
INTO tblMessages(UserIDSender,MessageTitle,MessageContent,MessageType)VALUES
(@.UserIDSender,@.MessageTitle,@.MessageContent,@.MessageType)Select @.WhateverID=@.@.IdentityINSERT
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?!?
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