PL/SQL DDL 立即执行

发布于 2025-01-06 09:13:05 字数 1325 浏览 0 评论 0原文

ACCEPT p_username PROMPT 'Enter Username : '
ACCEPT p_password PROMPT 'Enter New Password for Username : '
VARIABLE g_output VARCHAR2(4000)
DECLARE
   CURSOR NAME IS SELECT TABLE_NAME FROM DBA_TABLES 
      WHERE OWNER LIKE '%&p_username%';
   DDL_DROP VARCHAR2(200);
BEGIN
   FOR TNAME IN NAME
   LOOP
       BEGIN
          EXECUTE IMMEDIATE 'DROP TABLE' || ' ' || TNAME.TABLE_NAME;
         :g_output := :g_output || ' ' || TNAME.TABLE_NAME;
       END;
   END LOOP;
END;
/
PRINT g_output

您好,我是 PL/SQL 新手,正在尝试编写一个脚本来删除用户的表,并最终在删除表后更改其密码。我在使用 EXECUTE IMMEDIATE 命令时遇到困难。如果我删除 EXECUTE IMMEDIATE 行,该脚本将起作用。我通过在循环内打印表名称来测试它,并得到正确的表数量及其相应的名称。

如有任何帮助,我们将不胜感激,谢谢。

编辑代码以反映建议,但仍然不起作用。遇到同样的错误。

ACCEPT p_username PROMPT 'Enter Username : '
ACCEPT p_password PROMPT 'Enter New Password for Username : '
VARIABLE g_output VARCHAR2(4000)
DECLARE
   NAME SYS_REFCURSOR;
   DDL_WORD VARCHAR2(200);
BEGIN
   OPEN NAME FOR SELECT TABLE_NAME FROM DBA_TABLES 
      WHERE OWNER LIKE '%&p_username%';
   LOOP
         FETCH NAME INTO DDL_WORD;
         EXIT WHEN NAME%NOTFOUND;
         EXECUTE IMMEDIATE 'DROP TABLE "' || DDL_WORD || '" CASCADE CONSTRAINTS';
         :g_output := :g_output || ' ' || DDL_WORD;
   END LOOP;
   CLOSE NAME;
END;
/
PRINT g_output
ACCEPT p_username PROMPT 'Enter Username : '
ACCEPT p_password PROMPT 'Enter New Password for Username : '
VARIABLE g_output VARCHAR2(4000)
DECLARE
   CURSOR NAME IS SELECT TABLE_NAME FROM DBA_TABLES 
      WHERE OWNER LIKE '%&p_username%';
   DDL_DROP VARCHAR2(200);
BEGIN
   FOR TNAME IN NAME
   LOOP
       BEGIN
          EXECUTE IMMEDIATE 'DROP TABLE' || ' ' || TNAME.TABLE_NAME;
         :g_output := :g_output || ' ' || TNAME.TABLE_NAME;
       END;
   END LOOP;
END;
/
PRINT g_output

Hello, I'm new to PL/SQL and trying to make a script to drop the user's table and ultimately change their password later after dropping their tables. I am having difficulty with the EXECUTE IMMEDIATE command. The script works if I remove the EXECUTE IMMEDIATE line. I tested it by printing the table names inside the loop and I get the right # of tables and their corresponding names.

Any help is appreciated, thanks.

Edited the code to reflect the suggestion but still didn't work. Getting the same error.

ACCEPT p_username PROMPT 'Enter Username : '
ACCEPT p_password PROMPT 'Enter New Password for Username : '
VARIABLE g_output VARCHAR2(4000)
DECLARE
   NAME SYS_REFCURSOR;
   DDL_WORD VARCHAR2(200);
BEGIN
   OPEN NAME FOR SELECT TABLE_NAME FROM DBA_TABLES 
      WHERE OWNER LIKE '%&p_username%';
   LOOP
         FETCH NAME INTO DDL_WORD;
         EXIT WHEN NAME%NOTFOUND;
         EXECUTE IMMEDIATE 'DROP TABLE "' || DDL_WORD || '" CASCADE CONSTRAINTS';
         :g_output := :g_output || ' ' || DDL_WORD;
   END LOOP;
   CLOSE NAME;
END;
/
PRINT g_output

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

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

发布评论

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

评论(2

巡山小妖精 2025-01-13 09:13:05

您可能需要在 DROP 语句中指定表的所有者:

ACCEPT p_username PROMPT 'Enter Username : '
ACCEPT p_password PROMPT 'Enter New Password for Username : '
VARIABLE g_output VARCHAR2(4000)
DECLARE
   CURSOR NAME IS SELECT OWNER, TABLE_NAME FROM DBA_TABLES 
      WHERE OWNER LIKE '%&p_username%';
   DDL_DROP VARCHAR2(200);
BEGIN
   FOR TNAME IN NAME
   LOOP
       BEGIN
          EXECUTE IMMEDIATE 'DROP TABLE' || ' ' || TNAME.OWNER || '.' || TNAME.TABLE_NAME;
         :g_output := :g_output || ' ' || TNAME.OWNER || '.' || TNAME.TABLE_NAME;
       END;
   END LOOP;
END;
/
PRINT g_output

You probably need to specify the owner for the table in the DROP statement:

ACCEPT p_username PROMPT 'Enter Username : '
ACCEPT p_password PROMPT 'Enter New Password for Username : '
VARIABLE g_output VARCHAR2(4000)
DECLARE
   CURSOR NAME IS SELECT OWNER, TABLE_NAME FROM DBA_TABLES 
      WHERE OWNER LIKE '%&p_username%';
   DDL_DROP VARCHAR2(200);
BEGIN
   FOR TNAME IN NAME
   LOOP
       BEGIN
          EXECUTE IMMEDIATE 'DROP TABLE' || ' ' || TNAME.OWNER || '.' || TNAME.TABLE_NAME;
         :g_output := :g_output || ' ' || TNAME.OWNER || '.' || TNAME.TABLE_NAME;
       END;
   END LOOP;
END;
/
PRINT g_output
甜警司 2025-01-13 09:13:05

代码看起来不错。

你可以尝试这样的 ()

BEGIN

 EXECUTE IMMEDIATE (code_text);

END;

你可以尝试

 c SYS_REFCURSOR;
BEGIN
  OPEN c FOR 'SELECT * FROM table';
  CLOSE c;
END;

The code looks fine.

You could try with () like this

BEGIN

 EXECUTE IMMEDIATE (code_text);

END;

You could try

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