说我有一个选择陈述。
SELECT sensorname, starttime from sensors where id = ?;
例如返回:
Value1 | 123456789
Value1 | 987465465
(starttime是时间戳)
现在我想在Postgres中创建一个StoredProcedure。
someProcedure(argumentId){
Result r = SELECT sensorname, starttime from sensors where id = ?;
}
然后它应该遍历resultSet,获取结果并将它们插入到另一个表中,最后从原始表中删除旧的。
使用storedProcedure可以实现吗?
编辑:我需要一个存储过程。
类似的东西:
CREATE OR REPLACE FUNCTIONrollupMinutes(id bigint) RETURNS void AS $$
BEGIN
var qry_rsult = SELECT sensorname, starttime from sensors where id = id;
insert into rollup(qry_result)
END;
$$ LANGUAGE plpgsql;
答案 0 :(得分:1)
您可以使用下面的CTE(仅当您想要避免某项功能时)
WITH cte (id)
AS (
INSERT INTO another_table (sensorname,starttime)
SELECT sensorname
,starttime
FROM sensors WHERE id = id
returning id;
)
DELETE
FROM
sensors
WHERE id IN (SELECT *FROM cte);
OR
通过创建一个函数,它可以像
create or replace function fn(id int) returns void as
$$
insert into another_table(sensorname,starttime)
SELECT sensorname, starttime from sensors where id =id;
delete from sensors where id =id;
$$
language sql
用法:
select fn(12)