275 | | select p_id into v_old_station from crime_case where c_id = v_case_id; |
276 | | |
277 | | select pe_id into v_new_officer from police_station where p_id = v_new_station; |
278 | | |
279 | | update crime_case |
280 | | set p_id = v_new_station |
281 | | where c_id = v_case_id; |
282 | | |
283 | | insert into statements (s_id, statement_date, description, incident_timestamp, incident_place, c_id, pe_id, victim_pe_id, witness_pe_id) |
284 | | select coalesce(MAX(s_id), 0) + 1, CURRENT_DATE, 'Случајот е префрлен од станица ' || v_old_station || ' во станица ' || v_new_station, CURRENT_TIMESTAMP, 'Префрлување на случај', v_case_id, (select pe_id from policeman where p_id = v_new_station limit 1), 2, 3 |
285 | | from statements; |
286 | | |
287 | | raise notice 'Случајот со ID % е префрлен од полициска станица % во полициска станица %', v_case_id, v_old_station, v_new_station; |
288 | | |
289 | | exception |
290 | | when others then |
291 | | raise exception 'Transaction failed: %', SQLERRM; |
| 280 | begin |
| 281 | select p_id INTO v_old_station |
| 282 | from crime_case |
| 283 | where c_id = p_case_id; |
| 284 | |
| 285 | if not found then |
| 286 | raise exception 'Слулајот со ID % не е пронајден', p_case_id; |
| 287 | end if; |
| 288 | |
| 289 | if not exists (select 1 from police_station where p_id = p_new_station_id) then |
| 290 | raise exception 'Полициската станица со ID % не е пронајдена', p_new_station_id; |
| 291 | end if; |
| 292 | |
| 293 | update crime_case |
| 294 | set p_id = p_new_station_id |
| 295 | where c_id = p_case_id; |
| 296 | |
| 297 | select pe_id into v_new_policeman |
| 298 | from policeman |
| 299 | where p_id = p_new_station_id |
| 300 | limit 1; |
| 301 | |
| 302 | if v_new_policeman is null then |
| 303 | select pe_id into v_new_policeman from policeman limit 1; |
| 304 | if v_new_policeman is null then |
| 305 | raise exception 'Не е пронајден полицаец во системот'; |
| 306 | end if; |
| 307 | end if; |
| 308 | |
| 309 | select pe_id into v_victim_id from victim limit 1; |
| 310 | if v_victim_id is null then |
| 311 | raise exception 'Не се пронајдени жртви во системот'; |
| 312 | end if; |
| 313 | |
| 314 | select pe_id into v_witness_id from witness limit 1; |
| 315 | if v_witness_id is null then |
| 316 | raise exception 'Не се пронајдени сведоци во системот'; |
| 317 | end if; |
| 318 | |
| 319 | select coalesce(max(s_id), 0) + 1 into v_new_statement_id from statements; |
| 320 | |
| 321 | insert into statements ( |
| 322 | s_id, statement_date, description, incident_timestamp, |
| 323 | incident_place, c_id, pe_id, victim_pe_id, witness_pe_id |
| 324 | ) values ( |
| 325 | v_new_statement_id, |
| 326 | current_date, |
| 327 | 'Случајот е префрлен од станица ' || v_old_station || ' to station ' || p_new_station_id, |
| 328 | current_timestamp, |
| 329 | 'Трансфер на случај', |
| 330 | p_case_id, |
| 331 | v_new_policeman, |
| 332 | v_victim_id, |
| 333 | v_witness_id |
| 334 | ); |
| 335 | |
| 336 | raise notice 'Случајот % е успешно префрлен од станица % во станица %', |
| 337 | p_case_id, v_old_station, p_new_station_id; |
| 338 | |
| 339 | commit; |
| 340 | |
| 341 | exception |
| 342 | when others then |
| 343 | rollback; |
| 344 | raise exception 'Неуспешен трансфер: %', SQLERRM; |
| 345 | end; |