在postgresql函数的查询中放置变量
我试图在psql上创建一个函数。它将在表上插入时触发。我想在select上注入一个变量。无法开始工作在postgresql函数的查询中放置变量,sql,postgresql,plpgsql,Sql,Postgresql,Plpgsql,我试图在psql上创建一个函数。它将在表上插入时触发。我想在select上注入一个变量。无法开始工作 CREATE OR REPLACE FUNCTION updateHistoricLongTime() RETURNS void AS $$ DECLARE hour_nb int; index_hour int; saved_hours int; tmp_counter int; BEGIN
CREATE OR REPLACE FUNCTION updateHistoricLongTime()
RETURNS void AS $$
DECLARE
hour_nb int;
index_hour int;
saved_hours int;
tmp_counter int;
BEGIN
hour_nb := 0;
index_hour := 1;
saved_hours := 2160;
tmp_counter := 0;
SELECT COUNT(*) FROM locationhistoric WHERE type='hour' AND idLocation=6 INTO hour_nb;
IF (hour_nb<saved_hours) THEN
FOR i IN 1 .. saved_hours LOOP
SELECT COUNT(*) FROM visits
WHERE stend < (timestamp '2017-11-29 15:00' - interval **>> index_hour<<<** - 1 ' hour') AND stend > (timestamp '017-11-29 15:00' - interval **>>index_hour <<<**' hour') AND location_id=6 AND duration>0 INTO tmp_counter;
index_hour := index_hour + 1;
END LOOP;
END IF;
END;
$$
LANGUAGE 'plpgsql' IMMUTABLE;
创建或替换函数updateHistoricLongTime()
返回void作为$$
声明
小时整;
索引时间int;
节省的时间;
tmp_计数器int;
开始
小时=0;
索引时间:=1;
节省的小时数:=2160;
tmp_计数器:=0;
从LocationHistorical中选择COUNT(*),其中type='hour'和idLocation=6进入hour\u nb;
如果(hour_nb>index_hourindex_hour仅包含间隔值,如
interval concat(index_hour - 1 , ' hour')
为间隔指定的值不能作为变量传递。但是,如果基本单位始终为一小时,则可以将我们的间隔乘以所需的间隔数,例如
interval '1' hour * 5
将返回5小时。5
可以是一个参数。因此您的查询应该是:
SELECT COUNT(*)
INTO tmp_counter
FROM visits
WHERE stend < (timestamp '2017-11-29 15:00' - (interval '1' hour * index_hour))
AND stend > (timestamp '2017-11-29 15:00' - (interval '1' hour * index_hour))
AND location_id=6
AND duration > 0;
选择计数(*)
进入tmp_计数器
来自访问
其中stend<(时间戳“2017-11-29 15:00”-(间隔“1”小时*索引小时))
和stend>(时间戳“2017-11-29 15:00”-(间隔“1”小时*索引小时))
和位置_id=6
持续时间>0;
您希望为查询获取的语法(例如,其中index_hour=8)是:
使用结果值检查日期:
# select * from driver where id = a47768241();
id | vehicle_id | person_id | from_date_time | to_date_time | created_at | updated_at
-----+------------+-----------+---------------------------+---------------------------+----------------------------+------------
158 | 6784 | 15430 | 2012-09-13 17:00:41.39778 | 2012-09-14 01:54:46.39778 | 2016-06-03 16:43:11.456063 |
(1 row)
你想把什么放在哪里?。为什么不让fn()返回触发器呢?。不相关,但是:语言名称是一个标识符。不要把它放在单引号中
SELECT COUNT(*)
INTO tmp_counter
FROM visits
WHERE stend < (timestamp '2017-11-29 15:00' - (interval '1' hour * index_hour))
AND stend > (timestamp '2017-11-29 15:00' - (interval '1' hour * index_hour))
AND location_id=6
AND duration > 0;
select count(*)
from visits
where
stend < (timestamp '2017-11-29 15:00' - interval '7 hour') and
stend > (timestamp '2017-11-29 15:00' - interval '8 hour') and
location_id = 6 and
duration > 0;
execute
'select count(*) ' ||
'from visits ' ||
'where ' ||
'stend < (timestamp ''2017-11-29 15:00'' - interval ''' || (index_hour - 1) || ' hour'') and ' ||
'stend > (timestamp ''2017-11-29 15:00'' - interval ''' || index_hour || ' hour'') and ' ||
'location_id = 6 and ' ||
'duration > 0'
create function a47768241() returns integer
as $body$
declare
index_hour int;
id integer;
begin
index_hour = 8;
execute
'select id ' ||
'from driver ' ||
'where ' ||
'from_date_time < (timestamp ''2013-04-22 16:00:00'' - interval ''' || (index_hour - 1) || ' hour'') '
into id;
return id;
end;
$body$
language 'plpgsql';
# select a47768241();
a47768241
-----------
158
(1 row)
# select * from driver where id = a47768241();
id | vehicle_id | person_id | from_date_time | to_date_time | created_at | updated_at
-----+------------+-----------+---------------------------+---------------------------+----------------------------+------------
158 | 6784 | 15430 | 2012-09-13 17:00:41.39778 | 2012-09-14 01:54:46.39778 | 2016-06-03 16:43:11.456063 |
(1 row)