create table allocazione ( inserzionista varchar(16), spazioPubblicitario varchar(5), dataAllocazione datetime, dataRimozione datetime, tipoPubblicita varchar(20) check (tipoPubblicita IN ('commerciale','industriale','noprofit','altro')) DEFAULT('commerciale'), primary key (inserzionista,spazioPubblicitario,dataAllocazione), constraint diversi check (dataAllocazione < dataRimozione), foreign key (inserzionista) references inserzionisti(CodiceFiscale) on delete cascade on update cascade, foreign key (spazioPubblicitario) references SpazioPubblicitario(Codice) on delete no action on update cascade ) INSERT INTO allocazione VALUES('bdgcgdwer234hd89','aa123','03/05/2002','03/10/2002','commerciale'); INSERT INTO allocazione VALUES('bdgcgdwer234hd89','as777','04/10/2002','03/05/2003','industriale'); INSERT INTO allocazione VALUES('bdgcgdwer234hd89','cd533','01/07/2003','10/03/2003','industriale'); INSERT INTO allocazione VALUES('mdldoeeer234hd89','aa123','08/11/2002','03/05/2003','noprofit'); INSERT INTO allocazione VALUES('mdldoeeer234hd89','as777','03/08/2003','03/12/2003','noprofit'); create procedure SpazioPer @codSP varchar(5), @codI varchar(16), @data datetime as DECLARE @Cognome VARCHAR(30) DECLARE @nome VARCHAR(30) if (exists (select * from allocazione where spazioPubblicitario=@codSP and @data between dataAllocazione and dataRimozione)) begin DECLARE c CURSOR FOR select cognome, nome from inserzionisti, allocazione where inserzionista=codiceFiscale and spazioPubblicitario= @codSP and tipoPubblicita = 'commerciale' OPEN c FETCH NEXT FROM c INTO @Cognome, @Nome WHILE (@@FETCH_STATUS = 0) BEGIN PRINT 'Cognome: ' + @Cognome + ' Nome: ' + @Nome FETCH NEXT FROM c INTO @Cognome, @Nome END CLOSE c DEALLOCATE c end else begin insert into allocazione values (@codI,@codSP,@data,@data+10,'noprofit'); update inserzionisti set budget = budget - 10* (select costoGiornaliero from spazioPubblicitario where codice=@codSP) where codiceFiscale=@codI end GO exec spazioPer @codSP='as777', @codI='bdgcgdwer234hd89', @data='04/10/2002' GO