views:

58

answers:

1

Hi

I heard about doing staging tables and I am wondering how you actually do it.

I want to do a SqlBulkCopy into a staging table. Then then update the real table.

I heard in SQL Server 2008 that you can use something called merge but I am using SQL Server 2005 so I heard I have to use a update inner join?

I am not sure how that would look like(I am guessing best way would be to write it in SP). Also the staging table needs to be able to handle concurrent updates.

I was thinking of putting another column in the staged as GUID so that way I know which records belong to which group and when it is time to delete the records from the staged table I can just use that.

So can anyone show me any examples or tutorials on how to do it? Also how fast is this way? Say if your updating 50,000 records how long would that take(a guesstimate is fine)

Edit

So this is my SP now. I think it should be able to do concurrent connections but I wanted to make sure.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_MassUpdate]
@BatchNumber uniqueidentifier 
AS
BEGIN
    update Product
    set ProductQty = 50
    from Product prod
    join StagingTbl stage on prod.ProductId = stage.ProductId
    where stage.BatchNumber = @BatchNumber

    DELETE FROM StagingTbl
    WHERE BatchNumber = @BatchNumber

END
+1  A: 

You create a staging table the same way you create a regular table. With a CREATE TABLE script that defines the columns and datatypes.

OK

update prod
set field1 = stage.field1
from myproductiontable prod
join mystagingtable stage on prod.somefield = stage.somefield

To add records not in prod

Insert myproductiontable (f1, f2, f3)
select stage.f1, stage.f2, stage.f3 
from mystagingtable stage
where  not exists (select someid from myproductiontable prod WHERE stage.somefield = prod.somefield)

But you have to have one or more fields in the staging table that relate to the records in the prod table. Sometimes you have to do an additional join to get that relationship.

HLGEM
Sorry I should have mentioned I know what a staging table is and that it is the same as a regular table(in the way it is created). I don't understand how to do a inner join update on mass records and how to deal with concurrent updates.
chobo2
Hi. I got a couple questions. First is what happens if your PK is a incrementing int. How would you match the staging table to main table? Second does what you shown deal with concurrent insets? Like what say if I am doing an update and then someone else does a batch insert at that time into the staging table. How can I ensure that when I go to mass delete the ones in the staging table that they where already updated in the main table?
chobo2
YOu have to have another field or fields to join on that are unique. For instance, you might join on a combination of several fields that makes a unique id. If you don't have that, then you are in trouble, there has to be something to match on.I do not let anyone else insert into my staging tables, just the import process so there are no concurrent issues.
HLGEM
Well I might run into that situation of concurrent issues. So would you know of any way around it?
chobo2
Well in taht case, I would put each insert to the staging table in a batch with an assigned batchnumber. Then you only update things in your current batch number and a differnt process running would havea differnt batchnumber. Be sure to make the batch number assignment process such that it is all in one transaction to avoid the prblem of imports running into each other.
HLGEM
Ya I was thinking of using a grouping with a unique identifier. But I am not sure how to update things in the current batch. Is it just a where clause? I am not sure what you mean all in one transaction though.
chobo2
See my Edit....
chobo2