1
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 1 year has passed since last update.

SQLの置換変数

Posted at

置換変数とは

置換変数を使うと、入力したSQLの一部を、実行前に書き換えることができる。この機能はSQL*Plus及びSQL Developerで使用できる。
置換変数を利用するには、書き換えたい箇所に「&<変数名>」を指定する。

対話的に指定する方法

置換変数を含むSQL実行時に置換変数の値の入力を求めるプロンプトが表示されるため、これに入力する。

SQL> SELECT empno,ename,sal,deptno
  2  FROM employees
  3  WHERE deptno = &deptno;
deptnoに値を入力してください: 10
   3: WHERE deptno = &deptno
   3: WHERE deptno = 10

     EMPNO ENAME                                           SAL     DEPTNO
---------- ---------------------------------------- ---------- ----------
      1001 佐藤                                         500000         10
      1008 中村                                         245000         10
      1014 佐々木                                        230000         10

DEFINEコマンドで指定する方法

DEFINEコマンドで置換後の値をあらかじめ指定することができる。夜間などにSQLスクリプトを自動実行する場合に便利である。

SQL> DEFINE deptno = 20
SQL> SELECT empno,ename,sal,deptno
  2  FROM employees
  3  WHERE deptno = &deptno;

     EMPNO ENAME             SAL     DEPTNO
---------- ---------- ---------- ----------
      1002 鈴木           200000         20
      1005 渡辺           280000         20
      1009 小林           300000         20
      1011 加藤           110000         20
      1013 山田           280000         20 
1
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
1
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?