Solved

cursor update + insert

Posted on 2013-01-29
6
369 Views
Last Modified: 2013-01-29
hello guys,

I have a question, I have two tables with exactly the same settings example:


table1
NAME, AGE, DATE, IDENTIFIER, PHONE

table2
NAME, AGE, DATE, IDENTIFIER, PHONE

Table 1 is updated every day and I need to mount a cursor to read the record and compare to table2 record if they match make an update in table2 with data from table1.

if you have data in table1 in table2 that do not have an insert in table2 as I assemble this structure?
0
Comment
Question by:eduardo12fox
  • 4
6 Comments
 
LVL 14

Assisted Solution

by:Emes
Emes earned 250 total points
ID: 38831820
Try to use this

insert into table2
select NAME, AGE, DATE, IDENTIFIER, PHONE
FROM   table1
WHERE  NOT EXISTS
  (SELECT NAME, AGE, DATE, IDENTIFIER, PHONE
   FROM   table2
   WHERE  table2.NAME = table1.Name
and table2.Age = table1.age

and table2.date = table1.date
and table2.IDENTIFIER = table1.IDENTIFIER
and table2.PHONE = table1.Phone)
0
 
LVL 1

Accepted Solution

by:
DoutorApedeuta earned 250 total points
ID: 38831852
Hi,

Do you really need that cursor. I think you could easily solve the problem using this sintax:

insert into t1(a, b, c)
    select d, e, f from t2
    on duplicate key update b = e, c = f;

This, of course, assuming that there are unique keys in the tables.

Check this article for more info.
0
 

Author Comment

by:eduardo12fox
ID: 38831861
Fantastico! The insert was perfect but I can not implement UPDATE
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:eduardo12fox
ID: 38831869
Ok ok but to insert and find lines like how when I update the same line without the insert?
0
 

Author Comment

by:eduardo12fox
ID: 38831888
OK! Fantastico was correct and helped me a lot. I want to thank everyone's attention. Thank you!!
0
 

Author Closing Comment

by:eduardo12fox
ID: 38831892
OK! Fantastico was correct and helped me a lot. I want to thank everyone's attention. Thank you!!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question