Files
FTC25009/sql/20260710_cargo_space_height.sql

81 lines
2.0 KiB
PL/PgSQL

set nocount on;
set xact_abort on;
begin try
begin transaction;
declare @target_count int;
declare @high_count int;
declare @normal_count int;
declare @updated_rows int;
select @target_count = count(*)
from dbo.bas_cargo_space
where warehouse_id in ('01', '02');
select @high_count = count(*)
from dbo.bas_cargo_space
where warehouse_id in ('01', '02')
and the_floor in (1, 5);
select @normal_count = count(*)
from dbo.bas_cargo_space
where warehouse_id in ('01', '02')
and the_floor not in (1, 5);
if @target_count <> 340
begin
;throw 51001, 'Unexpected 01/02 cargo-space count; no changes committed.', 1;
end;
if @high_count <> 136
begin
;throw 51002, 'Unexpected high cargo-space count; no changes committed.', 1;
end;
if @normal_count <> 204
begin
;throw 51003, 'Unexpected normal cargo-space count; no changes committed.', 1;
end;
update dbo.bas_cargo_space
set the_height = case when the_floor in (1, 5) then 2 else 1 end
where warehouse_id in ('01', '02');
set @updated_rows = @@rowcount;
if @updated_rows <> @target_count
begin
;throw 51004, 'Not all 01/02 cargo spaces were updated; no changes committed.', 1;
end;
if exists (
select 1
from dbo.bas_cargo_space
where warehouse_id in ('01', '02')
and (
the_height is null
or the_height <> case when the_floor in (1, 5) then 2 else 1 end
)
)
begin
;throw 51005, 'Cargo-space height verification failed; no changes committed.', 1;
end;
commit transaction;
select warehouse_id,
the_floor,
the_height,
count(*) as cargo_space_count
from dbo.bas_cargo_space
where warehouse_id in ('01', '02')
group by warehouse_id, the_floor, the_height
order by warehouse_id, the_floor, the_height;
end try
begin catch
if @@trancount > 0
rollback transaction;
throw;
end catch;