Live ORACLE

if you are Oracle Developer ,than this Blog most likely will make you happy.

About my Blog

This Blog particularized for Oracle Developers ... you will see interesting Codes for SQL , PL/SQL as well as new ideas for Developer Suite and Client Tools that will help you in your professional life ... and I hope I reach for your satisfaction.

About Me

I'm Hany Freedom , 25 years old , I live in EL-Minia City in Egypt , I'm Moderator at ArabOUG.org the best Arabic Forum for ORACLE on the Net. if you interested to know more about me .... just Click Here.

Using CURRVAL and NEXTVAL


CREATE TABLE employees_temp
AS
SELECT employee_id,
first_name,
last_name
FROM employees;


CREATE TABLE employees_temp2
AS
SELECT employee_id,
first_name,
last_name
FROM employees;



DECLARE

seq_value NUMBER;

BEGIN

-- Display initial value of NEXTVAL

-- This is invalid: seq_value := employees_seq.NEXTVAL;

SELECT employees_seq.nextval

INTO seq_value

FROM dual;



dbms_output.Put_line('Initial sequence value: '

||To_char(seq_value));



-- The NEXTVAL value is the same no matter what table you select from

-- You usually use NEXTVAL to create unique numbers when inserting data.

INSERT INTO employees_temp

VALUES (employees_seq.nextval,

'Lynette',

'Smith');



-- If you need to store the same value somewhere else, you use CURRVAL

INSERT INTO employees_temp2

VALUES (employees_seq.currval,

'Morgan',

'Smith');



-- Because NEXTVAL values might be referenced by different users and

-- applications, and some NEXTVAL values might not be stored in the

-- database, there might be gaps in the sequence

-- The following uses the stored value of the CURRVAL in seq_value to specify

-- the record to delete because CURRVAL (or NEXTVAL) cannot used in a WHERE clause

-- This is invalid: WHERE employee_id = employees_seq.CURRVAL;

SELECT employees_seq.currval

INTO seq_value

FROM dual;



DELETE FROM employees_temp2

WHERE employee_id = seq_value;



-- The following udpates the employee_id with NEXTVAL for the specified record

UPDATE employees_temp

SET employee_id = employees_seq.nextval

WHERE first_name = 'Lynette'

AND last_name = 'Smith';



-- Display end value of CURRVAL

SELECT employees_seq.currval

INTO seq_value

FROM dual;



dbms_output.Put_line('Ending sequence value: '

||To_char(seq_value));

END;

/



Share/Save/Bookmark

0 comments:

Post a Comment



Newer Posts Older Posts Home Page
 
http://www.dpriver.com/images/sqlpp-banner-2.png

Thanks for reading my Blog ... you Visitor Number :-