Skip to Main Content
  • Questions
  • Unicode error strings with PL/SQL exception handling

Breadcrumb

Question and Answer

Sergiusz Wolicki

Thanks for the question, Mark.

Asked: October 09, 2020 - 11:06 pm UTC

Last updated: October 13, 2020 - 2:44 am UTC

Version: 19.6.0.0.0

Viewed 1000+ times

You Asked

Hi,

I would like to introduce the ability to send user-friendly error messages to the client from my PL/SQL API exceptions in the local language of the user. For this I plan to use the standard routine "raise_application_error". When I try this, the Unicode message that gets passed into the "raise_application_error" call does not always get displayed properly in the client. I have tested this with SQL*Plus and SQL Developer on a Windows 10 machine and it appears that there is some conversion taking place.

Environment information is as follows:

The database character set is AL32UTF8.
The national character set is AL16UTF16.
The NLS_LANG environment variable of the Windows client is AL32UTF8.
The database version is 19.6.0.0.0 on Linux x86-64.

The code I am using to test is as follows:

DECLARE
  h varchar2(100 char) := 'Proszę mi pomóc rozwiązać ten problem Unicode';
BEGIN
  SYS.DBMS_OUTPUT.PUT_LINE(h); 
  SYS.DBMS_STANDARD.RAISE_APPLICATION_ERROR(-20000, h); 
END;
/


Upon execution of the above code, I observe the following behaviour:

1) The output message from "raise_application_error" is being converted incorrectly within SQL developer and SQL*Plus.
2) The call to "dbms_output" does display a Unicode string correctly within SQL developer but not SQL*Plus.
3) Using the national data types makes no difference.

Can you help?

Many thanks,
Mark.

and Connor said...

There is nothing inherently wrong in the raise_application_error routine that stops this. For example:

UTF8_ERROR

But that worked only because

a) I ran "chcp 65001" in this MS-DOS command before I started SQL Plus
b) I saved the file as UTF8 and ran that (simply cut/paste doesn't work because its not UTF encoding by default on my machine)

Bottom line - there's a lot of moving parts that you have to get right here

- the database has to support it (my db is running the default UTF8 characterset)
- the client NLS settings must be correct
- the client OS settings must be alignment

My guess its the codepage you'll need to change

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Perfectly explained

Mark, October 12, 2020 - 12:21 pm UTC

Thanks so much for the quick reply. Two more related questions if I may.

1) If the database character set is subsequently changed from an 8-bit character set to Unicode (AL32UTF8), will all built-in SQL and PL/SQL string functions be automatically multi-byte character aware?

2) I believe that "raise_application_error" has a limit on the length of the message parameter. I looked at the documentation and it mentions "2048" but it doesn't qualify whether this is bytes or characters. Please could you clarify?

Many thanks,
Mark.
Sergiusz Wolicki
October 13, 2020 - 2:44 am UTC

1) All standard string processing in SQL and PL/SQL is character-based and based on the character set of the given environment: DB client, database, external interface like UTL_FILE or UTL_HTTP. Hence, yes, SQL and PL/SQL string functions are automatically multi-byte character aware in an AL32UTF8 database.

2) The 19c (at least) documentation ( https://docs.oracle.com/en/database/oracle/oracle-database/19/lnpls/plsql-error-handling.html#GUID-48F88C61-8CE9-4821-91CB-48A8F1BC09E1 ) specifies that the limit is 2048 bytes.

More to Explore

Administration

Need more information on Administration? Check out the Administrators guide for the Oracle Database