Declare @CurrentID int, @JobCode char(10),@Date char(10),@Time char(4)
    BEGIN TRY
    SET @CurrentID = 1;
    Select @JobCode = JobCode,@Date = [Date],@Time = [Time]   
    from Atd_DataReceive
    where DataReceiveId = @CurrentID;
    
    WHILE (@@ROWCOUNT >= 1)
BEGIN
   UPDATE Atd_DayResult
   SET Time1 = @Time
   WHERE JobCode = @JobCode AND AtdDate = @Date
SET @CurrentID = @CurrentID + 1; 
SELECT @JobCode = JobCode,@Date = [Date],@Time = [Time]
from Atd_DataReceive
    WHERE DataReceiveId = @CurrentID; 
        END
    COMMIT TRANSACTION
    END TRY
如上代码,当Atd_DataReceive 和Atd_DayResult的数据很多时 这段SQL会很慢 请问有什么办法能增加效率

解决方案 »

  1.   

    DataReceiveId ,JobCode ,AtdDate 创建索引
      

  2.   

    update a  SET Time1=b.[Time] from Atd_DayResult a,Atd_DataReceive b
    where a.JobCode =b.JobCode and a.[Date]=b.[Date]
    这样写不就可以吗
      

  3.   

    变量赋值的时候加上 top 1 会提高效率Select  top 1 @JobCode = JobCode,@Date = [Date],@Time = [Time]   
            from Atd_DataReceive
            where DataReceiveId = @CurrentID;