Solved

SQL Script in Oracle database..

Posted on 2012-03-18
3
355 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 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.​
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now