DECLARE -- 变量名 类型; v_name VARCHAR(16); BEGIN -- do something END;
变量赋值
1 2 3 4 5 6 7
BEGIN -- 需要使用 := v_name := 'tony'; -- 不能使用子查询 select name into v_name from user; END;
执行 ALTER 操作
直接使用会出现异常:
1 2 3 4 5 6 7 8
PLS-00103: Encountered the symbol "ALTER" when expecting one of the following:
( begin case declare end exception exit for goto if loop mod null pragma raise return select update while with <an identifier> <a double-quoted delimited-identifier> <a bind variable> << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge
需要使用立即执行命令:
1 2 3 4
EXECUTE IMMEDIATE 'a SQL'; -- 若需要变量,则需要使用字符串拼接 EXECUTE IMMEDIATE 'a SQL' || v_name;