[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

Problem converting integer to uniqueidentifier

Hey people, I have Problem converting integer data type to uniqueidentifier in SQL Server 2005. I am using SSIS to copy data from one table to another. Source table has integer data type as the primary key and destination table has uniqueidentfier as primarykey. Is there any work around like mapping to another data type. Any alternate solutions. Please help
1 Solution
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you  cannot transform a int into uniqueidentifier.

you can generate uniqueidentifier value with NEWID() or NEWSEQUENTIALID() function in the query, ie set NEWID() as default for the column in the destination table, and leave it out from the transformation.
As already mentioned by angelIII you cannot convert an int to a GUID. If you want to maintain a relationship to the source table your best workaround is to add another column to the destination.
if you have detail tables relared to your master table with the id key and you want to transfer that data then you should better use a cursor to tarnsfer teh data. Copy one row to the destination.. get the new id that is produced while you were inserting then copy the detail values by changing the id with new value...
Hope this helps
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

ajayanandvAuthor Commented:
I want to do bulk copy from the source table and since the destination type is unique identifier, i am not able to copy the data. I have tried using temporary tables to do the mapping. It just doesnt work. Are you aware of any techniques to make it work?
One other workaround. Just concat values together to form your own GUID. If you take an existing value from the table and say, drop off the last 6 chars - you could substitute those with a padded version of your integer value. Of course you do take the risk of  failoure if inserting a duplicate in your primary key.
Example value from table:
Here are some rows I inserted after this:

if your destination tables id field is an Identity field then you shouldnt care about the id fields... If not you may change it to be and Identity field...
In your package write a select stament including all columns except the id column as your source...
In the destination table insert these columns to the coressponding fields but do not try to assing any value to the id field of the destination table...
The package will transfer the package and produce the new id values ad unique identifier..
Here is some  code i messed around with to create my own id's. Caution! :-)
declare @varid as varchar(40);
declare @newid as uniqueidentifier;
select @newid = newid()
select @newid
select @varid = @newid
select @varid = substring(@varid,1,24)
declare @myIntVal as integer
select @myIntVal = 287654
declare @myVarInt as varchar(8)
declare @myVarBld as varchar(16)
select @myVarBld = '00000000' + cast(@myIntVal as varchar(8))
select @myVarBld
select substring(@myVarBld,len(@myVarBld)-7,8)
select @myVarInt = substring(@myVarBld,len(@myVarBld)-7,8)
select @myVarInt
declare @myVarFun as varchar(12);
select @myVarFun = 'HOGG';
select @myVarFun = @myVarFun + @myVarInt
select @myVarFun
select @varid = @varid + @myVarFun
select @varid

Open in new window


Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now