我正在尝试使用for ... loop将数据从一个表插入到postgres中的另一个表中。该方法在下面给出。
DO LANGUAGE PLPGSQL $$
DECLARE
data record;
BEGIN
FOR data IN SELECT * FROM forall_data
LOOP
INSERT INTO for_loop values data;<br>
END LOOP;
END;
$$
我已经在行迭代中使用了记录,但是找不到如何将“数据”插入“ for_loop”表中。当我运行此代码时,它给了我以下错误:
ERROR: syntax error at or near "data"
LINE 9: INSERT INTO for_loop values data;
^
这是我的两张桌子。
create table forall_data(
nid numeric(15,0)not null,
name varchar(15) not null,
city varchar(10) not null,
contact numeric(11,0) not null
);
create table for_loop(
nid numeric(15,0)not null,
name varchar(15) not null,
city varchar(10) not null,
contact numeric(11,0) not null
);
我在这里应该怎么做才能将该“数据”记录插入“ for_loop”表中?提前致谢。
使用此代码:
DO LANGUAGE PLPGSQL $$
DECLARE
rec record;
BEGIN
FOR rec IN SELECT * FROM budzet.forall_data
LOOP
INSERT INTO budzet.for_loop(nid, name , city , contact)
VALUES (rec.nid, rec.name , rec.city , rec.contact);
END LOOP;
END;
$$
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句