2013-08-18 147 views
0

我有一個問題,與我的一個交易。我把它打印多少行已經觸及@RowCount使用:@rowcount問題與交易

create proc Proc_Whole_Months_Work 
@ProjectNum int, 
@EmpId int, 
@StartDate datetime, 
@EndDate datetime 
as 
BEGIN TRANSACTION 
    declare @CurrentDate datetime 
    DECLARE @rowcount int 
    set @CurrentDate = @StartDate 

    while (@CurrentDate <= @EndDate) 
    begin 
     if (datepart(WEEKDAY, @CurrentDate) <=5)--if it is a weekday 
     begin 
      insert into WorkHours(ProjectNum,EmpId,WorkDate,BeginTime,EndTime)values (@ProjectNum,@EmpId,@CurrentDate,'09:00:00','17:00:00')  
     end 
     set @CurrentDate =DATEADD(dd, 1, @CurrentDate) 
    end 
SET @rowcount = @@ROWCOUNT 
--print @rowcount 
if(@rowcount = 0) 
    PRINT 'No rows Updated' 
else 
    print 'Number Of Rows Updated:' + ' ' + str(@rowcount) 
COMMIT TRANSACTION 

出於某種原因,我似乎不明白,程序打印「沒有行更新」,即使有行更新! 任何想法我做錯了什麼? 非常感謝!

回答

0

@@rowcount返回受上一語句影響的行數,所以你必須在執行任何語句之前,將其保存,否則會失去

set @rowcount = 0 
while @CurrentDate <= @EndDate 
begin 
    if datepart(weekday, @CurrentDate) <=5 --if it is a weekday 
    begin 
     insert into WorkHours(ProjectNum,EmpId,WorkDate,BeginTime,EndTime)values (@ProjectNum,@EmpId,@CurrentDate,'09:00:00','17:00:00')  
     set @rowcount = @rowcount + @@rowcount 
    end 
    set @CurrentDate = dateadd(day, 1, @CurrentDate) 
end 

print @rowcount 

我也刪除了一些多餘的括號中的代碼

+0

非常感謝您的回答!但它仍然沒有行更新..:\(1行受影響) (1 row(s)affected) 沒有行更新 – user2693861

+0

1.嘗試在您的過程中打印@rowcount。 2.你確定有插入的行嗎? –