0

I have an issue with one of my transaction. I set it up to print how many rows have been touched using @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

go

For some reason that I don't seem to understand, the procedure prints "No rows updated" even when there are rows updated! Any Idea what I am doing wrong? Thanks so much!

4

1 回答 1

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

我还在您的代码中删除了一些多余的大括号

于 2013-08-18T13:03:02.577 回答