Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error when insert hard code uniquedentifier

Status
Not open for further replies.

vyline

Programmer
Jan 18, 2012
6
US
Hi,

I need insert a hard code uniqueidentifier into a table, data type is uniqueidentifier. The value was got from another table and data type is uniqueidentifier. But when I execute the script, I got error:
"Conversion failed when converting from a character string to uniqueidentifier." the following is the script. I have tried CONVERT() and got the same error. Any idea, how should I do it?

Insert into table1(f1,f2,f3,f4,f5...f10)
select 21,'apple',CAST('221651A5-36A5-85EB-5A3B-BBC465928673' AS uniqueidentifier),...f10
from table2
where table2.status ='N'

Thank you so much for any helps and ideas!!!
 
That should work just fine. The thing you need to be careful about is the column order.

Specifically the order in which you list the columns in the Insert part:

[tt]Insert into table1([!]f1,f2,f3,f4,f5...f10[/!])[/tt]

Must match the order of the columns in the select part:

[tt] select [!]21,'apple',CAST('221651A5-36A5-85EB-5A3B-BBC465928673' AS uniqueidentifier),...f10[/!][/tt]

In your example, the 3rd column appears to be the unique identifier, so column 3 in the insert part must be a unique identifier.

Bottom line... column order is important with this query.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
gmmastros,

Thank you again!!
you are right. I have 4 fields which are all uniqueidentifier. I only cast the hard code one. Will check all and test.
Thanks a lot.
 
You're welcome.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top