我在 Postgresql 中的嵌套 IF 根本不起作用?
My nested IF in Postgresql doesn't work at all?
我的 Nested 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(,,,,,,,,,t_10,,,,,,,,,,,,);
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(,,,,,,,,,t_11,,,,,,,,,,,,);
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(,,,,,,,,,,,,,,,,,,t_21,,,);
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(,,,,,,,,,,,,,,,,,,t_22,,,);
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(,,,,,,,,,,,,,,,,,,t_23,,,);
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(,,,,,,,,,,,,,,,,,,,,);
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(,,,,,,,,,t_10,t_11,,,,,,,,,,t_21,t_22,t_23,,,);
END IF;
ELSE
END IF;
我用空值测试了我的 mdendtime。但条件不成立。
您应该删除函数底部的最后一个 ELSE
语句。
此外,在您的函数中,您使用了一种非常复杂的方法将字符串转换为 time
或 timestamp
值,但其中的 none 是必需的。而不是做:
IF mdendtime != '' THEN
t_11 := (select CAST (mdendtime as time without time zone) as dendtime);
INSERT INTO tbl_household(chholdnumber, dendtime, ...)
VALUES (, t_11, ...);
END IF;
你可以简单地做:
INSERT INTO tbl_household(chholdnumber, dendtime)
VALUES (, NULLIF(mdendtime, '')::time);
但是正如 Frank Heikens 所说,您的功能太复杂了。您最终可能会插入 6 次基本相同的数据。显然,您正在菲律宾进行家庭调查,并且希望存储有关 household 位置的信息和一些 survey 数据。家庭和调查是两个独立的实体,在适当的数据库设计中,所有实体都有自己的 table.
因此,首先将所有家庭数据放入 table households
。当你有一个新的调查时,检查家庭数据是否已经在 table 中。如果是,则检索hholdid
,否则将数据插入households
table RETURNING hholdid
.
现在创建一个 table survey
,它使用 households
table 中的 hholdid
作为外键。在 survey
table 中,您可以输入所有相关信息的列,包括 hholdid
,例如调查日期、开始时间、结束时间等。然后您可以简单地插入您的调查信息在那里,一切都在一行中,不需要功能。
根据所有调查数据的含义,您可能想要重复将所有数据分解为实体的过程。受访者信息看起来是一个不错的候选者:一个受访者可能会进行多项调查,因此使用 respid
主键创建一个受访者 table 并将该值存储在您的 survey
table.
您的 "Universe-of-Discourse" 分析是良好数据库建模的核心。
我的 Nested 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(,,,,,,,,,t_10,,,,,,,,,,,,);
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(,,,,,,,,,t_11,,,,,,,,,,,,);
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(,,,,,,,,,,,,,,,,,,t_21,,,);
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(,,,,,,,,,,,,,,,,,,t_22,,,);
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(,,,,,,,,,,,,,,,,,,t_23,,,);
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(,,,,,,,,,,,,,,,,,,,,);
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(,,,,,,,,,t_10,t_11,,,,,,,,,,t_21,t_22,t_23,,,);
END IF;
ELSE
END IF;
我用空值测试了我的 mdendtime。但条件不成立。
您应该删除函数底部的最后一个 ELSE
语句。
此外,在您的函数中,您使用了一种非常复杂的方法将字符串转换为 time
或 timestamp
值,但其中的 none 是必需的。而不是做:
IF mdendtime != '' THEN
t_11 := (select CAST (mdendtime as time without time zone) as dendtime);
INSERT INTO tbl_household(chholdnumber, dendtime, ...)
VALUES (, t_11, ...);
END IF;
你可以简单地做:
INSERT INTO tbl_household(chholdnumber, dendtime)
VALUES (, NULLIF(mdendtime, '')::time);
但是正如 Frank Heikens 所说,您的功能太复杂了。您最终可能会插入 6 次基本相同的数据。显然,您正在菲律宾进行家庭调查,并且希望存储有关 household 位置的信息和一些 survey 数据。家庭和调查是两个独立的实体,在适当的数据库设计中,所有实体都有自己的 table.
因此,首先将所有家庭数据放入 table households
。当你有一个新的调查时,检查家庭数据是否已经在 table 中。如果是,则检索hholdid
,否则将数据插入households
table RETURNING hholdid
.
现在创建一个 table survey
,它使用 households
table 中的 hholdid
作为外键。在 survey
table 中,您可以输入所有相关信息的列,包括 hholdid
,例如调查日期、开始时间、结束时间等。然后您可以简单地插入您的调查信息在那里,一切都在一行中,不需要功能。
根据所有调查数据的含义,您可能想要重复将所有数据分解为实体的过程。受访者信息看起来是一个不错的候选者:一个受访者可能会进行多项调查,因此使用 respid
主键创建一个受访者 table 并将该值存储在您的 survey
table.
您的 "Universe-of-Discourse" 分析是良好数据库建模的核心。