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?

More than 5 years have passed since last update.

SQLSERVER - 削除処理 - エラーを発生させる

Posted at

RAISEERROR() やタイムアウト系のエラー発生方法だとダメなとき

 削除処理

テーブルがないとき

DEL.SQL
BEGIN TRY
    DELETE TABLE toDeleteTable
END TRY
BEGIN CATCH
    RETURN 1
END CATCH

出力:コンパイルエラー
テーブル自体を削除するとコンパイルエラーしてBEGIN CATCH内に入らないのでダメ

 実装方法

テーブルがないとき

del.sql
BEGIN TRY
    DECLARE @SQL VARCHAR(2000)
    SET @SQL = 'DELETE TABLE toDeleteTable'
    EXEC (@SQL)
END TRY
BEGIN CATCH
    RETURN 1
END CATCH

出力:-1
CATCH内に入るようになった
なるほどね

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?