重写存储过程以删除游标

时间:2012-06-21 14:56:24

标签: sql-server tsql stored-procedures cursor

我正在尝试从此存储过程中删除游标,但不确定这种操作的最佳最新最佳实践是否能够在高效的语句中运行。

任何人都可以提供任何伪代码来实现从Dev开发角度消除这些内容吗?

    --Generate the channel date from a specified date
DECLARE @ConvDate DATETIME
SET @ConvDate = DateAdd(day,-100,getDate())
WHILE DateDiff(day,GetDate(), @ConvDate ) < 0 
BEGIN
    EXEC mltGenerateChannelData @ConvDate
    SET @ConvDate = DateAdd(day, 1, @ConvDate)
END


        CREATE PROCEDURE [dbo].[mltGenerateChannelData] (@ConvDate DATETIME)  AS  
        BEGIN  

           DECLARE @ChannelId INT, 
           @URLSignature Varchar(30), 
           @RawSQL VARCHAR(2000), 
           @SQLQuery VARCHAR(4000), 
           @ThisUTMId BIGINT 

           DECLARE cursChannels CURSOR STATIC FOR 
            SELECT 
                ChannelId, 
                URLSignature, 
                RawSQL  
            FROM dbo.TrackingChannel_tbl (NOLOCK) 
            WHERE ProcessVisitDate = 1  

            SET @ConvDate = dbo.datePart_fn(@ConvDate)  

         --Clear out any existing data for this conversion date  
         DELETE FROM TrackingChannelDailyTotal_tbl 
         WHERE TrackingDate = @ConvDate  

         OPEN cursChannels  

         FETCH cursChannels INTO @ChannelId, @URLSignature, @RawSQL  

         CREATE TABLE #UTM 
         (trpUTMID BIGINT PRIMARY KEY, 
         TotalMArgin MONEY, 
         RawURLRequest Varchar(2000), 
         Keywords VARCHAR(1000), 
         VisitDate DATETIME, 
         RefererURL VARCHAR(2000))  

         INSERT INTO #UTM (trputmid, TotalMargin)  
            SELECT trpUTMID, SUM(b.TotalMArgin)  
            FROM TrackingConversion_tbl c(NOLOCK), Booking_tbl b(NOLOCK)               
            WHERE c.BookingId = b.BookingId  
            AND c.BookedDate >= @ConvDate  
            GROUP BY trputmid  

         UPDATE u  
            SET RawURLRequest = v.RawURLRequest,  
             Keywords = v.Keywords,  
                   VisitDate = v.VisitDate,  
                   RefererURL = v.RefererURL  
             FROM #UTM u,    
            TrackingVisit_tbl (NOLOCK)  v   
         WHERE v.trpUTMID = u.trpUTMId  

         CREATE TABLE #UTM2  (trpUTMID BIGINT PRIMARY KEY)  

         WHILE @@FETCH_STATUS = 0  
         BEGIN  

           Print 'Processing Channel Id : ' + Convert(varchar(10), @ChannelId)  

           TRUNCATE TABLE #UTM2  


           SET @SQLQuery = ' INSERT INTO #UTM2 (trpUTMId)  
                             SELECT u.TrpUTMId  
                             FROM #UTM u               
                             WHERE u.VisitDate >= ''' + COnvert(varchar,@ConvDate) + '''   
                             AND u.VisitDate < DateAdd(day,1,''' + Convert(varchar,@ConvDate) + ''') '  


           IF @URLSignature <> ''  
           BEGIN              
            SET @SQLQuery = @SQLQuery + 'AND u.RawURLRequest like ''%' + @URLSignature + '%'' '    
           END  

           IF @RawSQL <> ''  
           BEGIN  
            SET @SQLQuery = @SQLQuery + @RawSQL  
           END  

           EXEC (@SQLQuery)  

           INSERT INTO TrackingChannelDailyTotal_tbl (ChannelId, TrackingDate, Conversions, TotalMargin)  
            SELECT @ChannelId, @ConvDate, Count(u1.trpUTMID), IsNUll(SUM(TotalMargin),0)  
            FROM #UTM u1, #UTM2 u2  
            WHERE u1.TRputmid = u2.trputmid  


           FETCH cursChannels INTO @ChannelId, @URLSignature, @RawSQL  

         END  

         CLOSE cursChannels  
         DEALLOCATE cursChannels  

2 个答案:

答案 0 :(得分:0)

如果您在查询中使用SSMS工具(http://www.ssmstoolspack.com/)和“包含实际执行计划”,则可以分解查询中的瓶颈,获取每个部分并尝试隔离并改进查询。

您可能会发现问题出在查询的不同部分,而不是光标。

答案 1 :(得分:-1)

如果旧服务器意味着低内存或处理能力 如果升级硬件是可行的 作为一名DBA,我建议升级硬件。