views:

230

answers:

2

Is there anyway to import data into SSIS where I expect the PKs to be sequential guids?

The only method I've been able to come up with so far is to create a temporary table with the column defaulting to newsequentialid() loading the data in there then copying it to the correct table. This isn't very elegant and is somewhat time consuming of having to add a bunch of extra layers in my packages to accommodate this.

A: 

I don;t use GUIDs as PKs but can't you set newsequentialid() as the default value, then any insert to your datbase will use that.

HLGEM
The issue is for foreign key relationships, if i can't generate the keys in SSIS I need to use a temporary table so I can read from it in SSIS so I can have the key defined to set foreign key relationships
Chris Marisic
I went with this answer because this is exactly what I'm doing currently.
Chris Marisic
+1  A: 

Have you looked at the OUTPUT clause?

Basically, you output anything that was inserted (including identity fields) into a table variable or a temp table. You can't insert it directly into another table if the other table has a foreign key relationship. Whenever I have used it, I used a temp table and then inserted from there into the child table.

Here is an example of its use:

DECLARE @roles TABLE (
    SecurityRoleId int
)

INSERT dbo.SecurityRole (
    SecurityRoleName,
    SecurityRoleDescription,
    LastUpdatedGuid
)
OUTPUT
    INSERTED.SecurityRoleId
INTO @roles
SELECT
    SecurityRoleName,
    SecurityRoleDescription,
    NEWID()
FROM @SecurityRole


INSERT INTO dbo.SecurityRoleDtl (
    SecurityRoleId,
    MemberSecurityRoleId
)
SELECT
    1,
    SecurityRoleId
FROM @roles
Gabriel McAdams
This just trades the work of writing the sql queries to create/drop the temp tables with writing custom sprocs to do the lifting which would actually be more work IMO.
Chris Marisic
You don't have to write sprocs to do this. You can even use temp tables if you want instead of table variables. What this gives you is a cleaner way to do the same thing you're doing now.
Gabriel McAdams
I disagree that this is any cleaner I actually feel this is substantially more work because having to write all the sql for it. Where getting the create statement of my table changing it so it has Temp in the name and removing all constraints on it and using SSIS to pump data in and out of it requires alot less work than this since you need to write these sql statements for every single instance this would occur then. But it all goes back to it's stupid that SSIS doesn't have a convention to use new sequentialid() as something you can put on as a derived column.
Chris Marisic

related questions