Oracle and more - Amardeep Sidhu

My experiences with technology…

Archive for the 'Oracle Tips' Category


DBA_MVIEW_COMMENTS view in 10g

Posted by Sidhu on 21st July 2008

Our application (3 tier, Front end Forms10g and back end 10gR2) provides user with a front end to refresh the mviews. That form has 2 columns showing mview name and the comment against it. Recently i saw that while opening this front end ORA-01403 NO DATA FOUND was being raised.

I opened the fmb and found that it was populating comments from DBA_TAB_COMMENTS. In 10g the comments against mviews are stored in DBA_MVIEW_COMMENTS unlike till 9i where it was in ALL_TAB_COMMENTS. So there was a little modification required.

BTW if you try to comment on the table (which is created with MVIEW) it won’t allow you to do so and instead raise ORA-12098: cannot comment on the materialized view.

So may be that little change needs to be done !

Posted in Oracle Tips | No Comments »

Import and default values for columns

Posted by Sidhu on 10th May 2008

I had got an export of a database and had to import it to a new database. The only difference was that in new database few of the large tables were partitioned. So instead of partitioning it after the import, i thought about pre-creating the tables (with partitioning) and then run import with ignore=Y . Everything went fine. But later on the front end application gave some error and we came to know that default values for columns in some tables were not set. I did some googling and didn’t find much. Then i posted the same to OTN forums and came to know that if the table pre-exists, import doesn’t take care of default values of columns. Metalink note 224727.1 discusses this. So if you are pre-creating the tables and there are some default values for any columns, set it manually, don’t rely on import for this. Same is true for impdp as well.

Posted in Oracle Tips | No Comments »

Unxutils for Windows

Posted by Sidhu on 10th April 2008

If you are used to work in Unix enviroment and then sometime, in between have to sit on Windows and tail -f alert_DB.log, its a real pain. There is a small bundle of utilities, called Unxutils which can make you feel at home in Windows too. These are the exe’s of all major commands in Unix like more, less, ls, grep etc…
To use it just download the zip file from the link above, extract it to some folder and add the path of exe’s to your Windows PATH. Restart your machine and you are done.
Happy more’ing…less’ing…

Posted in Oracle Tips, Unix/Linux | No Comments »

ORA-03113 Refresh of a mview in Oracle 10g

Posted by Sidhu on 3rd January 2008

At my workplace we were facing a problem with refresh of a mview. Say it was created in schema of user1 but when I tried to refresh it from user2 it would give ORA-03113: end-of-file on communication channel. Then we raised a SR and have been following up with Oracle support for long but it was not getting anywhere. Yesterday that guy seemed to have reached some point. The mviews that we have created and are having problem with refresh are created on top of both local & remote objects and he said that up to 11gr2 there is no possibility of creating mviews on both local and remote objects. I did validate this thing. All the mviews failing to refresh are created on top of both local & remote objects. But again from the owner the refresh is fine but from another user it gives problem. By the way that guy hinted at bug 4084125 and also suggested a work around. I haven’t tried that yet. Will try and update about the results.

Sidhu

Posted in Oracle Tips, Troubleshooting | No Comments »

Importing a full database…

Posted by Sidhu on 18th August 2007

Many times, we are required to restore a database from an export dmp file. Its a simple task but sometimes there are some issues left like invalid objects or some objects missing, in the newly created database. Following steps, followed in order can help in creating an error free database:

  1. Create a blank database: The very first step is to create a blank database which is to be used as the target database. That can be done using Database Configuration Assistant. (In last step of the DBCA, change redo log file sizes to 500 MB each (or some appropriate values depdening upon the size of the databaes), as during import, lot of redo will be generated, so large redo size helps in that scenario)

  2. Extract DDLs and create tablespaces: Now run the import with show=Y option and create a log of all DDL statements. The main things to be looked for in the log are DDLs to create tablespaces and DB links. You may need to change the create tablespace statements according to the version of the Oracle you are using. If you have the export taken in an older version, where dictionary tablespaces were being used, you will need to change the statements accordingly, to create locally managed tablespaces.
    (If you have the dmp file in compressed (.Z) format check here, to run the import directly from compressed file)

  3. Adjust the size of SYSTEM, TEMP, USERS and UNDO: As SYSTEM, TEMP, USERS and UNDO tablespaces will get created with the database itself, so you can alter the sizes as per the sizes in the old database.

  4. Edit tnsnames.ora and create dblinks: Now edit tnsnames.ora to include all the databases used in the db links and create db links using the statements from DDL log.

  5. Run the import: Finally, run the import with FULL=Y and IGNORE=Y options and after the import finishes, look for any errors in the log. At last, compile all the invalid objects in the database (Here is the link to a script to compile all the invalid objects). (If the import terminates with ORA-01435, then have a look at this post.)

To read about all the options with imp have a look at Original Import & Export Utilities chapter of Oracle Utilities guide.

Sidhu

Posted in Oracle Tips | No Comments »

Moving datafiles,control files and log files - Part 1

Posted by Sidhu on 21st July 2007

Many times you need to move datafiles from one location to another. The simplest approach for this is to take the tablespace offline, copy the datafiles to new location, rename the files with alter database rename file (Except that you dont have to move the SYSTEM and UNDO tablespace, as you can’t take SYSTEM tablespace offline)


SYS@orcl AS SYSDBA> alter tablespace system offline;
alter tablespace system offline
*
ERROR at line 1:
ORA-01541: system tablespace cannot be brought offline; shut down if necessary

SYS@orcl AS SYSDBA>

Well lets try moving USERS tablespace.


SYS@orcl AS SYSDBA> column file_name format a50

SYS@orcl AS SYSDBA> set lines 100
SYS@orcl AS SYSDBA> select file_name,tablespace_name from dba_data_files where tablespace_name='USERS';

FILE_NAME                                          TABLESPACE_NAME
-------------------------------------------------- ------------------------------
C:\ORACLE\ORCL\USERS01.DBF                         USERS

SYS@orcl AS SYSDBA>

The current location of the datafile is C:\ORACLE\ORCL\. Suppose I have to move it to c:\oracle\oradata. So first lets take the tablespace offline


SYS@orcl AS SYSDBA> alter tablespace users offline;

Tablespace altered.

SYS@orcl AS SYSDBA>

Now copy the datafile to new location [Note the new directory should be already created]


C:\oracle\ORCL>copy USERS01.DBF c:\oracle\oradata
1 file(s) copied.

C:\oracle\ORCL>

Now make database aware of the new location of the datafile using alter database rename file:


SYS@orcl AS SYSDBA> alter database rename file 'c:\oracle\orcl\users01.dbf' to 'c:\oracle\oradata\users01.dbf';

Database altered.

SYS@orcl AS SYSDBA>

The last thing is to bring the tablespace online. If everything has gone rightly the message like this will appear and you can view the new location of the datafile.


SYS@orcl AS SYSDBA> alter tablespace users online;

Tablespace altered.

SYS@orcl AS SYSDBA> select file_name,tablespace_name from dba_data_files where tablespace_name='USERS';

FILE_NAME                                          TABLESPACE_NAME
-------------------------------------------------- ------------------------------
C:\ORACLE\ORADATA\USERS01.DBF                      USERS

SYS@orcl AS SYSDBA>

In next post we will discuss moving datafiles, controlfiles and logfiles.

Posted in Oracle Tips | No Comments »

Import ORA-01435: user does not exist…

Posted by Sidhu on 14th July 2007

One may encounter this error while importing from a dmp file from older versions of Oracle. Genereally this error is caused by some statement like alter session set current_schema=scott; And the simple reason is that the user scott doesn’t exist. Yesterday I came across this error. And the reason was that user was not created. As in case of import we generally create tablespaces first (by creating the DDL using option show=Y) but creation of users is done by import itself. In older versions of Oracle, the temp tablespaces were no different from other tablespaces. But in newer versions temp tablespaces are different. So in dmp files from thoese older versions create user statements are written like create user t1 identified by t1 default tablespace temp temporary tablespace temp. This thing worked fine in older versions but in newer versions we cannot specify the TEMP tablespace as the default tablespace for a user. So the statement create user t2 identified by t2 default tablespace temp temporary tablespace temp throws ORA-12910: cannot specify temporary tablespace as default tablespace. In such cases the users (for which default statement is specified as TEMP) have to be created manually by specifying the appropriate tablespace as default tablespace and then the import should be run with ignore=Y.

Sidhu

Posted in Oracle Tips | 1 Comment »

Shell script to spool a no of tables into .xls files…

Posted by Sidhu on 26th June 2007

On OTN someone asked a question that how to spool data from a table into a xls file. Spooling a single table I discussed in one of the previous posts. We can use the same approach to spool data from more than 1 table also. Well here I will do it through a shell script and assume that you have a text file having list of tables to be spooled (Even if you don’t have one, it can be easily made by spooling the names of tables into a simple text file) Here is the shell script that you can use to spool data to various xls files, table wise.

cat list.txt | while read a
do
echo "spooling $a"
sqlplus username/password@string <<EOF
set feed off markup html on spool on
spool /home/oracle/$a.xls
select * from $a;
spool off
set markup html off spool off
EOF
done

I didn’t see any work around for Windoze as SQLPLUS << EOF thing doesn’t seem to work in Windows. Will try to find some alternative. If you come across something, do let me know.

Sidhu

Posted in Oracle Tips, SQL | No Comments »

Is multiplication faster than division ?

Posted by Sidhu on 18th June 2007

An interesing post by Laurent. Check out http://laurentschneider.com/wordpress/2007/06/to-divide-or-to-multiply.html

My findings on 10gR2 on Windoze XP

SQL> var z numberSQL> var y numberSQL> exec :z := power(2,102)*2e-31;

PL/SQL procedure successfully completed.

SQL> exec :y := 1e125;

PL/SQL procedure successfully completed.

SQL> set timi onSQL> exec while (:y>1e-125) loop :y:=:y/:z; end loop

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.10SQL> set timi offSQL> print y

      Y----------9.988E-126

SQL> exec :z := power(2,-104)*2e31;

PL/SQL procedure successfully completed.

SQL> exec :y := 1e125;

PL/SQL procedure successfully completed.

SQL> set timi onSQL> exec while (:y>1e-125) loop :y:=:y*:z; end loop

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.04SQL> set timi offSQL> print y

      Y----------9.988E-126

SQL>

Sidhu

Posted in Oracle Tips | No Comments »

Spool to a .xls (excel) file…

Posted by Sidhu on 16th June 2007

A small tip, I read on OTN about spooling to a .xls (excel) file:

It goes like this

set feed off markup html on
spool onspool c:\salgrade.xls
select * from salgrade;
spool offset markup html off
spool off

And the xls it makes shows up like:

Sidhu

Posted in Oracle Tips, SQL | 7 Comments »