Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to write one query .
for this i decided to use cursor.

following are my criteria for this

first- i have to select some records from one table it should be n number of records
e.g select col1,col2 from table1

second -i have to select maxid from another table
e.g select max(id) from table2

third - i have to insert one by one records from table1 to another table3 using max(id) from table2 for each records which i have to insert in table3

e.g insert into table3(id,cl1,cl2,cl3,cl4) values (max(id),xyz,xyz,xyz)
insert into table4(id,cl5,cl6,cl7,cl8) values (max(id),abc,abc,abc)


please help me on this
what should i use cursor or any other
Posted
Comments
Sudhakar Shinde 30-Jul-13 4:08am    
You can use cursor for this functionality.

1 solution

Based on your inputs your code will be like as mentioned below..

SQL
BEGIN
   // Add appropriate WHERE Condition
   SELECT MAX(id) INTO maxId FROM table2;

   // Add appropriate WHERE Condition in SELECT statement mentioned below
   FOR i IN (SELECT col1,col2 FROM table1)
   LOOP
        INSERT INTO table3 VALUES (maxId, i.col1, i.col2);
   END LOOP;
END;


Please note , You need to do exception handling separately.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900