oracle中动态创建并执行sql命令

发布于 2024-08-22 03:15:33 字数 434 浏览 6 评论 0原文

我正在学习数据库课程,在课程的实验部分开始时,我们通常必须删除之前创建的数据库中的所有表。我希望能够运行一个动态执行此操作的脚本,但似乎无法让它工作。这是我到目前为止的代码。

declare tname string(50);
cursor ctable is select table_name from user_tables;

begin
 open ctable;
 LOOP
   FETCH ctable into tname;
   if tname != ''  then
     execute immediate 'drop table ' || tname;
   END if;
   EXIT WHEN ctable%NOTFOUND;
 END LOOP;
 close ctable;
end;

如果有人能指出我做错了什么的正确方向,那就太好了。谢谢。

I am taking a database class and at the beginning of the lab section of the class we usually have to drop all the tables in the database created previously. I wanted to be able to run a script that does this dynamically, but cannot seem to get it to work. Here is the code I have so far.

declare tname string(50);
cursor ctable is select table_name from user_tables;

begin
 open ctable;
 LOOP
   FETCH ctable into tname;
   if tname != ''  then
     execute immediate 'drop table ' || tname;
   END if;
   EXIT WHEN ctable%NOTFOUND;
 END LOOP;
 close ctable;
end;

If someone could point me in the right direction as to what I am doing wrong that would great. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北笙凉宸 2024-08-29 03:15:33

Oracle 的 VARCHAR2 将空字符串视为 NULL
因此

if tname != '' then

相同

if tname != NULL then

与返回 NULL 而不是 TRUE > 因为它没有定义。

您可以通过tname IS NOT NULL检查NULL

不过,table_nameuser_tables 中是强制性的,因此不需要进行此检查。


还有两件事:

  1. 在获取后立即检查 %NOTFOUND
  2. 如果可能的话,使用变量声明的列引用 (user_tables.table_name%TYPE)

的代码可能如下所示:

DECLARE
  tname user_tables.table_name%TYPE;
  CURSOR ctable IS SELECT table_name FROM user_tables;
BEGIN
  OPEN ctable;
  LOOP
    FETCH ctable INTO tname;
    EXIT WHEN ctable%NOTFOUND;
    EXECUTE IMMEDIATE 'drop table ' || tname;
  END LOOP;
  CLOSE ctable;
END;

所以您 还可以使用隐式游标以获得更好的可读性:

BEGIN
  FOR cur IN ( SELECT table_name FROM user_tables ) LOOP
    EXECUTE IMMEDIATE 'drop table ' || cur.table_name;
  END LOOP;
END;

Oracle's VARCHAR2 treats empty strings as NULL.
So

if tname != '' then

is the same as

if tname != NULL then

which will return NULL instead of TRUE since it is not defined.

You can check for NULL by tname IS NOT NULL.

table_name is mandatory in user_tables though, so there is no need for this check.


Two more things:

  1. Check for %NOTFOUND immediately after fetching
  2. Use column-references for variable declarations if possible ( user_tables.table_name%TYPE)

So your code could look like that:

DECLARE
  tname user_tables.table_name%TYPE;
  CURSOR ctable IS SELECT table_name FROM user_tables;
BEGIN
  OPEN ctable;
  LOOP
    FETCH ctable INTO tname;
    EXIT WHEN ctable%NOTFOUND;
    EXECUTE IMMEDIATE 'drop table ' || tname;
  END LOOP;
  CLOSE ctable;
END;

You could also use an implicit cursor for better readability:

BEGIN
  FOR cur IN ( SELECT table_name FROM user_tables ) LOOP
    EXECUTE IMMEDIATE 'drop table ' || cur.table_name;
  END LOOP;
END;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文