[PL/SQL] copies qualified records from one table to another.

Source: Internet
Author: User
I. Basic statements
Insert into tablename fieldname select fieldname from tablename;
Ii. Use cursor and fetch to copy qualified records from one table to another
In the evening, I had my own time. I used this time to study oracle. I forgot about what I learned last year, now, we can pick it up before we forget it.
Today I read about anonymous blocks, functions, processes, and so on. I remember that I had used cursor and fetch, and I almost forgot about it, today, we will try to copy the data in one table to another table with the same structure. However, one condition is that the ID of the original table must be a multiple of 5 to be inserted. Otherwise, the table is not inserted.
Before starting, let's review something:
1. The format of anonymous blocks is shown below.
[Declare]
/* Variable definition area, which can also be used for initial activation */
Begin
/* In the Start area, execute the sequential SQL statement */
[Exception]
/* In the exception handling area, exception handling can be performed. The raise keyword is used for exception throw */
End;
The box is optional and not required.
2. Fetch
1) first define the cursor, for example, cursor id_cur is select ID from components.
2) then open cursor: Open id_cur
3) The third is to use fetch to extract the cursor and save it to the variable.
4) Disable cursor
The Code is as follows:

Declare
Num_id integer;/* define variable */
SQL _str varchar (1000 );
Cursor id_cur is select ID from components;/* use the cursor to get the ID */
Begin
Open id_cur;/* Open the cursor for execution */
Loop/* keep running */
Fetch id_cur into num_id;/* According to the cursor, extract the ID number to the variable */
Exit when id_cur % notfound;/* exit when no record exists */
If Mod (num_id, 5) = 0 then/* If the record ID is a multiple of 5, execute the following insert */
/* Generate an SQL statement */
SQL _str: = 'insert into components2 select * from components where id = '| num_id;
/* Execute the generated SQL statement immediately */
Execute immediate SQL _str;
End if;/* Remember: The end bundle must be added after the condition ends ';'*/
End loop;
Close id_cur;/* close the cursor */
End;
/

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1697686


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.