We help IT Professionals succeed at work.

SQL Query - is this correct?

SmashAndGrab
SmashAndGrab asked
on
159 Views
Last Modified: 2017-05-31
Hi,

I have the following SQL Query:


INSERT INTO Archive_E2LTORI SELECT * FROM E2LTORI WHERE LGNUM = '" & prRS("LGNUM") & "' AND TANUM = '" & prRS("TANUM") & "' AND VBELN ='" & Delivery & "' AND VBELN NOT IN(SELECT VBELN from Archive_E2LTORI where VBELN= '" & Delivery & "'

It basically copys data from the LIVE table to the arichive table.

The problem is that I kept getting a a primary key issue - so I then added the "NOT IN" so that the query makes sure that the combo exists before inserting.


My question is:

Is this correct?   Or should I add the addtional fields the the NOT IN part?
Comment
Watch Question

Vitor MontalvãoIT Engineer
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
When archiving data you must assure that the data you're going to archive is not archived yet.
The NOT IN clause is just one of the many solutions that you have to achieve this.

Author

Commented:
I think the query should be ..


INSERT INTO Archive_E2LTORI SELECT * FROM E2LTORI WHERE LGNUM = '" & prRS("LGNUM") & "' AND TANUM = '" & prRS("TANUM") & "' AND VBELN ='" & Delivery & "' AND VBELN NOT IN(SELECT VBELN from Archive_E2LTORI LGNUM = '" & prRS("LGNUM") & "' AND TANUM = '" & prRS("TANUM") & "' AND VBELN ='" & Delivery & "')

I think?
IT Engineer
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Chris LuttrellSenior Database Architect
CERTIFIED EXPERT

Commented:
What is your primary key in the Archive_E2LTORI table?
What do you want to do if the record already exists, nothing, just do not insert again?  What if other columns have changed?
Select * is bad practice for production code, and not specifying the columns in the insert.  This will break if someone modifies either table on you.
Vitor MontalvãoIT Engineer
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
SmashAndGrab, a feedback will be appreciated.

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