Set charset to UTF-8 in SQL*Plus - oracle

I have a SQL script that contains the following statement:
insert into employee(fname,lname) values('Jörg','Müller');
My Database Charset ist set to AL32UTF8 but when I execute the script in SQLPlus the german letters Ö and Ü are not saved correctly. How can I set the Charset of SQLPlus to UTF-8?
Is it possible to set the SQL*Plus Charset to UTF-8 in my script file (the sql script file contains the command to set the charset to UTF-8)?
My runtime environment is Window but script should also be able to run on unix. My oracle version is 19c.

SQL*Plus inherits the character set from cmd window. You need to set encoding to UTF-8 and tell it Oracle. This is done by NLS_LANG parameter.
Try this:
chcp 65001
set NLS_LANG=.AL32UTF8
sqlplus ....
Of course, this works only if your sql-file is also saved as UTF-8. Otherwise you need to adapt character sets from above.
See OdbcConnection returning Chinese Characters as "?"

You would typically use the environment variable NLS_LANG and for example set it like this in windows: SET NLS_LANG=AMERICAN_AMERICA.AL32UTF8

Related

Oracle sqlldr ORA-28009:connection as SYS should be as SYSDBA or SYSOPER

I try to run from DOS sqlldr with SYS/PASS#
sqlldr.exe 'sys/PASS#orcl' control="D:/test/control_file.dat" log="D:/test/log.log"
And got this error
Oracle sqlldr ORA-28009:connection as SYS should be as SYSDBA or SYSOPER
Is anyone can tell me to specify SYSDBA or SYSOPER in command line above ?
You need to add AS SYSDBA to your connection string. Try this:
sqlldr.exe 'sys/PASS#orcl AS SYSDBA' control="D:/test/control_file.dat" log="D:/test/log.log"
P.S.
NOTE from documentation:
This example shows the entire connect string enclosed in quotation marks and backslashes. This is because the string, AS SYSDBA, contains a blank, a situation for which most operating systems require that the entire connect string be placed in quotation marks or marked as a literal by some method. Some operating systems also require that quotation marks on the command line be preceded by an escape character. In this example, backslashes are used as the escape character. If the backslashes were not present, the command line parser that SQL*Loader uses would not understand the quotation marks and would remove them.
See your Oracle operating system-specific documentation for information about special and reserved characters on your system.

Oracle table column contain $

I'm currently migrating AIX to Linux. The Oracle script contains a $ in the column name. While fetching through the shell script, I set the escape character to $, but it does not work. The query is like below:
Set escape $
Select c.logoff$time from temp c;
When run from shell script I'm getting "c.logoff invalid identifier".
How can I fix this?
This isn't an SQL*Plus (I assume that's your client) problem, so the set escape isn't doing anything - that's for escaping things SQL*Plus tries to interpret - see the docs.
This is a shell issue/feature. the $time part is being treated as a shell variable, and that doesn't exist, so the final table name doesn't have it. You can escape that at shell level, referring to \$time; e.g. if you're using a heredoc:
sqlplus -s -l usr/pass#db <<EOF
select c.logoff\$time from temp c;
EOF

The oracle profile variable, PS1

After I finish to install ORACLE, then there is the step that I have to register some environment values. One of them is PS1.
export PS1=$'\\n[$LOGNAME#\h:$ORACLE_SID]'
Kind of an explanation is "User OS prompt setting variable".
I can't understand when it is used and the variable is also quite weird.
Does anyone have an idea for it?
"have to" - says who? It is not a mandatory requirement.
5.1 Bourne Shell Variables
PS1 - The primary prompt string. The default value is ‘\s-\v\$ ’. See
Controlling the Prompt, for the complete list of escape sequences that
are expanded before PS1 is displayed.
6.9 Controlling the Prompt
\n - A newline.
\h - The hostname, up to the first '.'.
LOGNAME (login name) and ORACLE_SID are custom environment variables.

How to encode properly non 7bits ASCII characters with sqlplus host echo

I some scripts I just echo some information to log program.
Unfortunately, although NLS_LANG = AMERICAN_AMERICA.UTF8 I am only able to display output with ASCII 7 bits.
sqlplus /nolog
host echo %NLS_LANG%
host echo "an OK test with ASCII 7 bit characters"
host echo "a KO test with accentuated characters : àéùôù"
Edit : it is a problem with unicode file (UTF-8)
You must set the codepage of your cmd window, since SQL*Plus inherits it.
Run chcp 65001 before you launch sqlplus.
However, there is an issue with UTF-8 on command line window, see how to use sqlplus with utf8 on windows command line: works [only] for characters [...] if the first character in a line is ASCII (code < 128, e.g. blank)
Perhaps Codepage 1252 also fulfill your needs:
chcp 1252
set NLS_LANG=.WE8MSWIN1252
sqlplus ...

Special chars from sqlplus client into AL32UTF8 oracle database

I've a SQL script with some special chars, the original encoding is WE8MSWIN1252, inside one script I have this char 'Ç'. When I execute a script with this char, my Oracle database encoding is AL32UTF8, if the client encoding is AL32UTF8 I will get a wrong char in database.
With this process, I need to adapt the environment variable each time I need to compile this script.
I'm looking for a more flexible approach, I want to have environment variable and the database set to AL32UTF8, and then adapt/convert the file from ANSI to UTF8. But I can't executed the script having the correct char in the database.
Do you know how can I do this, or configure something that is flexible to execute script with different special chars without changing the environment variable?
Thanks,
You can do this all in command line or but it into a single BAT file.
Would be like this:
C:\>chcp 1252
Active Codepage: 1252.
C:\>set NLS_LANG=.WE8MSWIN1252
C:\>sqlplus user/pwd#db #<your_sql_script_in_ANSI_encoding.sql>
C:\>exit
NLS_LANG is WE8MSWIN1252 only while you command line window is open.

Resources