SQL 命令未正确结束

发布于 2024-08-06 08:51:17 字数 224 浏览 5 评论 0原文

当点击下面的这一行时,我发现 sql 命令没有正确结束。我想将表 B 中的数据插入到表 A 中。它们都有相同的列,但顺序可能不同。蒂亚!

Insert into a (select column_name from user_tab_columns where table_name = 'B')
select * from b

我正在使用 pl/sql 开发人员。

I am getting the sql command not properly ended when hiting this line below. I want to insert into table A with data from table B. Both of them have the same columns but the sequence might be different. TIA!

Insert into a (select column_name from user_tab_columns where table_name = 'B')
select * from b

I am using pl/sql developer.

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

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

发布评论

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

评论(3

眼波传意 2024-08-13 08:51:17

在 plsql 过程中使用动态 SQL。在游标 for 循环中循环遍历列名称并将它们添加到字符串中。然后执行查询。请参阅下面的代码(未测试)

declare
   l_query varchar2(32767);
   l_columns varchar2(32767);

   cursor c is select column_name
    from user_tab_columns
    where table_name=&table_name;

begin
   for r in c loop
     l_columns := l_columns ||','||r.columns_name;
   end loop;
   -- remove first ','
   l_columns := substr(l_columns,2);

   l_query := 'insert into a ('||l_columns||') select '||l_columns||' from &table_name;

   execute immediate l_query;
end;

Use dynamic SQL in a plsql procedure. Loop through the column names in a cursor for loop and add them to a string. Then execute the query. See code below (not tested)

declare
   l_query varchar2(32767);
   l_columns varchar2(32767);

   cursor c is select column_name
    from user_tab_columns
    where table_name=&table_name;

begin
   for r in c loop
     l_columns := l_columns ||','||r.columns_name;
   end loop;
   -- remove first ','
   l_columns := substr(l_columns,2);

   l_query := 'insert into a ('||l_columns||') select '||l_columns||' from &table_name;

   execute immediate l_query;
end;
好倦 2024-08-13 08:51:17

在类似的情况下,我在目标表上创建了一个视图,其中的列按适当的顺序排列。

例如,

Table A has columns (A, B, C)
Table B has columns (B, C, A)

您创建一个像这样的视图

CREATE VIEW A_V AS SELECT B,C,A FROM A;

,然后您可以执行 insert into a_v select * from b;

优点是,即使将列添加到表 a 而不是添加到表 b,只要它们可以为空或有默认值,通过视图插入仍然有效。

我自动创建了 CREATE VIEW 脚本,并在 USER_TAB_COLUMNS 中查找表 B。

In a similar situation I created a view over the destination table that had the columns in the appropriate order.

For example

Table A has columns (A, B, C)
Table B has columns (B, C, A)

You create a view like

CREATE VIEW A_V AS SELECT B,C,A FROM A;

Then you can do an insert into a_v select * from b;

The advantage is that, even if columns are added to table a but not to table b then, as long as they are nullable or have a default, the insert via the view still works.

I created the CREATE VIEW scripts automatically, looking up against USER_TAB_COLUMNS for table B.

飞烟轻若梦 2024-08-13 08:51:17

这也许可以做你想做的事。

INSERT INTO a (col1, col2)
SELECT col1, col2
FROM b

This maybe do what you want.

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