怎样在oracle用变量名传递表名进行查询?这里面我想删除一些过期的没用的费表.
1.set serveroutput on;
2.declare
3.DropTableName sys.dba_objects%rowtype;
4.Cursor Object_Name is select OBJECT_NAME FROM sys.dba_objects where 5.object_type in('TABLE') and
6.owner='SYSTEM' and created>sysdate-2;
7.begin
8.for cnt_var in Object_Name
9.loop
10.fetch Object_Name into DropTableName.OBJECT_NAME;
*11.drop table DropTableName.OBJECT_NAME;
12.end loop;
13.end;
/
执行上面的存储过程的时候,出现下面的错误.
DROP TABBLE DropTableName.OBJECT_NAME;
*
ERROR 位于第 9 行:
ORA-06550: 第 9 行, 第 1 列:
PLS-00103: 出现符号 "DROP"在需要下列之一时:
begin case declare end
exit for goto if loop mod null pragma raise return select
update while with
1<an identifier="">
2<a delimited-identifier="" double-quoted=""> <a bind="" variable=""> <<
3close current delete fetch lock insert open rollback
4savepoint set sql execute commit forall merge
5<a single-quoted="" sql="" string=""> pipe
6符号 "declare在 "DROP" 继续之前已插入。
7ORA-06550: 第 10 行, 第 1 列:
8PLS-00103: 出现符号 "END"在需要下列之一时:
9begin function package
10pragma procedure subtype type use <an identifier="">
11<a delimited-identifier="" double-quoted=""> form current cursor
12如将第*11句改为 dbms_output. put_line(DropTableName.OBJECT.NAME)程序执行正确.oracle 不能用变量传递表名?请教!急急,在线等待!!!
13
14\---------------------------------------------------------------
15
16drop table DropTableName.OBJECT_NAME;
17改成
18execute immediate 'drop table ' ¦ &brvbarDropTableName.OBJECT_NAME;
19
20\---------------------------------------------------------------
21
22直接执行是不行的,用下面的试试:
23
24SQL_STR = 'drop table ' ¦ ¦ DropTableName.OBJECT_NAME ;
25EXECUTE IMMEDATE SQL_STR;
26
27
28\---------------------------------------------------------------
29
30用oracle的内部存储过程包dbms_sql构造sql,然后执行。见下面的例子(摘自sql programing)
31
32PROCEDURE drop_object
33(object_type_in IN VARCHAR2, object_name_in IN VARCHAR2)
34IS
35cursor_id INTEGER;
36BEGIN
37/*
38¦ ¦ Open a cursor which will handle the dynamic SQL statement.
39¦ ¦ The function returns the pointer to that cursor.
40*/
41cursor_id := DBMS_SQL.OPEN_CURSOR;
42/*
43¦ ¦ Parse and execute the drop command which is formed through
44¦ ¦ concatenation of the arguments.
45*/
46DBMS_SQL.PARSE
47(cursor_id,
48'DROP ' ¦ ¦ object_type_in ¦ ¦ ' ' ¦ ¦ object_name_in,
49DBMS_SQL.NATIVE);
50/* Close the cursor. */
51DBMS_SQL.CLOSE_CURSOR (cursor_id);
52EXCEPTION
53/* If any problem arises, also make sure the cursor is closed. */
54WHEN OTHERS
55THEN
56DBMS_SQL.CLOSE_CURSOR (cursor_id);
57END;</a></an></a></a></a></an>