Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / Programming / SQL / August 2008

Tip: Looking for answers? Try searching our database.

Syntax error converting from a character string to uniqueidentifie

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Garyon - 30 Aug 2008 20:04 GMT
I am getting this error when attempting to insert a uniqueidentifier into a
table through a Stored Procedure & even direct SQL in Query Analyzer. I have
searched and tried numerous alternatives ALL with no success ...

Perhaps there is a Database setting that I am missing?

Kinda desperate and don't know what else to try,  so any assistance would be
greatly appreciated!

Thank You,
Gary

------------------------------------------
Alternative #1: Direct Assignment

PROCEDURE dbo.Vendor_UpdateVendorInfo(
@vendorId UniqueIdentifier,
@vendorName as varchar(255),
@vendorPhone as varcahr(255)

)
AS
insert vendor_info (vendor_id, vendor_name, vendor_phone) VALUES(@vendorId,
@vendorName, @vendorPhone)

------------------------------------------
Alternative #2 : The vendor Id is passed in as a uniqueidentifier. We
retrieve the vendor id form the vendor table again, ensuring that it IS a
valid data type and format.

PROCEDURE dbo.Vendor_UpdateVendorInfo(
@vendorId UniqueIdentifier,
@vendorName as varchar(255),
@vendorPhone as varcahr(255)

)
AS
DECLARE @guidVendorId as UniqueIdentifier
SELECT @guidVendorId = vendor_id FROM vendor WHERE vendor_id = @vendorId

insert vendor_info (vendor_id, vendor_name, vendor_phone)
VALUES(@guidVendorId, @vendorName, @vendorPhone)

---------------------------------------------
Alternative #3 : Pass the vendorId in as a string and CAST it as a
uniqueidentifier

PROCEDURE dbo.Vendor_UpdateVendorInfo(
@vendorId as varchar(50),
@vendorName as varchar(255),
@vendorPhone as varcahr(255)

)
AS
DECLARE @guidVendorId as UniqueIdentifier
SET @guidVendorId = CAST(@vendorId as UniqueIdentifier)

insert vendor_info (vendor_id, vendor_name, vendor_phone)
VALUES(@guidVendorId, @vendorName, @vendorPhone)
Plamen Ratchev - 30 Aug 2008 21:39 GMT
What value do you pass for the @vendorId parameter? Is it a valid
UNIQUEIDENTIFIER?

You can easily test:

DECLARE @vendorid UNIQUEIDENTIFIER;

SET @vendorid = NEWID();

EXEC Vendor_UpdateVendorInfo @vendorid, 'foo', '123-234-4345';

Signature

Plamen Ratchev
http://www.SQLStudio.com

Garyon - 30 Aug 2008 22:14 GMT
DEFINITELY valid. It was actually generated by SQL Server. If you look at
Alternative #2, you can see that even though I can successfully retrieve a
record, using the GUID, and then use the GUID returned by the query, I am
STILL getting the error.

I appreciate very much your attention to this post. It is MOST baffling. I
am sure it will turn out to be some ridiculous oversight, but my GOd I can't
figure it out to save my life!

Gary

> What value do you pass for the @vendorId parameter? Is it a valid
> UNIQUEIDENTIFIER?
[quoted text clipped - 6 lines]
>
> EXEC Vendor_UpdateVendorInfo @vendorid, 'foo', '123-234-4345';
Plamen Ratchev - 31 Aug 2008 03:21 GMT
If you rule out the stored procedure parameter, then Erland is correct
and you should look at triggers on the table. As he noted posting the
table structure and the complete error message will help too.

Signature

Plamen Ratchev
http://www.SQLStudio.com

Erland Sommarskog - 30 Aug 2008 23:21 GMT
> I am getting this error when attempting to insert a uniqueidentifier
> into a table through a Stored Procedure & even direct SQL in Query
> Analyzer. I have searched and tried numerous alternatives ALL with no
> success ...
>
> Perhaps there is a Database setting that I am missing?

First check for triggers on the table. Maybe something bad is happening
there.

If that does not reveal anything, could you post the CREATE TABLE
statement for the vendor_info table? Could you also post how you call
these procedures? And, finally, the full error message you get?

Signature

Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

Garyon - 31 Aug 2008 05:50 GMT
Here is the requested information ... It's actually for a vendor logon
feature that I am working on ...

TABLE
-------------------------------------------
CREATE TABLE [dbo].[vendor_logon] (
    [logon_id]  uniqueidentifier ROWGUIDCOL  NOT NULL ,
    [vendor_id] [uniqueidentifier] NOT NULL ,
    [logon_uid] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
    [logon_pwd] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
END

GO

ALTER TABLE [dbo].[vendor_logon] WITH NOCHECK ADD
    CONSTRAINT [PK_vendor_logon] PRIMARY KEY  CLUSTERED
    (
        [logon_id]
    )  ON [PRIMARY]
GO

ALTER TABLE [dbo].[vendor_logon] WITH NOCHECK ADD
    CONSTRAINT [DF_vendor_logon_logon_id] DEFAULT (newid()) FOR [logon_id]
GO

EXAMPLE OF CODE WHICH FAILS - TYPED DIRECTLY IN QUERY ANALYZER
This is an example of Alternative #2
----------------------------------------------------
DECLARE    @vendorId varchar(50)
DECLARE    @logonUID varchar(255)
DECLARE    @logonPWD varchar(255)

SET @vendorId    = '9b436d61-1262-4ec5-a48e-bcee886846f1'
SET @logonUID    = 'My Name'
SET @logonPWD    = 'My Word'
DECLARE @guidVendorId as UniqueIdentifier
SELECT @guidVendorId = vendor_id FROM vendor WHERE vendor_id = @vendorId
insert vendor_logon (vendor_id, logon_id, logon_pwd) VALUES(@guidVendorId,
@logonUID, @logonPWD)

COMPLETE ERROR MESSAGE FROM QUERY ANALYZER
--------------------------------------
Server: Msg 8169, Level 16, State 2, Line 10
Syntax error converting from a character string to uniqueidentifier.

Again ... Thank you for your help. :-)
Gary

> > I am getting this error when attempting to insert a uniqueidentifier
> > into a table through a Stored Procedure & even direct SQL in Query
[quoted text clipped - 9 lines]
> statement for the vendor_info table? Could you also post how you call
> these procedures? And, finally, the full error message you get?
Plamen Ratchev - 31 Aug 2008 06:17 GMT
You changed the code you posted initially. The code you just posted will
fail not because of @vendorid but because of @logonUID. You have the
INSERT column set to logon_id (the second column in your INSERT list)
which is declared as UNIQUEIDENTIFIER and you pass the parameters as:
SET @logonUID = 'My Name', which is not a valid UNIQUEIDENTIFIER. I
guess you just need to change to:

INSERT INTO vendor_logon (vendor_id, logon_uid, logon_pwd)
VALUES(@guidVendorId, @logonUID, @logonPWD);

Signature

Plamen Ratchev
http://www.SQLStudio.com

Garyon - 31 Aug 2008 15:34 GMT
OMG!! Yes ... that was it! I knew it would be something ridiculous. I suppose
I probably should work on my naming conventions!

Thank you VERY much!
Gary

> You changed the code you posted initially. The code you just posted will
> fail not because of @vendorid but because of @logonUID. You have the
[quoted text clipped - 5 lines]
> INSERT INTO vendor_logon (vendor_id, logon_uid, logon_pwd)
> VALUES(@guidVendorId, @logonUID, @logonPWD);
Elkestra - 31 Aug 2008 09:53 GMT
Second attempt at replying to this thread. Seems the newsgroup occasionally
eats posts.

Just a thought, and I haven't had a chance to test it myself yet this
morning, but shouldn't the Guid string be enclosed in { } ?

SET @vendorId = '{9b436d61-1262-4ec5-a48e-bcee886846f1}'

Yours,

Ann-Marie

> Here is the requested information ... It's actually for a vendor logon
> feature that I am working on ...
[quoted text clipped - 59 lines]
>> statement for the vendor_info table? Could you also post how you call
>> these procedures? And, finally, the full error message you get?
Erland Sommarskog - 31 Aug 2008 10:34 GMT
> Just a thought, and I haven't had a chance to test it myself yet this
> morning, but shouldn't the Guid string be enclosed in { } ?
>
> SET @vendorId = '{9b436d61-1262-4ec5-a48e-bcee886846f1}'

It's OK with or without braces.

Signature

Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

Elkestra - 31 Aug 2008 16:06 GMT
My apologies for posting such a silly thought then. I've got so used to
always bracketing Guids with {}, it just looked out of place without them.
I'll place safe and wriggle out of feelings of stupidity for posting it, by
admitting that when i first thought of it, I hadn't had my coffee yet.

Ann-Marie

>> Just a thought, and I haven't had a chance to test it myself yet this
>> morning, but shouldn't the Guid string be enclosed in { } ?
>>
>> SET @vendorId = '{9b436d61-1262-4ec5-a48e-bcee886846f1}'
>
> It's OK with or without braces.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.