我的嵌套if条件在这里不起作用。我不知道为什么。我是Postgresql的新手。你可以帮我解决这个问题吗?我不知道该怎么做。 :'(
CREATE OR REPLACE FUNCTION pl_uploadhousehoold(mchholdnumber character varying, mcbrgycode character varying, mcstio character varying, mcstreet character varying, mnhousenumber character varying, mclat character varying, mclong character varying, mcrespondentname character varying, mddateinterview date, mdstattime character varying, mdendtime character varying, mcpurok character varying, mnuserid integer, mczone character varying, mccategory character varying, mfile_path_household character varying, mfile_path character varying, mfile_respondent character varying, mdyear date, mnuser integer, mdaccomplished character varying, mdregistered character varying, mdvalidated character varying, mcsubcategory character varying, mcfacilityname character varying, mcposition character varying)
RETURNS SETOF tbl_household AS
$BODY$
DECLARE
t_10 time without time zone;
t_11 time without time zone;
t_21 timestamp without time zone;
t_22 timestamp without time zone;
t_23 timestamp without time zone;
hh_id character varying;
BEGIN
IF hh_id is NULL THEN
IF mdstattime !='' THEN
t_10:=(select CAST (mdstattime as time without time zone) as dstattime);
INSERT INTO tbl_household(chholdnumber,cbrgycode,csitio,cstreet,nhousenumber,clat,clong,crespondentname,ddateinterview,dstattime,cpurok,nuserid,czone,ccategory,file_path_household, file_path, file_respondent,dyear,nuser, csubcategory, cfacilityname, cposition)
values($1,$2,$3,$4,$5,$6,$7,$8,$9,t_10,$12,$13,$14,$15,$16,$17,$18,$19,$20,$24,$25,$26);
END IF;
IF mdendtime !='' THEN
t_11:=(select CAST (mdendtime as time without time zone) as dendtime);
INSERT INTO tbl_household(chholdnumber,cbrgycode,csitio,cstreet,nhousenumber,clat,clong,crespondentname,ddateinterview,dendtime,cpurok,nuserid,czone,ccategory,file_path_household, file_path, file_respondent,dyear,nuser, csubcategory, cfacilityname, cposition)
values($1,$2,$3,$4,$5,$6,$7,$8,$9,t_11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$24,$25,$26);
END IF;
IF mdaccomplished !='' THEN
t_21:=(select CAST (mdaccomplished as timestamp without time zone) as daccomplished);
INSERT INTO tbl_household(chholdnumber,cbrgycode,csitio,cstreet,nhousenumber,clat,clong,crespondentname,ddateinterview,cpurok,nuserid,czone,ccategory,file_path_household, file_path, file_respondent,dyear,nuser,daccomplished, csubcategory, cfacilityname, cposition)
values($1,$2,$3,$4,$5,$6,$7,$8,$9,$12,$13,$14,$15,$16,$17,$18,$19,$20,t_21,$24,$25,$26);
END IF;
IF mdregistered !='' THEN
t_22:=(select CAST (mdregistered as timestamp without time zone) as dregistered);
INSERT INTO tbl_household(chholdnumber,cbrgycode,csitio,cstreet,nhousenumber,clat,clong,crespondentname,ddateinterview,cpurok,nuserid,czone,ccategory,file_path_household, file_path, file_respondent,dyear,nuser,dregistered, csubcategory, cfacilityname, cposition)
values($1,$2,$3,$4,$5,$6,$7,$8,$9,$12,$13,$14,$15,$16,$17,$18,$19,$20,t_22,$24,$25,$26);
END IF;
IF mdvalidated !='' THEN
t_23:=(select CAST (mdvalidated as timestamp without time zone) as dvalidated);
INSERT INTO tbl_household(chholdnumber,cbrgycode,csitio,cstreet,nhousenumber,clat,clong,crespondentname,ddateinterview,cpurok,nuserid,czone,ccategory,file_path_household, file_path, file_respondent,dyear,nuser,dvalidated, csubcategory, cfacilityname, cposition)
values($1,$2,$3,$4,$5,$6,$7,$8,$9,$12,$13,$14,$15,$16,$17,$18,$19,$20,t_23,$24,$25,$26);
END IF;
IF mdaccomplished ='' and mdstattime='' and mdendtime='' and mdregistered ='' and mdvalidated ='' THEN
INSERT INTO tbl_household(chholdnumber,cbrgycode,csitio,cstreet,nhousenumber,clat,clong,crespondentname,ddateinterview,cpurok,nuserid,czone,ccategory,file_path_household, file_path, file_respondent,dyear,nuser, csubcategory, cfacilityname, cposition)
values($1,$2,$3,$4,$5,$6,$7,$8,$9,$12,$13,$14,$15,$16,$17,$18,$19,$20,$24,$25,$26);
END IF;
IF mdaccomplished !='' and mdstattime!='' and mdendtime!='' and mdregistered !='' and mdvalidated !='' THEN
t_22:=(select CAST (mdregistered as timestamp without time zone) as dregistered);
t_21:=(select CAST (mdaccomplished as timestamp without time zone) as daccomplished);
t_10:=(select CAST (mdstattime as time without time zone) as dstattime);
t_11:=(select CAST (mdendtime as time without time zone) as dendtime);
t_23:=(select CAST (mdvalidated as timestamp without time zone) as dvalidated);
INSERT INTO tbl_household(chholdnumber,cbrgycode,csitio,cstreet,nhousenumber,clat,clong,crespondentname,ddateinterview,dstattime,dendtime,cpurok,nuserid,czone,ccategory,file_path_household, file_path, file_respondent,dyear,nuser,daccomplished,dregistered,dvalidated, csubcategory, cfacilityname, cposition)
values($1,$2,$3,$4,$5,$6,$7,$8,$9,t_10,t_11,$12,$13,$14,$15,$16,$17,$18,$19,$20,t_21,t_22,t_23,$24,$25,$26);
END IF;
ELSE
END IF;
我用空值测试了我的mdendtime。但条件不起作用。
答案 0 :(得分:0)
您应该删除功能底部的最后一个ELSE
语句。
此外,在您的函数中,您使用一种非常复杂的方法将字符串转换为time
或timestamp
值,但这些都不是必需的。而不是做:
IF mdendtime != '' THEN
t_11 := (select CAST (mdendtime as time without time zone) as dendtime);
INSERT INTO tbl_household(chholdnumber, dendtime, ...)
VALUES ($1, t_11, ...);
END IF;
你可以这样做:
INSERT INTO tbl_household(chholdnumber, dendtime)
VALUES ($1, NULLIF(mdendtime, '')::time);
但是像弗兰克海肯斯所说,你的功能太复杂了。您最终可能会进行6次基本相同的数据插入。显然,您正在菲律宾进行家庭调查,并且您希望存储有关家庭的位置和一些调查数据的信息。住户和调查是两个独立的实体,在适当的数据库设计中,所有实体都有自己的表格。
首先将所有家庭数据放在表households
中。进行新调查时,请检查家庭数据是否已在表格中。如果是,请检索hholdid
,否则将数据插入households
表RETURNING hholdid
。
现在创建一个表survey
,它使用hholdid
表中的households
作为外键。在survey
表格中,您可以为所有相关信息添加列,包括hholdid
,例如调查日期,开始时间,结束时间等。然后,您只需在此处插入调查信息,单行,无需函数。
根据所有调查数据的含义,您可能希望重复将所有数据分解为实体的过程。受访者信息看起来是一个很好的候选人:单个受访者可能会进行多次调查,因此请使用respid
主键创建一个受访者表,并将该值存储在survey
表中。
对你的"话语宇宙的分析"是良好的数据库建模的核心。