动态Sql,绑定问题

时间:2010-11-16 16:17:15

标签: plsql dynamic-sql

我有个街区:

DECLARE
  stmnt VARCHAR2(100);
  rol VARCHAR2(10); --role name
  tab_name VARCHAR2(10); --table name
BEGIN
  rol := '&Role_name';
  stmnt := 'create role ' || rol;
  EXECUTE IMMEDIATE stmnt;
  stmnt := 'grant :p on ' || '&tab_name' || ' to ' || rol;
  EXECUTE IMMEDIATE stmnt using '&Privilege';
END;

当我执行此块时,在获得SELECT权限后,Oracle会给我一个错误或权限无效的错误ORA-00990: missing or invalid privilege

为什么它不绑定变量?

1 个答案:

答案 0 :(得分:2)

您无法绑定Oracle名称,只能绑定数据值。这样做:

DECLARE
  stmnt VARCHAR2(100);
  rol VARCHAR2(10); --role name
  tab_name VARCHAR2(10); --table name
BEGIN
  rol := '&Role_name';
  stmnt := 'create role ' || rol;
  EXECUTE IMMEDIATE stmnt;
  stmnt := 'grant &Privilege. on &tab_name. to ' || rol;
  EXECUTE IMMEDIATE stmnt;
END;