大丑陋的游标

时间:2012-09-13 00:28:40

标签: sql sql-server-2008 tsql cursor dynamic-sql

我正在从大约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),'''+@rentDate+''',
                            '''+@rtrnDate+''', '''+@rentTime+''', '''+@returnTime+''',
                            (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;

2 个答案:

答案 0 :(得分: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
)

答案 1 :(得分: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