Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Insert/Update data in a table by using SELECT

Author  Topic 

tejo_pan
Starting Member

10 Posts

Posted - 2005-07-13 : 13:11:58
Dears,

I've a query that return 5000 rows.
I'm insert this results in other table with this command:

SET DATEFORMAT dmy

INSERT INTO tbOther
SELECT
Name,
DateAdd
FROM tbCustomers
WHERE
(DateAdd BETWEEN
CONVERT(CHAR(10), DATEADD(mm, -1, GETDATE()), 103)
AND CONVERT(CHAR(10), GETDATE(), 103))


This query is executed all days, but have one problem...
After 1 year, in my table tbOther, the customers that IS in table tbOther will be inserted AGAIN in this table, but I don't want that this happens.. I want UPDATE the customers that already exists in table tbOther and INSERT the new customers in table tbOther too...

Ex.:

[code]
Data of the table tbOther:
==========================
| NAME | DATE |
|---------|--------------|
| Sergio | 30/07/1981 | ****
| Pedro | 10/12/1980 |
|---------|--------------|

Result of my SELECT:
==========================
| NAME | DATE |
|---------|--------------|
| Sergio | 30/07/1981 | ****
| Carlos | 20/01/1980 |
| John | 05/05/1982 |
|---------|--------------|

My table tbOther AFTER the SELECT (UPDATE)
==========================
| NAME | DATE |
|---------|--------------|
| Sergio | 30/07/1981 | ****
| Pedro | 10/12/1980 |
| Carlos | 20/01/1980 |
| John | 05/05/1982 |
|---------|--------------|

In this case, the Sergio already exists in table 1, so, only need UPDATE him data..

The Carlos and John, that exists in result of my query, I want INSERT in my table tbOther, understand??

Thank you so much!!!!

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-07-13 : 13:17:20
run the update and if @@rowcount immediatly after it is 0 then insert new record.

Go with the flow & have fun! Else fight the flow
Go to Top of Page
   

- Advertisement -