Link to home
Start Free TrialLog in
Avatar of SmashAndGrab
SmashAndGrab

asked on

SQL Query - is this correct?

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?
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

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.
Avatar of SmashAndGrab
SmashAndGrab

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland 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
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.
SmashAndGrab, a feedback will be appreciated.