We help IT Professionals succeed at work.

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

Eduardo Fuerte
on
54 Views
Last Modified: 2018-12-20
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.
Comment
Watch Question

Pratik SomaiyaApplication Development Analyst
CERTIFIED EXPERT

Commented:
You can try:

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

Open in new window

Pratik SomaiyaApplication Development Analyst
CERTIFIED EXPERT

Commented:
You can run a stored procedure as well to do this operation for you.
Eduardo FuerteDeveloper and Analyst

Author

Commented:
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?
Application Development Analyst
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Just one thing.

The line to be inserted on Table2 must to be just the last line inserted on Table.
Pratik SomaiyaApplication Development Analyst
CERTIFIED EXPERT

Commented:
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?
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Yes, just the last record...
Pratik SomaiyaApplication Development Analyst
CERTIFIED EXPERT

Commented:
Then you can definitely filter the last record by the date column.
Pratik SomaiyaApplication Development Analyst
CERTIFIED EXPERT

Commented:
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 HornSQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015

Commented:
(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.
Eduardo FuerteDeveloper and Analyst

Author

Commented:
@Pratik Somaiya answer is pratically running well.... testing...
Eduardo FuerteDeveloper and Analyst

Author

Commented:
Hi @Pratik Somaiya

The job is done, thank you very much!

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.