LoginSignup
0
0

PostgreSQL 日本語を別名参照する際の注意

Posted at

はじめに

業務でPostgresを使用する際、別名で日本語を使用していました。
別名で日本語を使用するにあたって、特定の別名を参照する際にエラーが発生し、解決方法を調べるのに時間がかかりました...。
同様の問題に直面する方もおられるかもしれないので、解決策を残しておこうと思います!

エラー発生する場合としない場合の違い

エラー起きない場合

sample1.sql
select
    main.サンプル as "サンプル"
from
    (
        select
            sample as "サンプル"
        from
            table 
    ) main

"全角日本語"だけの場合、エラーは発生せずSQLを実行することができました。

また、"半角英字(小文字)"+"全角日本語"の場合もエラーは発生しませんでした。

sample2.sql
select
    main.サンプル as "abcサンプル"
from
    (
        select
            sample as "abcサンプル"
        from
            table 
    ) main

エラー起きた場合

sample3.sql
select
    main.ABCサンプル as "ABCサンプル"
from
    (
        select
            sample as "ABCサンプル"
        from
            table 
    ) main

"半角英字(大文字)"+"全角日本語"の組み合わせで参照した場合、エラーが発生しました。
ちなみに次のようなエラーです。

column tnegimesuryo.abcサンプル does not exist

エラーに対する対応方法

エスケープ処理を実施します。
具体的には次の通りです。

sample2.sql
select
    - main.ABCサンプル as "ABCサンプル"
    + main."ABCサンプル" as "ABCサンプル"
from
    (
        select
            sample as "ABCサンプル"
        from
            table 
    ) main

別名を参照する際、ダブルクォーテーションで囲むとエラーが発生しなくなります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0