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:

img001
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.
Microsoft SQL Server

Avatar of undefined
Last Comment
Eduardo Fuerte

8/22/2022 - Mon
Pratik Somaiya

You can try:

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

Open in new window

Pratik Somaiya

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

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?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Pratik Somaiya

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Eduardo Fuerte

Just one thing.

The line to be inserted on Table2 must to be just the last line inserted on Table.
Pratik Somaiya

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?
ASKER
Eduardo Fuerte

Yes, just the last record...
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Pratik Somaiya

Then you can definitely filter the last record by the date column.
Pratik Somaiya

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

Jim Horn

(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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER
Eduardo Fuerte

@Pratik Somaiya answer is pratically running well.... testing...
ASKER
Eduardo Fuerte

Hi @Pratik Somaiya

The job is done, thank you very much!

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