Error converting data type varchar to uniqueidentifier

This seemed like a straight-forward issue, and it was, after I straightened out the curves. I had a simple logging mechanism, a SQL stored procedure something like this:

CREATE PROCEDURE [dbo].[LogError]
 @Message VARCHAR(500)
 , @Id UNIQUEIDENTIFIER
AS
 INSERT INTO ErrorLog (Message, Id)
 VALUES (@Message, @Id)

And in a calling stored procedure:

EXEC LogError 'An invalid response was returned.', NEWID

Both stored procedures compiled just fine. However, when I attempted to log the message, I got the error

Error converting data type varchar to uniqueidentifier.

It turns out that just because my SP compiled fine didn’t mean it was actually going to work. Just using “NEWID” wasn’t enough. Instead, I had to modify my logging function to use a variable of type UNIQUEIDENTIFIER, like so:

DECLARE @NewId UNIQUEIDENTIFIER
SET @NewId = NEWID()
EXEC LogError 'An invalid response was returned.', @NewId
Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: