oracle_scripting_elementk1003.pdf

7
 1 Title: Useful Oracle/Unix String Processing Ti by Dan Hotka Applications: Oracle RDBMS (any version), Unix Shell (Borne/Bash/Korn) This article appeared in the October 2003 Element K Journals . All rights reserved. Introduction: This article describes how to mix Oracle and Unix to provide some powerful string functions. These string functions are useful to poss ibly automat e manual processes. These kinds of scripts can be used to automatically fix certain types of error conditions. Why would you want to do this when Oracle handles it automatically? Oracle does not check for the success/failure of the automated command and Oracle does not send a page and/or an email when this process occurred. Shell scripts are perfect to not only create the command syntax and run it but to alert you that it even occurred. This material comes from my 2-day Oracle/Unix Scripting Tips and Techniques workshop. Rules section: This article covers some useful string processing. The example is pulli ng the full name of a database file, examining it, and incrementing its numbering sequence by one. This technique is useful to automatically solve ‘tablespace cannot extent’ type errors . I leave the implementation of the solution to the user, this article illustrates how to extract this file name and increment its number. Overview: Assumptions are that the file names have a name (such as tablespace name) followed by a 2  position sequence number with a file suffix of .dbf., see Example 1. /home/oracle/oradata/ORALI9I/users02.dbf Example 1: Oracle Database File Name This task could be accomplished via shell scripts alone but it might be more wor k. You would have to find the file locations, then use awk and/or cut commands to pull out what you

Upload: thota-mahesh-dba

Post on 04-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Oracle_Scripting_ElementK1003.pdf

8/13/2019 Oracle_Scripting_ElementK1003.pdf

http://slidepdf.com/reader/full/oraclescriptingelementk1003pdf 1/7

1

Title: Useful Oracle/Unix String Processing Tiby Dan Hotka

Applications: Oracle RDBMS (any version), Unix Shell (Borne/Bash/Korn)

This article appeared in the October 2003 Element K Journals. All rights reserved.

Introduction:This article describes how to mix Oracle and Unix to provide some powerful string functions.These string functions are useful to possibly automate manual processes. These kinds ofscripts can be used to automatically fix certain types of error conditions.

Why would you want to do this when Oracle handles it automatically? Oracle does not checkfor the success/failure of the automated command and Oracle does not send a page and/or anemail when this process occurred. Shell scripts are perfect to not only create the commandsyntax and run it but to alert you that it even occurred.

This material comes from my 2-day Oracle/Unix Scripting Tips and Techniques workshop.

Rules section:This article covers some useful string processing. The example is pulling the full name of a databasefile, examining it, and incrementing its numbering sequence by one.

This technique is useful to automatically solve ‘tablespace cannot extent’ type errors. I leavethe implementation of the solution to the user, this article illustrates how to extract this filename and increment its number.

Overview:

Assumptions are that the file names have a name (such as tablespace name) followed by a 2 position sequence number with a file suffix of .dbf., see Example 1.

/home/oracle/oradata/ORALI9I/users02.dbf

Example 1: Oracle Database File Name

This task could be accomplished via shell scripts alone but it might be more work. Youwould have to find the file locations, then use awk and/or cut commands to pull out what you

Page 2: Oracle_Scripting_ElementK1003.pdf

8/13/2019 Oracle_Scripting_ElementK1003.pdf

http://slidepdf.com/reader/full/oraclescriptingelementk1003pdf 2/7

2

need. Using Oracle’s SUBSTR and INSTR SQL syntax to accomplish part of the task makeslife easier.

This article involves using Unix shell scripts in conjunction with SQL to accomplish this task. The

Oracle string functions will work in any Oracle environment. I utilize the Unix shell to provide thefinal solution. I suppose just a little more Oracle syntax would allow us to accomplish this task with asingle SQL statement but I think the code would be difficult to follow.

Body:

Our goal is to find the last file in the sequence of files for a particular tablespace, increment thatnumber, and then simply display the new file name. Once we have this file name, it can easily beincorporated into the ‘alter tablespace add file’ syntax. It can also easily be emailed or text-paged foracknowledgement that this process occurred.

We start with a look at the source for our data. Example 2 shows the DBA_DATA_FILEScolumns using TOAD.

Example 2: DBA_DATA_FILES columns

Lets examine the contents of the interested fields. FILE_NAME contains the entire file nameincluding its full path on the Unix operating system. FILE_ID is the assigned Oracle internalidentifier. This number is always incrementing so when we are working with a particular tablespacethat has more than one physical file assigned, this number will insure that we can get the last file inthe sequence. We will also use TABLESPACE name, using the USERS tablespace for this example.Example 3 shows the contents of these 3 fields on my Unix server.

Page 3: Oracle_Scripting_ElementK1003.pdf

8/13/2019 Oracle_Scripting_ElementK1003.pdf

http://slidepdf.com/reader/full/oraclescriptingelementk1003pdf 3/7

3

Example 3: Available data in DBA_DATA_FILES

The solution will require the use of 2 string functions in SQL: SUBSTR and INSTR.SUBSTR returns a character string from its target. Its syntax is: SUBSTR(‘<string orfield>’,n[m] ). The string is the column or character string. ‘n’ is the beginning position of the string.‘m’ is optional and is the length of the string to return. If ‘n’ is a positive number, the position isrelative to the beginning of the string. If ‘n’ is negative, then the position of ‘n’ is relative to the end ofthe string.

So, SUBSTR hard at work is visualized in Examples 4 and 5.

Example 4: SUBSTR Forward Search

Example 4 shows that a partial string is returned from the 3 rd position for 2 positions. Noticethis combination does return the 3 rd and 4 th positions of the character string.

Page 4: Oracle_Scripting_ElementK1003.pdf

8/13/2019 Oracle_Scripting_ElementK1003.pdf

http://slidepdf.com/reader/full/oraclescriptingelementk1003pdf 4/7

4

Example 5: SUBSTR Reverse Search

This example shows that the search starts 3 positions from the end of the string and returns 2characters.

Like other Oracle functions, you can mix the functions for the required results. In this case,we need to find the ‘.’ in the string. We want to find the number that is located right in frontof the dot and we also want to capture the string less this dot and the number. INSTR returnsthe position of a particular text string. INSTR’s syntax looks like this: INSTR(‘<string orfield>’,’<search string>[,n[,m]]). The string or field is the string to conduct the search. Thesearch string is the desired text where we would like to know it’s location. ‘n’ is the positionin the string to begin the search and ‘m’ is optional and returns the location of the ‘m’ numberof occurrences of the search string. Again, if ‘n’ is negative, it performs the search from theend of the string. Example 6 shows an example of finding string locations using INSTR.

Example 6: INSTR Text Search

Notice that the string ‘ous’ starts at location 4 in the string ‘A Mouse in the House’.

Building the example:

Lets start with a useful test of our SUBSTR and INSTR functions. Example 7 illustrates how we accessthe necessary pieces in the FILE_NAME for our solution. We will be looking for the last file createdfor the USERS tablespace. Notice in Example 3 that there are 2 physical files assigned to thistablespace.

Page 5: Oracle_Scripting_ElementK1003.pdf

8/13/2019 Oracle_Scripting_ElementK1003.pdf

http://slidepdf.com/reader/full/oraclescriptingelementk1003pdf 5/7

5

Example 7: SUBSTR/INSTR Code Preparation Example

First, we use the DUMMY column (line 1) in conjunction with the UNION statements (lines 7 and 13)to keep our output in the same order (line 19) as these SQL statements.

The first part of this SQL statement (lines 2 through 6) just displays our target string. Notice we use asub query in each of the queries (lines 4 thru 6, 10 thru 12, and 16 thru 18) to access the FILE_NAMEwith the highest FILE_ID for the desired tablespace, USERS in this example.

The second part of this SQL statement (lines 8 thru 12) pulls just the number part of the filename. Lets take a close look at line 8. Notice we are using all 4 options of SUBSTR. Thestring being searched is FILE_NAME. We want the search to start at the ‘.’ so we use INSTRto return the character position of the dot for the starting location for our SUBSTR search.You notice that we are counting backwards in the string from the INSTR location for 2

positions (with the ‘–2’ option) and we are capturing just 2 positions (with the ‘2’ option atthe end). The second line of Example 8 shows that indeed we captured the 2 positions in frontof the ‘.’, or, the number part of the FILE_NAME.

The last part we will need to capture is the file name up to the number part of the file. Againwe will use INSTR to find the ‘.’ and work from there. Line 14 is the SUBSTR but noticethat the string search starts with position 1 and the INSTR is now used to find the end positionof the string (the first ‘.’ found searching backwards from the end of the string) and we endthe search 3 positions before this point (backing up in the string past the ‘.’ and the 2-positionnumber. The third line in Example 8 shows that indeed we grabbed the string up to but notincluding the number part of the file.

File Name Info---------------------------------------/home/oracle/oradata/ORALI9I/users02.dbf02/home/oracle/oradata/ORALI9I/users

Example 8: SUBSTR/INSTR Code Output

Stepping through the code:

Example 9 shows the shell script that is used to bring these pieces together for our solution.

Page 6: Oracle_Scripting_ElementK1003.pdf

8/13/2019 Oracle_Scripting_ElementK1003.pdf

http://slidepdf.com/reader/full/oraclescriptingelementk1003pdf 6/7

6

Example 9: SUBSTR Coding Solution

Notice the shell variable-loading technique. Line 1 and line 20 sets a variable equal to thereturn of the sqlplus query. The back ticks are a shell directive that says take the contents ofthe back ticks and run it as a command. Both SQL statements return a single value that isthen populated into FileNO and FileText accordingly. Notice the “ << EOF” at line 1 and the“EOF` “ on line 9. This too is a shell directive saying take what ever falls between theselabels (in this case EOF) as if it were a file being directed in. This saves us from having a

bunch of extra files of code to maintain. The “EOF” can be about anything. It just needs to be consistent and spelled the same in both places. I tend to use EOF, I have seen others use“!”. You can use anything that makes sense to you.

Lines 2 and 11 turn off any output from sqlplus and do notice that we run sqlplus in silentmode (the –s after sqlplus on lines 1 and 10). I simply cropped the query from example 7lines 8 through 12 to capture the file sequence number and example 7 lines 14 through 18 tocapture the file name up to this file number. I increment the file number at line 20.

Line 26 creates the output that we see in Example 10. I needed the shell to zero fill a 2- position number but I am not the best scriptor in the world. Perhaps you can email me with a better solution where I don’t have to check the number and pad with a 0 in this manor. Lines24 through 29 checks our number to see if we need to pad with a zero. If we do (ie: ournumber is less than 10), then we include a variable named ‘zero’ that I set to 0 at line 22.

Example 10: SUBSTR Coding Solution Output

Page 7: Oracle_Scripting_ElementK1003.pdf

8/13/2019 Oracle_Scripting_ElementK1003.pdf

http://slidepdf.com/reader/full/oraclescriptingelementk1003pdf 7/7

7

Conclusion:This article shows how to integrate SUBSTR and INSTR to provide access to the file patternused with Oracle’s database files. I illustrated how to insure you get the right file and Ishowed exactly how this technique can be used in conjunction with shell scripts to provide thedesired results: the full-path file name that is next in sequence for a particular tablespace.

Dan Hotka is a Training Specialist who has over 25 years in the computer industry and over20 years of experience with Oracle products. He is an internationally recognized Oracleexpert with Oracle experience dating back to the Oracle V4.0 days. Dan’s latest book is theOracle10g on Linux by Oracle Press. He is also the author of Oracle9i Development ByExample and Oracle8i from Scratch by Que and has co-authored 6 other popular books. He isfrequently published in Oracle trade journals, and regularly speaks at Oracle conferences anduser groups around the world. Visit his website at www.DanHotka.com . Dan can be reachedat [email protected] .