|
| Append records from other table |
 |
Mon, 03 Mar 2008 10:52:41 -050 |
let's say, I have 2 tables with exactlly same structure.
table1 is the main table which has 100 records in it.
table2 is holding 5 records.
is it possible to write a SQL to perform this function :
append table2 to table1
if the reocord alread exist in table1 ,update it ( prevent duplicated records in
table1 )
|
| Post Reply
|
| Re: Append records from other table |
 |
Mon, 03 Mar 2008 14:53:54 -050 |
Roland Wingerter Wrote:
> "eric wu" wrote
> > let's say, I have 2 tables with exactlly same structure.
> > table1 is the main table which has 100 records in it.
> > table2 is holding 5 records.
> > is it possible to write a SQL to perform this function :
> >
> > append table2 to table1
> > if the reocord alread exist in table1 ,update it ( prevent duplicated
> > records in table1 )
> --------
> OLH UpdateSet Class, method appendUpdate().
>
> Roland
>
>
sorry, I mean the SQL statement , like
sql = "update collection set worth = 900 where item = 'STRING'"
|
| Post Reply
|
| Re: Append records from other table |
 |
Mon, 3 Mar 2008 18:08:49 +0100 |
"eric wu" wrote
> let's say, I have 2 tables with exactlly same structure.
> table1 is the main table which has 100 records in it.
> table2 is holding 5 records.
> is it possible to write a SQL to perform this function :
>
> append table2 to table1
> if the reocord alread exist in table1 ,update it ( prevent duplicated
> records in table1 )
--------
OLH UpdateSet Class, method appendUpdate().
Roland
|
| Post Reply
|
| Re: Append records from other table |
 |
Mon, 3 Mar 2008 22:06:15 -0600 |
thank you
but I have many columns in the table
SET t1.amount = t1.amount + ; ---> only work on a column
"amount"
INSERT INTO Table1 (CustID, Amount) ;
---> only work on 2 colmuns CustID and Amount
do I have to write all column names in the statement ?
"Roland Wingerter" <ich@daheim.de> wrote in message
news:oMkiv0XfIHA.1880@news-server...
> "eric wu" wrote > Roland Wingerter Wrote:
>>
>>> "eric wu" wrote
>>> > let's say, I have 2 tables with exactlly same structure.
>>> > table1 is the main table which has 100 records in it.
>>> > table2 is holding 5 records.
>>> > is it possible to write a SQL to perform this function :
>>> >
>>> > append table2 to table1
>>> > if the reocord alread exist in table1 ,update it ( prevent
duplicated
>>> > records in table1 )
>>> --------
>>> OLH UpdateSet Class, method appendUpdate().
>>>
>> sorry, I mean the SQL statement , like sql = "update collection
set worth
>> = 900 where item = 'STRING'"
> -----------
> You would have to do it in two steps. Here is a sample.
>
> Suppose your tables have the fields CustID, Amount.
> // 1. Update existing rows in Table1
> UPDATE Table1 T1 ;
> SET t1.amount = t1.amount + ;
> (SELECT t2.amount FROM Table2 t2 WHERE t1.custID = t2.custID)
>
> // 2. Append new rows to Table1
> INSERT INTO Table1 (CustID, Amount) ;
> SELECT CustID, Amount ;
> FROM Table2 ;
> WHERE CustID NOT IN (SELECT CustID FROM Table1)
>
> Hope this helps
>
> Roalnd
>
>
>
|
| Post Reply
|
| Re: Append records from other table |
 |
Mon, 3 Mar 2008 23:03:38 +0100 |
"eric wu" wrote > Roland Wingerter Wrote:
>
>> "eric wu" wrote
>> > let's say, I have 2 tables with exactlly same structure.
>> > table1 is the main table which has 100 records in it.
>> > table2 is holding 5 records.
>> > is it possible to write a SQL to perform this function :
>> >
>> > append table2 to table1
>> > if the reocord alread exist in table1 ,update it ( prevent
duplicated
>> > records in table1 )
>> --------
>> OLH UpdateSet Class, method appendUpdate().
>>
> sorry, I mean the SQL statement , like
>
> sql = "update collection set worth = 900 where item = 'STRING'"
-----------
You would have to do it in two steps. Here is a sample.
Suppose your tables have the fields CustID, Amount.
// 1. Update existing rows in Table1
UPDATE Table1 T1 ;
SET t1.amount = t1.amount + ;
(SELECT t2.amount FROM Table2 t2 WHERE t1.custID = t2.custID)
// 2. Append new rows to Table1
INSERT INTO Table1 (CustID, Amount) ;
SELECT CustID, Amount ;
FROM Table2 ;
WHERE CustID NOT IN (SELECT CustID FROM Table1)
Hope this helps
Roalnd
|
| Post Reply
|
|
|
|
|
|
|
|
|
|