0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SQLで想定外の値が設定されていた時にエラーを出力する

Last updated at Posted at 2024-12-24

SELECTでcase文を使用して「特定カラムの値の判定結果」を取得するクエリにおいて、その特定カラムに不正な値が設定されている場合にエラーを発生させたい状況があった。

少し強引なやり方ではあるが、以下のようにすることで不正な場合にエラーと表示することができる。

テーブル名:test_table

name boolean
sato true
taro false
12345 aiueo
select 
    *,
    case boolean
    when 'true' then 'myouji'
    when 'false' then 'namae'
    else convert(varchar,convert(int,'error boolean'))
    END AS boo
from 
  test_table;

SQLを実行すると以下のようなエラーとなる。
image.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?