将值添加到PostgreSQL 9.3中现有表中新插入的列

时间:2017-02-16 12:11:31

标签: postgresql psql

创建了一个名为“collegetable”的表:

 create table collegetable (stid integer primary key not null,stname
 varchar(50),department varchar(10),dateofjoin date);

为每列提供了值:collegetable data

在其中插入一个名为“cgpa”的新列,并尝试使用以下代码一次性为该列添加值:

WITH col(stid, cgpa) as 
(    VALUES((1121,8.01),
            (1131,7.12),
            (1141,9.86))
)        
UPDATE collegetable as colldata
SET cgpa = col.cgpa
FROM col
WHERE colldata.stid = col.stid;

并收到错误:

ERROR:operator does not exist:integer=record 
LINE9:where colldata.stid=col.stid; 
HINT:No operator matches the given name and arguement type.you might need to add explicit type casts.

请提前帮助解决问题。

1 个答案:

答案 0 :(得分:0)

with子句仅定义列的名称数据类型:

with col (stid, cgpa) as (
  ...
)
update ...;

有关详细信息,请参阅tutorialfull reference