Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

Coul you point a way to automatically repicate an insertion on a table to another table by using MS-SQLServer?

Hi Experts

Coul you point a way to automatically repicate an insertion on a table to another table by using MS-SQLServer?

Accordingly to:

User generated image
The table tb_individuo receives an insertion sent by an web-service.

Imediatelly after some columns values must to be replicated to the table assistido.

Could you suggest a way to do that?

Thanks in advance.
Avatar of Pratik Somaiya
Pratik Somaiya
Flag of India image

You can try:

INSERT INTO newTable (col1, col2, col3)
SELECT column1, column2, column3
FROM oldTable 

Open in new window

You can run a stored procedure as well to do this operation for you.
Avatar of Eduardo Fuerte

ASKER

Well.
The insertion must to be automatic, as soon the line is inserted at tb_individuo.
Maybe an insetion trigger could do the job better.
Any suggestion code on it?
ASKER CERTIFIED SOLUTION
Avatar of Pratik Somaiya
Pratik Somaiya
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just one thing.

The line to be inserted on Table2 must to be just the last line inserted on Table.
Then you can add a RowAddDate column in Table 1 and 2 and in trigger you can add a WHERE condition as:

WHERE Col4 = (SELECT MAX(RowAddDate) from Table1)

You can try this.

Do you want only 1 record insertion? or all the records from table 1 to table 2?
Yes, just the last record...
Then you can definitely filter the last record by the date column.
Or else, you can have an IDENTITY column named as RowId in your table and then use the following logic to get the last record:

SELECT TOP 1 * FROM Table1 ORDER BY RowId DESC

Open in new window

(Potentially silly answer)  If both tables are in the same database then create a view that selects all from the first table.  

That avoids all the concurrency issues you'll have with two tables where if rows change in one then you'd always have to pull off changing it in the other.
@Pratik Somaiya answer is pratically running well.... testing...
Hi @Pratik Somaiya

The job is done, thank you very much!

@Jim
I couldn't even try your suggestion... in a hurry...