2012-09-13 54 views
2

我從大約1000列的表中填充約15列的表格。我需要從大桌子上搶下時間。那個時間分成幾分鐘和幾小時[rn-min]和[rn-hr],我需要在新表格中以am/pm格式存儲它們。該表由外部公司填寫,所以我不能對此做太多改變,我確實讓他們爲我檢查了一個轉移列。它很大很慢,我只需要幾列,而且有很多重複/相似的行。無論如何,我正在從更大的桌子上做出更小的桌子。我寫了一個光標,它的速度很慢,我想知道是否有更好的方法來做到這一點。我不能只使用簡單的插入(選擇列),因爲我想改變時間和日期的存儲方式。謝謝,任何幫助或建議表示讚賞大難看光標

declare data CURSOR READ_ONLY FORWARD_ONLY 
for 
    select [raID], 
      (otherfields), 
      CAST([RA-rent-mm] as varchar(2)) + '/' + CAST([RA-rent-dd] as varchar(2)) + '/' + 
      CAST([RA-Rent-CC] as varchar(2)) + CAST([RA-RENT-YY] as varchar(2)) [Date_Out], 
      CAST([RA-Rtrn-mm] as varchar(2)) + '/' + CAST([RA-Rtrn-dd] as varchar(2)) + 
      '/' + CAST([RA-Rtrn-CC] as varchar(2)) + CAST([RA-Rtrn-YY] as varchar(2)) [Date_In], 
      CAST([RA-RENTAL-HOURS] as varchar(2)), 
      CAST([RA-RENTAL-Minutes] as varchar(2)), 
      CAST([RA-RTRN-HOURS] as varchar(2)), 
      CAST([RA-RTRN-MINUTES] as varchar(2)), 
      (other fields) 
     from table_name 
    where Transfered is null 
     and [RA-rtrn-mm] != 0 --this keeps me from getting the duplicate/similar rows, once this doesn't equal 0 there aren't anymore rows so I just grab this one 

declare @sql as varchar(max) 
declare @raID int; 
    (other fields), 
declare @rentDate varchar(8); 
declare @rtrnDate varchar(8); 
declare @rentHours varchar(2); 
declare @rentMinutes varchar(2); 
declare @rtrnHours varchar(2); 
declare @rtrnMinutes varchar(2); 
    (other fields) 

open data 
    fetch next from data into 
     @raID, 
     (other fields), 
     @rentDate , 
     @rtrnDate , 
     @rentHours , 
     @rentMinutes , 
     @rtrnHours , 
     @rtrnMinutes , 
     (other fields), 
    while @@FETCH_STATUS = 0 
    begin 
     set @rentMinutes = left('0' + @rentMinutes,2);--padding with 0 if minutes is 1-9 
     set @rtrnMinutes = left('0' + @rtrnMinutes,2); 

     --turning the varchar times into a time then back to varchar with correct am/pm notation 
     declare @rentT time = @rentHours + ':' + @rentMinutes; 
     declare @rtnT time = @rtrnHours + ':' + @rtrnMinutes; 
     declare @rentTime varchar(7) = convert(varchar(15),@rentT, 100); 
     declare @returnTime varchar(7) = convert(varchar(15),@rtnT, 100); 

     --print @rentTime; 
     set @sql = 'INSERT other_tbl_name(raID, (other fields), Date_Out, Date_In, Time_Out, Time_In, (other fields)) 
       values ('+cast(@raID as varchar(max))+', (other fields),'''[email protected]+''', 
          '''[email protected]+''', '''[email protected]+''', '''[email protected]+''', 
          (other fields))'; 

     --exec(@sql) 
     print @sql 

     --need a way to make sure the insert worked before updating 
     --need to update transferred to keep from updating the same info 

     declare @update as varchar(max) = ' 
     UPDATE Capture.icokc_data 
      SET Transfered = 1 
     WHERE [raID] = '+cast(@raID as varchar(10)) 

     --exec(@update) 
     --print @update 

     fetch next from data into 
      @raID, 
      (other fields) 
      @rentDate , 
      @rtrnDate , 
      @rentHours , 
      @rentMinutes , 
      @rtrnHours , 
      @rtrnMinutes , 
      (other fields) 
    end 

close data; 
deallocate data; 
+0

@Johan謝謝您的回答傢伙,他們都幫助 – Kevin

回答

3

你爲什麼不批量插入它,改造的日期和時間的選擇?

事情是這樣的:

INSERT other_tbl_name(raID, (other fields), Date_Out, Date_In, Time_Out, Time_In, (other fields)) 
select 
[raID], 
(otherfields), 
CAST([RA-rent-mm] as varchar(2)) + '/' + CAST([RA-rent-dd] as varchar(2)) + '/' + CAST([RA-Rent-CC] as varchar(2)) + CAST([RA-RENT-YY] as varchar(2)) [Date_Out], 
CAST([RA-Rtrn-mm] as varchar(2)) + '/' + CAST([RA-Rtrn-dd] as varchar(2)) + '/' + CAST([RA-Rtrn-CC] as varchar(2)) + CAST([RA-Rtrn-YY] as varchar(2)) [Date_In], 
CONVERT(varchar(15),DATEADD(minute, [RA-RENTAL-Minutes], DATEADD(hour, [RA-RENTAL-HOURS], '00:00')), 100) as [Time_out], 
CONVERT(varchar(15),DATEADD(minute, [RA-RTRN-MINUTES], DATEADD(hour, [RA-RTRN-HOURS], '00:00')), 100) as [Time_in], 
(other fields) 
from table_name 
where Transfered is null 
and [RA-rtrn-mm] != 0 

UPDATE Capture.icokc_data 
SET Transfered = 1 
WHERE [raID] IN 
(
select 
[raID] 
from table_name 
where Transfered is null 
-- and [RA-rtrn-mm] != 0 -- not sure about this one 
) 
+0

是的!這更多的是我想要做的。我還沒有測試過這個,但我仍然會將其標記爲正確的,因爲這是我想要去的方向。 – Kevin

+0

@Kevin:這就是我一直在說的...... – Guffa

+0

@Guffa你的答案都是正確的,並且幫助我使查詢更快。 – Kevin

2

由於這是一個直接轉換,即一個記錄,一個記錄了,我實在看不出有任何理由它不能用單一插入查詢完成。

無論如何,不​​要動態創建查詢。動態查詢將針對每次迭代進行解析和計劃,這很可能是大多數性能問題的原因。

,而不是例如:

declare @update as varchar(max) = ' 
UPDATE Capture.icokc_data 
SET Transfered = 1 
WHERE [raID] = '+cast(@raID as varchar(10)) 

exec(@update) 

只是做:

UPDATE Capture.icokc_data 
SET Transfered = 1 
WHERE [raID] = @raID 
+0

謝謝你,我不知道爲什麼,我就是這麼做的,我一定是在動態得到趕上我正在創建的sql龍捲風。 – Kevin