Problem converting integer to uniqueidentifier

Posted on 2008-11-08
Last Modified: 2013-11-10
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
Question by:ajayanandv
    LVL 142

    Accepted Solution

    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.
    LVL 17

    Expert Comment

    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.
    LVL 26

    Expert Comment

    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

    Author Comment

    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?
    LVL 17

    Expert Comment

    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:

    LVL 26

    Expert Comment

    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..
    LVL 17

    Expert Comment

    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

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
    For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
    Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now