2013-07-30 25 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