简体   繁体   English

sql检查对象是否在使用中

[英]sql check if object is in use trigger

I have a table lets say tableA already filled with var_code and start_time and end_time and I want to insert a value into another table but before I insert it I want to check if this var_code is already in use in a specific time defined in tableA. 我有一个表,可以说tableA已经充满了var_code以及start_time和end_time,我想在另一个表中插入一个值,但是在我插入它之前,我想检查该var_code是否已在tableA中定义的特定时间内使用。 To find the relation between the new object and time I had to join three tables. 为了找到新对象和时间之间的关系,我必须连接三个表。 When I try to insert something I get an error message: ERROR: query has no destination for result data What do I do wrong? 当我尝试插入内容时,出现错误消息:错误:查询没有结果数据的目的地我该怎么办?

CREATE OR REPLACE FUNCTION same_obj_does_not_already_used()
RETURNS trigger AS
$BODY$
BEGIN
declare

    var1 numeric;  
    var2 date;  
    var3 time without time zone;  
    var4 time without time zone;  
    begin
select id as var1,date as var2,start_t as var3,end_t as var4
from tableA tA inner join tableB tB 
        on (tA.id = tB.id) 
        inner join tableC tC
         on (tB.code = tC.fcode);

 if(new.id = var1 and new.date = var2 and new.start_t>=var3 and new.end_t<= var4) then
    raise exception 'Error';
end if;

 end;
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION ame_obj_does_not_already_used()
OWNER TO postgres;

i think this part 我认为这部分

select id as var1,date as var2,start_t as var3,end_t as var4
from tableA tA inner join tableB tB 
        on (tA.id = tB.id) 
        inner join tableC tC
         on (tB.code = tC.fcode);

should have INTO clauses.. 应该有INTO子句。

select id, date, start_t, end_t INTO var1, var2, var3, var4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM