Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Script in Oracle database..

Posted on 2012-03-18
3
Medium Priority
?
365 Views
Last Modified: 2012-03-21
Hi,
I am working on writing a scripts in plsql on Oracle 9i database. In that script it update the table and in that I need validate 3 thing be update the table.
1. The user which run scripts should be eg.SCOTT
2. The tablespace which table is there eg. user should have 500mb of free space.
3. The date which user enter to update the record should not be < than 1 year the sysdate
(eg: sysdate is 2012/03/12 it should be less than 2011/03/11)
The date is enter by the user as a user parameter.
Thanks in advance.
0
Comment
Question by:irsbenz
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
Wasim Akram Shaik earned 1220 total points
ID: 37736314
you can do something like this as you said, the user will have the parameters, to validate the third condition we would need parameters which user will enter..

probably this can  be done via a trigger on that table, however incase you want to do via a plsql script, then better would you give user an api which will accept all the input parameters for updation and validate the incoming date parameter, also the scott would need to have the access to few dba tables, to query the free space, prepared a rough version without any exception handling, you modify this according to your needs..


check this out


DECLARE
   v_user    VARCHAR2 (100) := 'SCOTT';
   v_free    NUMBER;
   v_total   NUMBER;
   v_date    VARCHAR2 (100) := '01-JAN-1986';
   v_days    NUMBER;
BEGIN
   SELECT USER
     INTO v_user
     FROM DUAL;

   IF v_user = 'SCOTT'
   THEN
      SELECT *
        INTO v_free, v_total
        FROM (SELECT /* + RULE */ df.BYTES / (1024 * 1024) "Size (MB)",
                        SUM (fs.BYTES) / (1024 * 1024) "Free (MB)"
                  FROM dba_free_space fs,
                       (SELECT   tablespace_name, SUM (BYTES) BYTES
                            FROM dba_data_files
                        GROUP BY tablespace_name) df
                 WHERE fs.tablespace_name(+) = df.tablespace_name
                   AND fs.tablespace_name = 'USERS'
              GROUP BY df.tablespace_name, df.BYTES
              UNION ALL
              SELECT /* + RULE */ fs.BYTES / (1024 * 1024),
                        SUM (df.bytes_free) / (1024 * 1024)
                  FROM dba_temp_files fs,
                       (SELECT   tablespace_name, bytes_free, bytes_used
                            FROM v$temp_space_header
                        GROUP BY tablespace_name, bytes_free, bytes_used) df
                 WHERE fs.tablespace_name(+) = df.tablespace_name
                   AND fs.tablespace_name = 'USERS'
              GROUP BY df.tablespace_name,
                       fs.BYTES,
                       df.bytes_free,
                       df.bytes_used);

      IF v_free > 500
      THEN
         SELECT SYSDATE - TO_DATE (v_date, 'DD-MON-YYYY')
           INTO v_days
           FROM DUAL;

         IF v_date < 365
         THEN
                    NULL;--- Replace this null with the update statement, such as update emp set hire_date=v_date;
         ELSE
            DBMS_OUTPUT.put_line ('Date Validation is failed');
         END IF;
      ELSE
         DBMS_OUTPUT.put_line ('TableSpace is not free ');
      END IF;
   ELSE
      DBMS_OUTPUT.put_line ('Script Terminated As User is not SCOTT');
   END IF;
END;

Open in new window

0
 

Author Comment

by:irsbenz
ID: 37750625
thanks.
0
 

Author Closing Comment

by:irsbenz
ID: 37750627
Excellent straight to point.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

876 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