Showing posts with label Oracle. Show all posts
Showing posts with label Oracle. Show all posts

Friday, 9 February 2024

Oracle - Handling Exceptions to Oracle Queries

In Oracle DB, if we already have data in the table and would like to use exceptions to handle errors, please follow below approach:
declare
 e_col_exists exception;
 pragma exception_init(e_col_exists,-1430);
 e_invalid_identifier exception;
 pragma exception_init(e_invalid_identifier,-904);
begin
  begin
    execute immediate 'alter table tbl_questions add value_new varchar2(4000 CHAR) null';
    exception
      when e_col_exists then
        null;
  end;
  begin
    execute immediate 'update tbl_questions set value_new = value';
  end;
  begin
    execute immediate 'alter table tbl_questions drop column value';
    exception
      when e_invalid_identifier then
        null;
  end;
  begin
    execute immediate 'alter table tbl_questions rename column value_new to value';
    exception
      when e_invalid_identifier then
        null;
  end;
end;  
/
------
Explanation:

1. e_col_exists exception:

  • This exception is likely used to handle situations where a column referenced in your code doesn't actually exist in the table you're trying to access.

  • The pragma exception_init line associates the exception with the error code -1430. This error code typically corresponds to the ORA-01430 error message, which indicates "column does not exist".

2. e_invalid_identifier exception:

  • This exception is likely used to handle situations where an identifier (e.g., a variable name, column name, etc.) used in your code is invalid or doesn't follow the naming conventions.

  • The pragma exception_init line associates the exception with the error code -904. This error code typically corresponds to the ORA-00904 error message, which indicates "invalid identifier".

By defining these custom exceptions, you can make your code more robust and easier to maintain. When either of these exceptions is raised, your code can handle the error gracefully instead of crashing or producing unexpected results.

Here are some additional points to note:

  • You can define custom exceptions to handle any specific error conditions you want to anticipate in your code.
  • It's generally considered good practice to define custom exceptions for situations that are specific to your application logic and not already covered by standard Oracle error codes.
  • You can use exception handlers to trap these custom exceptions and take appropriate actions when they occur.

I hope this explanation helps! Feel free to ask if you have any other questions.

Wednesday, 27 January 2016

Connect to PHP with Oracle database via configuring Oracle Instant Client for Linux and Windows

PS: Create the Databases on Oracle after installing the Oracle Express Database XE 11.2, then follow the below step to enable OCI to connect to PHP.

1. Open the php.ini and uncomment the below line.
extension=php_oci8_11g.dll ; Use with Oracle 11gR2 Instant Client

2. Download the php_oci8_11g.dll files from the link below based on your version, find the "Thread Safe (TS) x86" version.

https://pecl.php.net/package/oci8/2.0.8/windows

Then, unzip the files and copy to the php/ext/ folder.

3. Next, download the "Instant Client Package - Basic" for Windows from the Oracle Instant Client page.
Because PHP is 32 bit, download the 32 bit version of Instant Client from here.

Package Name
Instant Client Package - Basic: All files required to run OCI, OCCI, and JDBC-OCI applications 
Download instantclient-basic-nt-11.2.0.4.0.zip (51,477,933 bytes)

Once downloaded, unzip the Instant Client files to C:\instantclient_11_2

4. Then, edit the Windows PATH environment setting and add C:\instantclient_11_2.
For example, on Windows XP, follow Start -> Control Panel -> System -> Advanced -> Environment Variables and edit PATH in the System Variables list.

Normally, you need to reboot Windows so the new environment is correctly set.

5. If does not work for some reason, please check the files path and location once again.

6. Also, if still does not work, you may try download this package "Microsoft Visual C++ 2012 SP1 Redistributable Package (x64)" and install it.
 (I am not sure when we would require to install it, try without it first as it may not require on your case).

Find more information for Linux and other stuff here...