如何使用获取表复制表的名称和新表的名称的过程来复制表,以在PL/SQL Oracle中创建它?

发布于 2025-01-31 06:07:47 字数 660 浏览 2 评论 0原文

运行以下代码,

create or replace procedure copy_table(
        from_table in out varchar2,
        new_table_name in out varchar2
    ) is v varchar(4000);
begin
    v :='create table new_table_name as select * from from_table';
    execute immediate v;
end copy_table;

begin
copy_table(lalala, new_table);
end;

我收到错误

begin
copy_table(lalala, new_table);
end;
Error report -
ORA-06550: line 2, column 12:
PLS-00357: Table,View Or Sequence reference 'LALALA' not allowed in this context
ORA-06550: line 2, column 1:
PL/SQL: Statement ignored

如何正确调用该过程,因为我有“ Lalala”表? 我的程序可以用于应对现有表并创建新表格吗?还是代码错误?

Running the following code

create or replace procedure copy_table(
        from_table in out varchar2,
        new_table_name in out varchar2
    ) is v varchar(4000);
begin
    v :='create table new_table_name as select * from from_table';
    execute immediate v;
end copy_table;

begin
copy_table(lalala, new_table);
end;

I got the error

begin
copy_table(lalala, new_table);
end;
Error report -
ORA-06550: line 2, column 12:
PLS-00357: Table,View Or Sequence reference 'LALALA' not allowed in this context
ORA-06550: line 2, column 1:
PL/SQL: Statement ignored

How to correctly call the procedure since I alredy have "lalala" table?
And will my procedure work for coping the existing table and create a new one? Or the code is wrong?

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

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

发布评论

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

评论(1

谁对谁错谁最难过 2025-02-07 06:07:47

您已经将过程参数作为字符串传递,而它们必须作为变量传递。此外,您的过程参数必须仅在外而不是输入。因此,您的更新代码将是 -

create or replace procedure copy_table(
        from_table in varchar2,
        new_table_name in varchar2
    ) is v varchar(4000);
begin
    v :='create table '|| new_table_name ||' as select * from '|| from_table;
    execute immediate v;
end copy_table;

You have passed the procedure parameters as strings while they must be passed as variables. Also your procedure parameters must be IN only instead of IN OUT. So your updated code would be -

create or replace procedure copy_table(
        from_table in varchar2,
        new_table_name in varchar2
    ) is v varchar(4000);
begin
    v :='create table '|| new_table_name ||' as select * from '|| from_table;
    execute immediate v;
end copy_table;

Demo.

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