Link to home
Start Free TrialLog in
Avatar of ajayanandv
ajayanandv

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
http://www.sqlteam.com/article/uniqueidentifier-vs-identity
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
Avatar of ajayanandv
ajayanandv

ASKER

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:
02C053C3-D1D3-4369-A235-7865E559FFC7
Here are some rows I inserted after this:

'02C053C3-D1D3-4369-A235-7865E0000001'
'02C053C3-D1D3-4369-A235-7865E0000002'
'02C053C3-D1D3-4369-A235-7865E0000003'
'02C053C3-D1D3-4369-A235-7865E0000004'
'02C053C3-D1D3-4369-A235-7865E0000005'
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