?
Solved

Howto Q: Writing an Oracle Cursor in stead of select into

Posted on 2003-03-19
8
Medium Priority
?
1,068 Views
Last Modified: 2009-12-16
Hello *,
I am having an oracle table "Alfabet" with the columns A,B,D. The column D is a Long field. The table needs adjusted: the column C needs to be added. Because long fields needs to be at the end of the tabledefinition, a simple alter table won't do. I thought of creating a temp table, and Select into everything into the temptable, drop the original table, create the new table (with all A,B,C,D columns) and select everything back into  it. NOT! The select into doesn't work with long fields (ora 997 error).

Questions:

1) i've heard that a cursor can be used to select into the temp table and back (and ora997 won't show up) - is this true?

2) being unexperienced with cursor writing, can someone send me the code to do this?

3) Or is there some other way i can do the trick?

TIA!
0
Comment
Question by:wouter13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 22

Expert Comment

by:Helena Marková
ID: 8165520
Maybe someone will provide a better solution, but you can try to create new table with A,B,C columns -> add values to A and B columns -> add D column -> add values to D
... it is just a tip ...
0
 
LVL 1

Author Comment

by:wouter13
ID: 8165585
Henka, isn't this the same idea as i posted? AFAIK, you cannot rename tables in oracle. So, there needs to be a temporal table, where the data can be stored, the old table can be deleted and recreated, and the data needs to be put back into place. This can only be done by a cursor if the fieldtype Long is used - AFAIK.
0
 
LVL 1

Author Comment

by:wouter13
ID: 8165630
Henka, isn't this the same idea as i posted? AFAIK, you cannot rename tables in oracle. So, there needs to be a temporal table, where the data can be stored, the old table can be deleted and recreated, and the data needs to be put back into place. This can only be done by a cursor if the fieldtype Long is used - AFAIK.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 15

Accepted Solution

by:
andrewst earned 1000 total points
ID: 8166654
You can rename tables in Oracle, so you don't need to introduce a temporary table.

This simple PL/SQL solution will work so long as none of your LONG values exceeds 32767 characters:

RENAME alphabet TO old_alphabet

CREATE TABLE alphabet
( a number -- or whatever
, b number
, c number
, d long );

BEGIN
  FOR r IN (SELECT * FROM old_alphabet) LOOP
    INSERT INTO alphabet( a, b, c, d )
    VALUES (r.a, r.b, r.c, r.d);
  END LOOP;
END;
/

DROP TABLE old_alphabet; -- When you are sure it worked!

If the LONG column does contain values bigger than 32767 than there is a more complex solution involving DBMS_SQL (I think) - hopefully you won't need that?
0
 

Expert Comment

by:lamikay
ID: 8167413
create a new table alfabt1
 create table  alfabt1
 (a char,
 b number,
c number,
d long);

declare
cursor x is select a,b,d from alfabt;
begin
for xrec in x loop
insert into alfabt1
values(xrec.a,xrec.b,null,xrec.d);
end loop;
end;
/
update column c later using
update alfabt
set c= 'whatever';
it'll work!
0
 
LVL 1

Author Comment

by:wouter13
ID: 8167466
0
 
LVL 15

Expert Comment

by:andrewst
ID: 8167477
> update column c later using
update alfabt
set c= 'whatever';
it'll work!

... if you don't mind every row of alphabet having c set to the string 'whatever'!

:)

0
 
LVL 8

Expert Comment

by:Danielzt
ID: 8167526
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question