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?

DatabricksにおけるEXECUTE IMMEDIATEのサポート

Posted at

Databricksランタイム14.3でサポートされました。

動的SQLを記述できます。

-- リテラル文字列による自己完結実行
EXECUTE IMMEDIATE 'SELECT SUM(col1) FROM VALUES(?), (?)' USING 5, 6;

Screenshot 2024-02-01 at 18.16.57.png

-- SQL変数で構成されたSQL文字列
DECLARE sqlStr = 'SELECT SUM(col1) FROM VALUES(?), (?)';
DECLARE arg1 = 5;
DECLARE arg2 = 6;
EXECUTE IMMEDIATE sqlStr USING arg1, arg2;

Screenshot 2024-02-01 at 18.17.27.png

-- INTO句を使用
DECLARE sum INT;
EXECUTE IMMEDIATE sqlStr INTO sum USING arg1, arg2;
SELECT sum;

Screenshot 2024-02-01 at 18.18.06.png

-- 名前付きパラメータマーカーを使用
SET VAR sqlStr = 'SELECT SUM(col1) FROM VALUES(:first), (:second)';
EXECUTE IMMEDIATE sqlStr INTO sum
  USING (5 AS first, arg2 AS second);
SELECT sum;

Screenshot 2024-02-01 at 18.18.37.png

私はこれまでは動的に生成していたSQL文字列をspark.sqlに渡していましたが、SQL側で結構動的に処理できるようになりますね。

はじめてのDatabricks

はじめてのDatabricks

Databricks無料トライアル

Databricks無料トライアル

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?