Wednesday, March 28, 2012

Is it possible to make INSERT/UPDATE operation in SSIS?

Hi!
I use SSIS to insert some data from text sources to SQL server 2005. I use
check constraints option. Is it possible if iserted record has the same
primary key as existing record in table to replace existing record? How to
make it?
Thank you
Igor A. ChechetIgor
BEGIN TRANSACTION
IF EXISTS (SELECT * FROM Table WHERE id=@.id)
BEGIN
UPDATE Table SET col=...,col2...c,ol3=... WHERE id=@.id
END
ELSE
BEGIN
INSERT INTO Table (cols here) VALUES (here)
END
COMMIT TRANSACTION
"Igor A. Chechet" <ichechet@.mail.ru> wrote in message
news:uVHJyuxpGHA.1440@.TK2MSFTNGP03.phx.gbl...
> Hi!
> I use SSIS to insert some data from text sources to SQL server 2005. I use
> check constraints option. Is it possible if iserted record has the same
> primary key as existing record in table to replace existing record? How to
> make it?
> Thank you
> Igor A. Chechet
>|||Igor,
First better to import to a staging table all the records .
You can write a query which checks the existence of a record on Primary
Key
UPDATE TABLE SET COL1= STAGING.A1,
COL2 = STAGING.COL2
...
...
FROM TABLE , STAGING
WHERE TABLE.PK - STAGING.PK
INSERT INTO TABLE
SELECT * FROM STAGING A
WHERE NOT EXISTS ( SELECT 1 FROM TABLE B WHERE B.PK =A.PK)
Note: PK is PRIMARY KEY
M A Srinivas
Igor A. Chechet wrote:
> Hi!
> I use SSIS to insert some data from text sources to SQL server 2005. I use
> check constraints option. Is it possible if iserted record has the same
> primary key as existing record in table to replace existing record? How to
> make it?
> Thank you
> Igor A. Chechet|||There's no need to drop to an intermediary table. You can do this in the
pipeline.
Here's how: http://www.sqlis.com/default.aspx?311
Regards
Jamie Thomson
An SSIS blog - http://blogs.conchango.com/jamiethomson/
<masri999@.gmail.com> wrote in message
news:1152880213.339951.42940@.i42g2000cwa.googlegroups.com...
> Igor,
> First better to import to a staging table all the records .
> You can write a query which checks the existence of a record on Primary
> Key
> UPDATE TABLE SET COL1= STAGING.A1,
> COL2 = STAGING.COL2
> ...
> ...
> FROM TABLE , STAGING
> WHERE TABLE.PK - STAGING.PK
> INSERT INTO TABLE
> SELECT * FROM STAGING A
> WHERE NOT EXISTS ( SELECT 1 FROM TABLE B WHERE B.PK =A.PK)
> Note: PK is PRIMARY KEY
> M A Srinivas
>
>
> Igor A. Chechet wrote:
>> Hi!
>> I use SSIS to insert some data from text sources to SQL server 2005. I
>> use
>> check constraints option. Is it possible if iserted record has the same
>> primary key as existing record in table to replace existing record? How
>> to
>> make it?
>> Thank you
>> Igor A. Chechet
>

No comments:

Post a Comment