Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-03-19
8
Medium Priority
?
1,077 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
If you try to migrate from Elastix to Issabel, you will face a lot of issues. These problems are inevitable but fortunately, you can fix them. In the guide below, I will explain how I performed the migration while keeping all data and successfully t…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

564 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