problem with Pl/Pgsql function

Поиск
Список
Период
Сортировка
Искать

problem with Pl/Pgsql function

От:
"Matteo Centenaro" <gente_che_mixa@hotmail.com>
Дата:
Hi I have a problem with this function:

Declare
rec_struttura record;rec_camp record;prov int;estra_capo int;id_lista int;estra_non int;rand int8;count int;count_estra int;count_id int;rand_doub float8;row int8;occ boolean;note varchar;pre char(4) /*NOT NULL := '02'*/;tel char(13) /*NOT NULL := '342522'*/;com int;citta char(35);nome varchar;num int4;data_oggi date;capo int;cap char(5);capo_bool boolean;

Begin
For rec_struttura in Select * From struttura_campione Loop
	/* estraggo i dati della provincia considerata */
	prov := rec_struttura.cod_prov;	estra_capo := rec_struttura.num_capo;	estra_non := rec_struttura.num_non_capo;
	/* estrazione dei nominativi nel capoluogo */	capo := 1;	count_estra := 0;
		/* modificato andrea*/		<>		while (count_estra < estra_capo) Loop
		count_estra := count_estra + 1;

		/*modificato andrea*/		/*Select into rand PERFORM random ();*/		/* in ogni caso bisogna rimappre rand da o al ROW_COUNT della select, 
immagino che così vada piu'			veloce il tutto anche perche' rand arriva fino a 2^31-1 ;) */
		rand_doub:= random() ;         count := 0;
		/* Questa select quante volte viene ripetuta? tante volte quanti sono i 
soggetti da estrarre? */
		For rec_camp in Select 
note,prefisso,telefono1,cod_com,cod_prov,citta,nome_cogno,capoluo,estrazione,num_estra,cap 
From nominativi inner join comuni on citta = nome_com Where ((cod_prov = 
prov) and (capoluo=capo)) Loop
			/* bisogna trovare un modo piu' figo per beccare il record				altrimenti fa troppi giri dentro sti cicli */			GET DIAGNOSTICS row = ROW_COUNT;			rand := round (rand_doub * row);
			if (rand > row) then
				/*questo l'ho messo perche' altrimenti alla fine ne estrae meno di 
quelli che servono*/
				exit caso;			end if;
			count := count + 1;			if count = rand then					/* modificato andrea, prima la condizione "not occ" */				if not rec_camp.occupato then						note := rec_camp.note;						pre := rec_camp.prefisso;						tel := rec_camp.telefono1;						if pre IS NULL or tel IS NULL then							exit caso;						end if;						com := rec_camp.cod_com;						citta := rec_camp.citta;						cap := rec_camp.cap						nome := rec_camp.nome_cogno;						num := rec_camp.num_estra;						capo := rec_camp.capoluo;						exit;
				else count_estra := count_estra -1; exit caso;				end if;			end if;
		End Loop; /* end del for*/

		/* estraggo i dati dal record prescelto */		/*note := rec_camp.note;		pre := rec_camp.prefisso;		tel := rec_camp.telefono1;		com := rec_camp.cod_com;		citta := rec_camp.citta;		nome := rec_camp.nome_cogno;*/
		/* faccio l'update di occupato */
		/*num := rec_camp.num_estra;*/		num := num + 1;		data_oggi := now();
		update nominativi set occupato = TRUE, num_estra = num, estrazione = 
data_oggi Where ((prefisso = pre) and (telefono1 = tel));
		/* faccio l'insert su tab_estrazioni */
		id_lista := nextval('tab_estrazioni_id_seq');		insert into tab_estrazioni values (id_lista, pre, tel);		Get Diagnostics count_id = RESULT_OID;
		/* prendo l'ultimo id utilizzata */
		/*Select into id_lista id From tab_estrazioni Where oid = count_id;*/

		/* inserisco il record nella tabella campione_out */
		if capo = 1 then			capo_bool := True;		else capo_bool := False;		end if;
		insert into campione_out values 
(note,pre,tel,cap,com,prov,citta,nome,capo_bool,NULL,NULL,id_lista);
	End Loop; /* end del while*/
End Loop; /*del primo FOR ? */
return count_id;

End;


The error riported is:

Fail to add null value in not null attribute prefisso

The line which the error refere is:

id_lista := nextval('tab_estrazioni_id_seq');
insert into tab_estrazioni values (id_lista, pre, tel);

Has Anybody any suggest? Thanks!
_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.


Re: problem with Pl/Pgsql function

От:
"Josh Berkus" <josh@agliodbs.com>
Дата:
Matteo,

> The error riported is:
> 
> Fail to add null value in not null attribute prefisso
> 
> The line which the error refere is:
> 
> id_lista := nextval('tab_estrazioni_id_seq');
> insert into tab_estrazioni values (id_lista, pre, tel);
> 
> Has Anybody any suggest? Thanks!

While my Italian isn't really good enough to analyze your function, the
above error seems simple.  tab_estrazioni probably has the field
prefisso, which is defined as NOT NULL with no default.  In the above
code, you're trying to add a record to tab_estrazioni filling in only
the columns id_lista, pre, and tel.  Without a default or a supplied
value, this inserts NULL into the prefisso column, which the database
rejects.

Thus the solution is to either: a) Change prefisso to NULL, b) give
prefisso a default, or c) supply a value for prefisso.  Unfortunatley,
the first two require dropping and re-creating the table.

-Josh Berkus


______AGLIO DATABASE SOLUTIONS___________________________                                      Josh Berkus Complete information technology      josh@agliodbs.com  and data management solutions       (415) 565-7293 for law firms, small businesses        fax 621-2533   and non-profit organizations.      San Francisco

FAQ