Showing posts with label oracle. Show all posts
Showing posts with label oracle. Show all posts

Oracle report all users, their roles and privileges

>> Wednesday, 31 October 2018


I took the sql for listing privileges recursively for Oracle users from Rene Nyffenegger's site, it is very good but only works for one user at a time as it stands, I wanted to run a report that outputted the same way for every user in my database.  So it needed to go into a PL/SQL loop.



set serveroutput on

declare

TYPE re_type IS TABLE OF varchar2(1024);
v_statement varchar2(1024);
v_result re_type;

BEGIN
for x in ( select username from dba_users)
LOOP
v_statement := 'select concat(lpad('' '', 2*level), granted_role) line1 from (select null grantee, username granted_role from dba_users where username = ''' || x.username || ''' union select grantee, granted_role from dba_role_privs union select grantee, privilege from dba_sys_privs ) start with grantee is null connect by grantee = prior granted_role';

execute immediate v_statement BULK COLLECT INTO v_result;

for indx in v_result.FIRST .. v_result.LAST
loop DBMS_OUTPUT.PUT_LINE(v_result(indx));
end loop; 

END LOOP;
END;
/

You get a formatted output like:

MYUSER1
    MYROLE_SESSION
      ALTER SESSION
      CREATE SESSION
    CONNECT
      CREATE SESSION
    MYROLE1
    MYROLE2
  MYUSER2
    CONNECT
      CREATE SESSION
    MYROLE1
    MYROLE3 

Particular thanks to Pete for seeing me past my quotes and array woes

Read more...

Finding Oracle tablespaces with mixed autoextensible datafiles

>> Monday, 27 February 2017




Two ways to identify tablespaces where some datafiles are autoextend true and some are false.
(Mixing these can cause issues with monitoring, so try to keep them consistent.)


SELECT DISTINCT a.tablespace_name
FROM dba_data_files a
WHERE a.autoextensible ='YES'
AND EXISTS
(SELECT DISTINCT b.tablespace_name
FROM dba_data_files b
WHERE b.autoextensible ='NO'
AND b.tablespace_name=a.tablespace_name )
ORDER BY tablespace_name;



SELECT  ddf2.tablespace_name
FROM
(SELECT DISTINCT ddf.tablespace_name,ddf.autoextensible
FROM  dba_data_files ddf
GROUP BY ddf.tablespace_name,ddf.autoextensible
) ddf2
GROUP BY ddf2.tablespace_name
HAVING  count(1) = 2
ORDER BY ddf2.tablespace_name;





Read more...

EXPDP using QUERY with to_timestamp or to_date

>> Friday, 20 May 2016

For expdp with select queries using to_timestamp or to_date I've had a bit of game getting the apostrophes to play happy bunny. Within a parfile I was getting nowhere, so back to my easy friend ksh (run against an 11.2 database):


#!/bin/ksh

#set the environment
export PATH=$PATH:/usr/local/bin
export ORACLE_SID=MYSID
export ORAENV_ASK=NO
. oraenv
unset ORAENV_ASK

export NLS_LANG="English_United Kingdom.AL32UTF8"

#piping password in is good practise for not seeing it when doing a ps -ef | grep expdp
PASSWORD=myuserpassword
echo $PASSWORD | $ORACLE_HOME/bin/expdp myuser \
DIRECTORY=MY_DPUMP_DIR \
CONTENT=ALL \
COMPRESSION=ALL \
DUMPFILE=mydumpfilename.dmp \
LOGFILE=mylogfilename.log \
EXCLUDE=STATISTICS \
tables=schemaowner.table_name \
QUERY=\"where column_name \> to_timestamp\(\'2015-11-10 00:00:00:000000\',\'YYYY-MM-DD HH24:MI:SS:FF\'\)\"means the parameter is simply ignored




Notes:

Whilst out of habit I set NLS_LANG parameter, in expdp it is not really needed any more. See utilities manual for further advice

DIRECTORY: remember for datapump you need to define the directory in oracle:

Create directory MY_DPUMP_DIR as '/myfilesystem/mydirectory';
grant read, write on directory MY_DPUMP_DIR to myuser;
select * from dba_directories;

My other parameters like CONTENT, COMPRESSION, EXCLUDE are there for reasons relevant to what I was doing at the time. You need to think about what you need for the type of file you require, where it is going to be imported to and what is actually needed there.

http://docs.oracle.com/cd/B28359_01/server.111/b28319/dp_export.htm#i1006293

and finally, watch your spaces, I found that leaving spaces like this:

VERSION = 11.1.0 \

caused the parameter to be ignored
whereas:

VERSION=11.1.0 \

it is was taken into account


Read more...

My experience of UKOUG TECH15 (UK Oracle User Group Technology Conference 2015)

>> Wednesday, 16 December 2015

This year's UK Oracle User Group Technology Conference took place in Birmingham.

I went on the middle day of the 3 (4 including super Sunday) day event.

The agenda is huge on every day.  There are 7 streams: Database, Apex&DB Dev, Big Data and Warehousing, Business Analytics, Engineered Systems, Integrations and Collaboration, Operating Systems, Virtualisation, Web&Mobile apps, Keynotes and General Cross Topic.  And within those streams there are different presentations happening all day, each day - that's a lot to choose from.

So remembering that Apps15 and JDE15 are also happening at the same time you start to get a feel for how huge it is.

In fairness I rarely use this word but....it's awesome (!)









I sat down with a print out of the streams I was interested in and a highlighter pen and planned my day in advance.

I plumped for:


Women in IT Breakfast : Debra Lilley and Evolution
Advanced ASH Analytics ASH Masters:  Marcin Przepiorowski
Keynote: Oracle Database 12c What's next?
Oracle Flashback: Connor McDonald
Adaptive dynamic sampling : Christian Antognini
Interpreting AWR Reports:  Franck Pachot
Just Don't Do it: Jonathon Lewis
Evolution of performance Management: Oracle 12c Adaptive optimization: Nelson Calero


The mobile app for the conference works well, you tell it your agenda and it tells you where to go and when, simples.  The full agenda is on it to look through, the speaker lists and you can leave feedback for the presentations through it.

It was a full day 8am to 5:30pm with little break.  I admit to being brain fried by the end of the day.  Whilst all the presentations were worthwhile, I have to say that Connor and Franck made the day worth its weight in membership fee and travel time.

I find it hard to explain the value of this type of training...yes, it is training.  Some of it is sound bite, some of it is in-depth, all of it excites me, all of it motivates me to want to use these things in my day to day work and yes, they motivate me to work smarter....I suspect most people attending feel the same way.




I'm not sure you could keep up the level of presentation attendance I did in one day for 3 (or 4) full days but there are other 'diversions'.


There was a set of challenges through the 'conference explorer' for each day to enter a prize draw (and get scores on the doors), some involved visiting exhibitor stands, getting selfies, tweeting stuff, finding stuff in the app, meeting new people.  I quite like things like that.

The loudest exhibitor stand seemed to by the golfing area that had putting for pounds.  Clever draw.  As popular as the Scalextric a few years ago.












There was the drinks in the conference hall after the days work and that was followed by the Big Birmingham Bash.  I didn't stay for the bash and I know very little of it,  I suspect what happens in Birmingham stays in Birmingham!!



 Badges have become quite the thing and there were many 'in house' and 'out of house' ones to be collected.


Part of the draw for me for the UKOUG is the incredible community it is.  I now know people that go to these events I am proud to call my friends.  Of course the good and the great are there to be spoken to (So many ACEs, Masters, OakTable members) but also the people I talk to a lot on social media (there may be some cross over there!).  The people that help each other as problems present themselves across the year.  The person that responds to a twitter beg when you really can't find something in a manual, the ones who write all those great blog posts you've been cribbing off all year.  It's a great opportunity to give them a hug/whiskey/wide berth* (delete as appropriate based on last interaction.)



As I said to one twitter friend, you'll spot me I'm playing 'Where's Wally' but in monochrome!

One year I swear I will get there for the full conference! Now that really would be quite awesome but I think I would have to go for the full red and white stripes.

If you are a member of the UKOUG and get a conference pass but don't use it: huge mistake - no, seriously - just go - get that training and networking in.

If you aren't a member of the UKOUG you should look at joining.  If you are working for a company there are many ways you can talk to them about justifying the cost of membership.

For me this one day taught me that we can use Flashback data archive now and it is a good solution for some of my archiving woes.  I found out I knew more about Sharding and other up and coming changes than the guy from Oracle that comes into my company to brief us on new features. I got more confident about the way I interpret AWR, and I have already seen Adaptive Sqlplans in explain plans I've been looking at and thought "ahhhh I know what you are and how you got there!"

It's all good.

Read more...

sending sql output to a csv file

>> Thursday, 15 October 2015

So I would previously have been doing something like this

spool myfile.csv;
set heading off
set pages off
select column1_name||','||column2_name||','||column3_name||','||column4_name
from mytable order by 1;
spool off

which is fine but I had to build up the statement each time.


Step in Tom, I found this procedure tucked away in an asktom page from 2000

sqlplus / as sysdba
grant execute on utl_file to system;
create or replace directory DPUMP_TMP as '/tmp';
grant read,write on directory DPUMP_TMP to system;


CREATE OR REPLACE procedure SYSTEM.dump_table_to_csv( p_tname in varchar2,
                                               p_dir   in varchar2,
                                                p_filename in varchar2 )
   is
     l_output        utl_file.file_type;
     l_theCursor     integer default dbms_sql.open_cursor;
      l_columnValue   varchar2(4000);
    l_status        integer;
    l_query         varchar2(1000)
                      default 'select * from ' || p_tname;
    l_colCnt        number := 0;
     l_separator     varchar2(1);
    l_descTbl       dbms_sql.desc_tab;
 begin
      l_output := utl_file.fopen( p_dir, p_filename, 'w' );
     execute immediate 'alter session set nls_date_format=''dd-mon-yyyy hh24:mi:ss'' ';

     dbms_sql.parse(  l_theCursor,  l_query, dbms_sql.native );
      dbms_sql.describe_columns( l_theCursor, l_colCnt, l_descTbl );

    for i in 1 .. l_colCnt loop
          utl_file.put( l_output, l_separator || '"' || l_descTbl(i).col_name || '"' );
         dbms_sql.define_column( l_theCursor, i, l_columnValue, 4000 );
       l_separator := ',';
      end loop;
     utl_file.new_line( l_output );

     l_status := dbms_sql.execute(l_theCursor);

      while ( dbms_sql.fetch_rows(l_theCursor) > 0 ) loop
        l_separator := '';
          for i in 1 .. l_colCnt loop
            dbms_sql.column_value( l_theCursor, i, l_columnValue );
              utl_file.put( l_output, l_separator || l_columnValue );
              l_separator := ',';
         end loop;
          utl_file.new_line( l_output );
     end loop;
     dbms_sql.close_cursor(l_theCursor);
       utl_file.fclose( l_output );

     execute immediate 'alter session set nls_date_format=''dd-MON-yy'' ';
   exception
     when others then
         execute immediate 'alter session set nls_date_format=''dd-MON-yy'' ';
          raise;
 end;
/



exec SYSTEM.DUMP_TABLE_TO_CSV ('schema.tablename','DPUMP_TMP','myfilename.csv');


If you want to read more about Tom talking about unloading data to a flat file go here http://tkyte.blogspot.co.uk/2009/10/httpasktomoraclecomtkyteflat.html


Read more...

Oracle Midlands - ClubOracle

>> Thursday, 21 May 2015

It as taken me a while to get to an Oracle Midlands event - this was the 9th and I've been saying I'd go since number 1.. The venue in Birmingham was easy to get to from the train station, there is easy parking at the venue (once you are through city centre traffic!)

I was surprised at the number of attendees.  There were 30 there and I think there would have been more if it weren't for the dreadful weather on the day combined with the unusually heavy traffic.

The group was really friendly and I was pleased to see people I immediately recognised both from twitter feeds and from previous jobs.

The first speaker at #om9 was Joel Goodman.  I think that was a bit of a scoop. Joel is amazing.  He is a walking oracle encyclopaedia combined with some of the best training (and presentation) skills I have ever known.  He trained me on Oracle 8i Backup and Recovery more years back than I am prepared to admit to.

His session was on Oracle Distributed Transactions. Clear and concise fundamentals including2 phase commits and the killer "this is how it can bite you" information.

I knew by 7:05pm my train ticket had paid for itself when the fog started to clear on some of the reasons for in-doubt transactions and the chain of table locks.  Another one of those 'I'm sure I knew this once but I really ought to know it all the time" moments.

There was a break for a snack and a chat, a which point the penny dropped for a few of the people I'd been talking to like old friends who I actually was and that they talk to me on-line fairly regularly.  And the same for me as people introduced themselves.  I love how friendly DBAs are.

The second session was Datapump.  I'll kick back on this I thought, I know this one. Wrong!  Rich Harrison pulled the "oh, I didn't know that" out of the bag a number of times.  He is also a really smooth presenter and one to watch out for in the future.  The next day at work a colleague was having a slow running export nightmare and I was all "did you know you can do this?" !  Felt good.

So If you are in the Midlands region (or passing through), I recommend this user group. It is free to attend (sponsored by Redstack Techologies) and in the evening.  What's not to like.

Have a look at the up and coming events here.


Read more...

DBSNMP.BSLN_INTERNAL invalid ORA-12012 ORA-04063

>> Wednesday, 14 January 2015

ORA-12012: error on auto execute of job "SYS"."BSLN_MAINTAIN_STATS_JOB"
ORA-04063: package body "DBSNMP.BSLN_INTERNAL" has errors
ORA-06508: PL/SQL: could not find program unit being called: "DBSNMP.BSLN_INTERNAL"
ORA-06512: at line 1

Tested fix on Oracle version  11.2.0.3.7


I've been getting the error in the alert log since the database was built using an rman clone.

I could force the error by running:

As sys:
     exec dbms_scheduler.run_job('BSLN_MAINTAIN_STATS_JOB',false);

     select log_date,status from dba_scheduler_job_run_details
     where job_name='BSLN_MAINTAIN_STATS_JOB' order by log_date;


I tried the recommended work around from Oracle to recreate the dbsnmp user:

@?/rdbms/admin/catnsnmp.sql
@?/rdbms/admin/catsnmp.sql

This did not fix it.

Oracle recommended I apply a patch for bug 13637859

This did not fix it.

I rolled back the patch, restored dbsnmp as it was and started a rethink.  There were 2 distinct problems happening:


The first one is that the package DBSNMP.BSLN_INTERNAL is invalid:

select substr(owner,1,12) owner, substr(object_name,1,30) object, substr(object_type,1,30) type,
 status from dba_objects where object='BSLN_INTERNAL';

DBSNMP BSLN_INTERNAL PACKAGE BODY INVALID
   
alter package DBSNMP.BSLN_INTERNAL compile;

Warning: Package altered with compilation errors.

select * from dba_errors where OWNER='DBSNMP';

DBSNMP   BSLN_INTERNAL    PACKAGE BODY   1       1910          7
PLS-00201: identifier 'DBMS_JOB' must be declared
ERROR                201

DBSNMP  BSLN_INTERNAL PACKAGE BODY   2       1910          7
PL/SQL: Statement ignored
ERROR                  0

So DBMS_JOB has a problem, well execute was revoked from public on DBMS_JOB on this database.

grant execute on sys.dbms_job to dbsnmp;
alter package DBSNMP.BSLN_INTERNAL compile;
revoke execute on sys.dbms_job from dbsnmp;

select substr(owner,1,12) owner, substr(object_name,1,30) object, substr(object_type,1,30) type,
 status from dba_objects where object_name='BSLN_INTERNAL';

DBSNMP BSLN_INTERNAL PACKAGE BODY VALID
   

     exec dbms_scheduler.run_job('BSLN_MAINTAIN_STATS_JOB',false);

     select log_date,status from dba_scheduler_job_run_details
     where job_name='BSLN_MAINTAIN_STATS_JOB' order by log_date;

Job Failed

So I have a valid package now but my job is still failing.  If your database was not cloned you might find this grant and recompile will fix your problem. (The revoke of the privilege again afterwards was fine, it seemed to only need it to recompile, not to actually run)

My second issue was caused when cloning a new db using rman.

select * from DBSNMP.BSLN_BASELINES;
nnnnnnnn OLDSOURCEDBNAME  0 xxxxxxxxxxxxxxxxxxxx Y ACTIVE dd/mm/yyyy

There was only one row in my table and that had the name of the old database in it, I removed it:

delete from DBSNMP.BSLN_BASELINES WHERE INSTANCE_NAME='OLDSOURCEDBNAME ';
commit;

exec dbms_scheduler.run_job('BSLN_MAINTAIN_STATS_JOB',false);
select log_date,status from dba_scheduler_job_run_details
     where job_name='BSLN_MAINTAIN_STATS_JOB' order by log_date;
succeeded

This removal of one line is going to be how the Oracle recommended work around for this problem fixes it, you should probably use the Oracle way not my hack.  But for me Oracle Enterprise Grid/Cloud/OEM/yet another name change for OEM used DBSNMP and I didn't want to upset it on my production system, which is why I deleted the row rather than doing:

@?/rdbms/admin/catnsnmp.sql
@?/rdbms/admin/catsnmp.sql

I found at least 3 blogs with pieces of all this information in but none with it together that solved my issue.

Remember here we have considered:

  • An invalid package caused by revoking execute on DBMS_JOB fixed by granting execute directly to DBSNMP.
  • An entry in DBSNMP.BSLN_BASELINES table carried over from an old source database on clone build.
  • The Oracle Workaround covered in Oracle Support Doc ID 1413756.1 ORA-12012: error on auto execute of job "SYS"."BSLN_MAINTAIN_STATS_JOB"
  • An Oracle Patch for Bug 13637859

Please speak to Oracle Support if you are in anyway unsure that you have the same issue and always make sure you have a good backup before you change anything.

Read more...

The Importance of Using Common Sense

>> Wednesday, 11 May 2011

It is so important to sense check documentation and general information you find before you blindly wading in thinking you're doing ok.

I'm not just talking about the lay blogger who randomly throws out advice now and again (!), even Oracle get it wrong.  Actually that was double sarcasm, they often get it wrong.  Well why wouldn't they?  Each person is only human.

When you are Googling around, browsing MyOracleSupport or, the last resort, actually reading Oracle's own documentation (why don't I go there first?!) remember not to rely on the accuracy of everything you read.

Firstly with Oracle documentation check the document version at the top of the page.  There are many subtle differences between the versions, if you decide to read the 10.2 Security Guide to implement 11.2 Security you will fall down a hole at some point.

Even then when you are happy you have the right documentation for the right version and it's straight from Oracle's mouth, there are still pit falls in accuracy. 


 
PASSWORD_REUSE_MAX
 Sets the number of days before which a password cannot be reused.

PASSWORD_REUSE_TIME
 Sets the number of password changes required before the current


No, go back and look again, do you feel your right eye brow raising wondering if that is quite right, quite logical?

In 9i, 10g and 11.2 documentation the PASSWORD_REUSE_MAX is number of changes and the PASSWORD_REUSE_TIME is days.  A slip up for one version.

I reported it to Oracle and they are correcting, so maybe by the time you find this it will all be ok.

These kind of sense checks are particularly important with new features in new versions.  Hunt around for dbms_audit_mgmt and you will find a number of conflicting posts at the moment.

Don't blindly follow examples, have a think about why you are doing what you are doing and whether it really makes sense and hey, if it makes no sense whatsoever and it is definitely correct then it's a 'feature' so blog it!

I feel a dbms_audit_mgmt post coming on...

Read more...

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP