Sie sind auf Seite 1von 7

Title: Useful Oracle/Unix String Processing Tip

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 possibly automate 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 pulling 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 work. You would have to find the file locations, then use awk and/or cut commands to pull out what you

need. Using Oracles SUBSTR and INSTR SQL syntax to accomplish part of the task makes life 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 the final solution. I suppose just a little more Oracle syntax would allow us to accomplish this task with a single 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 that number, and then simply display the new file name. Once we have this file name, it can easily be incorporated into the alter tablespace add file syntax. It can also easily be emailed or text-paged for acknowledgement that this process occurred.

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

Example 2: DBA_DATA_FILES columns


Lets examine the contents of the interested fields. FILE_NAME contains the entire file name including its full path on the Unix operating system. FILE_ID is the assigned Oracle internal identifier. This number is always incrementing so when we are working with a particular tablespace that has more than one physical file assigned, this number will insure that we can get the last file in the 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.

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 or field>,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 is relative to the beginning of the string. If n is negative, then the position of n is relative to the end of the 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 3rd position for 2 positions. Notice this combination does return the 3rd and 4th positions of the character string.

Example 5: SUBSTR Reverse Search


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

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 front of the dot and we also want to capture the string less this dot and the number. INSTR returns the position of a particular text string. INSTRs syntax looks like this: INSTR(<string or field>,<search string>[,n[,m]]). The string or field is the string to conduct the search. The search string is the desired text where we would like to know its location. n is the position in the string to begin the search and m is optional and returns the location of the m number of occurrences of the search string. Again, if n is negative, it performs the search from the end 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 access the necessary pieces in the FILE_NAME for our solution. We will be looking for the last file created for the USERS tablespace. Notice in Example 3 that there are 2 physical files assigned to this tablespace.

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 a sub query in each of the queries (lines 4 thru 6, 10 thru 12, and 16 thru 18) to access the FILE_NAME with 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 file name. Lets take a close look at line 8. Notice we are using all 4 options of SUBSTR. The string being searched is FILE_NAME. We want the search to start at the . so we use INSTR to 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 at the end). The second line of Example 8 shows that indeed we captured the 2 positions in front of 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. Again we will use INSTR to find the . and work from there. Line 14 is the SUBSTR but notice that the string search starts with position 1 and the INSTR is now used to find the end position of the string (the first . found searching backwards from the end of the string) and we end the search 3 positions before this point (backing up in the string past the . and the 2-position number. The third line in Example 8 shows that indeed we grabbed the string up to but not including the number part of the file.

File Name Info --------------------------------------/home/oracle/oradata/ORALI9I/users02.dbf 02 /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.

Example 9: SUBSTR Coding Solution Notice the shell variable-loading technique. Line 1 and line 20 sets a variable equal to the return of the sqlplus query. The back ticks are a shell directive that says take the contents of the back ticks and run it as a command. Both SQL statements return a single value that is then 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 these labels (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 silent mode (the s after sqlplus on lines 1 and 10). I simply cropped the query from example 7 lines 8 through 12 to capture the file sequence number and example 7 lines 14 through 18 to capture 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 2position number but I am not the best scriptor in the world. Perhaps you can email me with a better solution where I dont have to check the number and pad with a 0 in this manor. Lines 24 through 29 checks our number to see if we need to pad with a zero. If we do (ie: our number 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 6

Conclusion:
This article shows how to integrate SUBSTR and INSTR to provide access to the file pattern used with Oracles database files. I illustrated how to insure you get the right file and I showed exactly how this technique can be used in conjunction with shell scripts to provide the desired 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 over 20 years of experience with Oracle products. He is an internationally recognized Oracle expert with Oracle experience dating back to the Oracle V4.0 days. Dans latest book is the Oracle10g on Linux by Oracle Press. He is also the author of Oracle9i Development By Example and Oracle8i from Scratch by Que and has co-authored 6 other popular books. He is frequently published in Oracle trade journals, and regularly speaks at Oracle conferences and user groups around the world. Visit his website at www.DanHotka.com. Dan can be reached at dhotka@earthlink.net .

Das könnte Ihnen auch gefallen