Friday, October 21, 2011

Oracle LONG RAW

Convert LONG RAW to VARCHAR2 in Oracle


Here is the script to convert LONG RAW data to VARCHAR2.

DECLARE
   v_text   VARCHAR2 (32000) := '';
BEGIN
   FOR i IN (SELECT long_raw_column FROM long_raw_table)
   LOOP
      v_text := UTL_RAW.CAST_TO_VARCHAR2 (i.MESSAGE);

      DBMS_OUTPUT.put_line (v_text);
   END LOOP;
END;


Convert BLOB to CLOB using Java


In one of my testing I need CLOB data, but it was in BLOB, so I have used Java to convert but if any used in Oracle?.

package com;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.util.JDBCUtil;

/**
 * The class is used to convert BLOB column values to CLOB column.
 * 
 * @author Manivel
 * @see java.sql.Blob
 */

public class ConvertBlobToClob {
 public static void main(String[] args) {
  Statement stmt;
  PreparedStatement pstmt;
  ResultSet rs;
  Connection conn;

  int messageId;
  Blob message;

  try {
   conn = JDBCUtil.getJDBCConnection();
   stmt = conn.createStatement();
   /*
    * messages : message_id is clob column
    */
   pstmt = conn.prepareStatement("INSERT INTO messages (message_id, message) VALUES (?,?)");

   /*
    * messages_blob : message is blob column
    */
   rs = stmt.executeQuery("select message_id, message from messages_blob");

   while (rs.next()) {
    messageId = rs.getInt(1);
    message = rs.getBlob(2);

    int offset = -1;
    int chunkSize = 1024;
    long blobLength = message.length();
    if (chunkSize > blobLength) {
     chunkSize = (int) blobLength;
    }
    char buffer[] = new char[chunkSize];
    StringBuilder stringBuffer = new StringBuilder();
    Reader reader = new InputStreamReader(message.getBinaryStream());

    while ((offset = reader.read(buffer)) != -1) {
     stringBuffer.append(buffer, 0, offset);
    }

    pstmt.setInt(1, messageId);
    pstmt.setString(2, stringBuffer.toString());

    pstmt.executeUpdate();

    System.out.println("Success :" + messageId);

   }

  } catch (SQLException e) {
   System.err.println(e.getMessage());
   e.printStackTrace();
  } catch (IOException e) {
   System.err.println(e.getMessage());
   e.printStackTrace();
  }
 }
}




JDBCUtil.java




package com.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 
 * @author manivel
 *
 */
public class JDBCUtil {
 final static String jdbcDriver = "oracle.jdbc.driver.OracleDriver";

 /*
 private static String strHostName = "localhost";
 private static String strDBSID = "orcl";
 private static String strPortNo = "1521";
 */
 private static String strUserName = "scott";
 private static String strPassword = "tiger";
 
 
 private static String tnsName = "ORCL";
 
 private static Connection connection = null; 

 /**
  * 
  * @return
  */
 public static Connection getJDBCConnection() {
  
  try {
   if (connection == null || connection.isClosed()) {

    
    /*
      String strURL = (new StringBuilder())
      .append("jdbc:oracle:thin:@//").append(strHostName)
      .append(":").append(strPortNo).append("/")
      .append(strDBSID).toString();
    */

    String strURL = (new StringBuilder())
      .append("jdbc:oracle:oci8:@").append(tnsName).toString();
    
    Class.forName(jdbcDriver);

    connection = DriverManager.getConnection(strURL, strUserName,
      strPassword);

   }
  } catch (ClassNotFoundException e) {
   System.err.println(e.getMessage());
   e.printStackTrace();
  } catch (SQLException e) {
   System.err.println(e.getMessage());
   e.printStackTrace();
  }

  return connection;
 }
}




Copyright - There is no copyright on the code. You can copy, change and distribute it freely. Just mentioning this site should be fair

(C) August 2008, manivelcode

Find schema/user action(s) in Oracle

Find current running sql query(s) in Oracle


If you are uncertain who work around with Oracle as backend, you would face the sitution what are current sql, which is executed by whom and where.

SELECT S.USERNAME,
         S.program,
         S.machine,
         s.sid,
         s.osuser,
         t.sql_id,
         sql_text,
         s.sql_trace
    FROM v$sqltext_with_newlines t, V$SESSION s
   WHERE     t.address = s.sql_address
         AND t.hash_value = s.sql_hash_value
         AND s.status = 'ACTIVE'
ORDER BY s.sid, t.piece; 


Find invalid user object(s) in Oracle


In some time stored procedure may fail because of invalid objects, that certain we want to know the complete invalid objects lists to compile.

  SELECT OWNER,
         OBJECT_TYPE,
         OBJECT_NAME,
         STATUS
    FROM dba_objects
   WHERE STATUS = 'INVALID'
ORDER BY OWNER, OBJECT_TYPE, OBJECT_NAME;


Find last modified object(s) in Oracle


Here is the sql to find last modified objects list.

  SELECT OWNER,
         OBJECT_NAME,
         OBJECT_TYPE,
         TO_CHAR (LAST_DDL_TIME, 'MM/DD/YYYY HH24:MI:SS') last_modified,
         TO_CHAR (CREATED, 'MM/DD/YYYY HH24:MI:SS') created,
         STATUS
    FROM dba_objects
   WHERE (SYSDATE - LAST_DDL_TIME) < 1
ORDER BY LAST_DDL_TIME DESC;


Find user object notes(s) in Oracle


If you are looking out to find out the user objects counts, we need to use the following script.

SELECT USERNAME,
         COUNT (DECODE (o.TYPE#, 2, o.OBJ#, '')) Tables,
         COUNT (DECODE (o.TYPE#, 1, o.OBJ#, '')) Indexes,
         COUNT (DECODE (o.TYPE#, 5, o.OBJ#, '')) Synonyms,
         COUNT (DECODE (o.TYPE#, 4, o.OBJ#, '')) Views,
         COUNT (DECODE (o.TYPE#, 6, o.OBJ#, '')) Sequences,
         COUNT (DECODE (o.TYPE#, 7, o.OBJ#, '')) Procedures,
         COUNT (DECODE (o.TYPE#, 8, o.OBJ#, '')) Functions,
         COUNT (DECODE (o.TYPE#, 9, o.OBJ#, '')) Packages,
         COUNT (DECODE (o.TYPE#, 12, o.OBJ#, '')) Trigers,
         COUNT (DECODE (o.TYPE#, 10, o.OBJ#, '')) Dependencies
    FROM sys.obj$ o, dba_users u
   WHERE u.USER_ID = o.OWNER#(+)
GROUP BY USERNAME
ORDER BY USERNAME;


Find user privileges note(s) in Oracle


Here is the sql to find user privileges list.

  SELECT rp.GRANTEE,
         GRANTED_ROLE,
         rp.ADMIN_OPTION,
         DEFAULT_ROLE,
         PRIVILEGE
    FROM dba_role_privs rp, dba_sys_privs sp
   WHERE rp.GRANTEE = sp.GRANTEE AND rp.GRANTEE NOT IN ('SYS', 'SYSTEM', 'DBA')
ORDER BY rp.GRANTEE, GRANTED_ROLE, PRIVILEGE;




Copyright - There is no copyright on the code. You can copy, change and distribute it freely. Just mentioning this site should be fair

(C) August 2008, manivelcode