Solved

SQL Script in Oracle database..

Posted on 2012-03-18
3
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
Wasim Akram Shaik earned 305 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

756 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