SQL Select

30 pts.
Tags:
Application development
Does anyone know of a way to use SQL (embedded) to select only the 1st occurence of duplicate key records in a file. I have a file where there may be duplicate keys and I need the 1st occurrence of the key as the data following the key is the data that is needed.
ASKED: January 17, 2006  8:41 AM
UPDATED: January 17, 2006  11:41 AM

Answer Wiki

Thanks. We'll let you know when a new response is added.

There are several ways to do this. If you just want to read the record, put FOR 1 RECORD ONLY at the end of the SELECT in DB2. In Oracle, use a WHERE clause of ROWNUM<2. You must use <2 and not =1. If you want to be able to modify the first record, a cursor can do it for you. This example is in DB2 SQL:

DECLARE myCursor CURSOR FOR
SELECT stuff
FROM table
WHERE conditions;

OPEN myCursor;

FETCH myCursor INTO :stuff;

UPDATE table
SET field = expression
WHERE CURRENT OF myCursor;

CLOSE myCursor;

Sheldon Linker (sol@linker.com)
Linker Systems, Inc. (www.linkersystems.com)
800-315-1174 (+1-949-552-1904)

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following