350 の場所に対して実行する必要がある次のコードがあります。5 つの場所を実行するには 1 時間かかります。そのため、where location_code in ('0001', '0002', '0003', '0005', '0006') 1 つの location_id ともう 1 つの完了した 2 つの列を持つ一時テーブルを作成し、location_id 列の各値をループしてから、完了した列を日付とタイム スタンプで更新し、それぞれの後にコミットしたいと思います。このようにして、実行させて、それを強制終了する必要がある場合は、最後に完了した location_id を確認し、プロセスをどこから再開するかを知ることができます。さらに、完了した列の値をチェックし、存在する場合は次の行に進みます。 .....
--Collecting all records containing remnant cost. You will need to specify the location number(s). In the example below we're using location 0035
select sku_id, ib.location_id, price_status_id, inventory_status_id, sum(transaction_units) as units, sum(transaction_cost) as cost,
sum(transaction_valuation_retail) as val_retail, sum(transaction_selling_retail) as sell_retail
into #remnant_cost
from ib_inventory ib
inner join location l on l.location_id = ib.location_id
where location_code in ('0001', '0002', '0003', '0005', '0006')
group by sku_id, ib.location_id, price_status_id, inventory_status_id
having sum(transaction_units) = 0
and sum(transaction_cost) <> 0
--Verify the total remnant cost.
select location_id, sum(units) as units, sum(cost) as cost, sum(val_retail) as val_retail, sum(sell_retail) as sell_retail
from #remnant_cost
group by location_id
select *
from #remnant_cost
----------------------------------------------------Run above this line first and gather results--------------------------------
--inserting into a temp table the cost negation using transaction_type_code 500 (Actual shrink) before inserting into ib_inventory
--corrected query adding transaction date as column heading (Marshall)
select
sku_id, location_id, price_status_id, convert(smalldatetime,convert(varchar(50),getdate(),101)) as transaction_date, 500 as transaction_type_code, inventory_status_id, NULL as other_location_id,
NULL as transaction_reason_id, 999999 as document_number, 0 as transaction_units, cost * -1 as transaction_cost, 0 as transaction_valuation_retail,
0 as transaction_selling_retail,NULL as price_change_type, NULL as units_affected
into #rem_fix
from #remnant_cost
--Validating to make sure cost will have the exact opposite to negate.
select location_id, sum(transaction_units) as units, sum(transaction_cost) as cost, sum(transaction_valuation_retail) as val_retail,
sum(transaction_selling_retail) as sell_retail
from #rem_fix
group by location_id
BEGIN TRAN
EXEC inventory_update_$sp 'SELECT sku_id,location_id,price_status_id,transaction_date,transaction_type_code,inventory_status_id,other_location_id,
transaction_reason_id,document_number,transaction_units,transaction_cost,transaction_valuation_retail,transaction_selling_retail,price_change_type,
units_affected FROM #rem_fix'
COMMIT