开发者

Insert unique identifier foreign key into table in SQL Server

I’m attempting to insert dummy search data using statements like this (the values differ for each one):

insert into SEARCHRESULTEXTENSIONBASE
    (CAL_ISBN, CAL_SEARCHRESULTID, USAGEDATAIMPORTID)
    VALUES('9780347011266', '1317168818', '925e3a5e-e971-11e0-9425-00219b71cde3')

But I’m getting this error:

[SQL Server] Conversion failed when converting from a character string
             to uniqueidentifier.

The field USAGEDATAIMORTID is a uniqueidentifier field, and I'm using similar statements in another table which successfully accepts the data:

INSERT INTO IMPORTUNIVERSITY
    (USAGEDATAIMPORTID, PAGENUMBERS, PUBLISHERNAME, RECORDID, DEPARTMENTNAME) 
    VALUES('925e3a5e-e971-11e0-9425-00219b71cde3', '2',
            'Nationwide News Pty Limited',
             '74', 'Business');

The only diff开发者_开发百科erence that I can see is the SEARCHRESULTEXTENSIONBASE table USAGEDATAIMPORTID is a foreign key to the IMPORTUNIVERSITY table.

Any ideas?

EDIT:

insert into SEARCHRESULTEXTENSIONBASE
            (CAL_ISBN, CAL_SEARCHRESULTID, USAGEDATAIMPORTID)
            VALUES('06463322521', '1317173481', convert(uniqueidentifier,'925e3a5e-e971-11e0-9425-00219b71cde3'))

gives me the same error.


Try this:

insert into SEARCHRESULTEXTENSIONBASE             
(CAL_ISBN, CAL_SEARCHRESULTID, USAGEDATAIMPORTID)             
VALUES('9780347011266', '1317168818', 
convert(uniqueidentifier, 'b6c51461-e966-11e0-b87a-0219b71cde3'))

You need to convert your string to the uniqueidentifier datatype.


You have one less digit in the GUID you are using. Could be a copy paste error.

b6c51461-e966-11e0-b87a-0219b71cde3 - has 31 digits
F567D3BC-45E9-4891-A0FB-BDE143236867 - should have 32 digits (correct)

You can test it as

SELECT Cast('b6c51461-e966-11e0-b87a-0219b71cde3' as UniqueIdentifier)

throws

Conversion failed when converting from a character string to uniqueidentifier.

while

SELECT Cast('F567D3BC-45E9-4891-A0FB-BDE143236867' as UniqueIdentifier)

works fine.


Looks like CAL_ISBN or CAL_SEARCHRESULTID is uniqueidentifier and you are trying to insert a character string instead. Could you please provide schema definition of the table you are inserting to and getting a error.


The problem was caused by there being no matching UUIDs in the two tables, thus the foreign key relationship failed. Would've been easier to work out if the error message wasn't so misleading.

Thank you for your help!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜