2013-07-30 34 views
-1

我有以下代碼,我需要運行350個位置需要一個小時做5個位置,所以我一次運行5個位置使用where'location_code in'('0001', '0002','0003','0005','0006')我想創建一個臨時表,其中兩列一個location_id,另一個完成並遍歷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 
+1

有你看着一個[執行計劃]( http://msdn.microsoft.co m/en-us/library/ms178071(v = sql.105).aspx)來查看是否有任何可能容易解決的性能問題?提示:數據庫問題通常有助於爲軟件版本添加一個標籤,例如'sql-server-2014',因爲它們有不同的可用功能。 – HABO

+0

sql已經調整並且運行效率很高,它只是我需要通過的數據量,這是SQL 2005 – user2634688

+0

如何更新表?你有一些特定的動態SQL存儲過程,你需要給我們定義('inventory_update_ $ sp')。 –

回答

0

使你的架構的一些假設:

-- A working table to track progress that will stick around. 
create table dbo.Location_Codes 
    (Location_Code VarChar(4), Started DateTime NULL, Completed DateTime NULL); 

然後向上突破的工作是這樣的:

if not exists (select 42 from dbo.Location_Codes where Completed is NULL) 
    begin 
    -- All of the locations have been processed (or this is the first time through). 
    delete from dbo.Location_Codes; 
    -- Get all of the location codes. 
    insert into dbo.Location_Codes 
    select Location_Code, NULL, NULL 
     from Location; 
    end 

-- Temporary table to make things easier. 
declare @Pass_Location_Codes as Table (Location_Code VarChar(4)); 

-- Loop until all locations have been processed. 
while exists (select 42 from dbo.Location_Codes where Completed is NULL) 
    begin 
    -- Get the next five locations for which processing has not completed. 
    delete from @Pass_Location_Codes; 
    insert into @Pass_Location_Codes 
    select top 5 Location_Code 
     from dbo.Location_Codes 
     where Completed is NULL 
     order by Location_Code; 
    -- Record the start date/time. 
    update dbo.Location_Codes 
    set Started = GetDate() 
    where Location_Code in (select Location_Code from @Pass_Location_Codes); 

    -- Run the big query. 
    select ... 
    where Location_Code in (select Location_Code from @Pass_Location_Codes) 
    ... 

    -- Record the completion date/time. 
    update dbo.Location_Codes 
    set Completed = GetDate() 
    where Location_Code in (select Location_Code from @Pass_Location_Codes); 
    end 
相關問題