• You most certainly do not need a CURSOR for this. In fact, as a beginning SQL person it may not be too late to unlearn what will become a bad habit. Forget that you ever heard of CURSORs now! Learn that there is a better path. The anti-RBAR alliance will forever bless you if you do.

    Here's the solution without a CURSOR:

    DECLARE @Table1 TABLE

    (total INT, EmpID VARCHAR(6), Name VARCHAR(10), [Date] DATE

    ,[Start Time] TIME, [End Time] TIME)

    INSERT INTO @Table1

    SELECT 0,'A00030','John','2013/03/06','07:59:19','11:59:25'

    UNION ALL SELECT 0,'A00030','John','2013/03/11','07:58:40','10:36:35'

    UNION ALL SELECT 0,'A00048','May','2013/03/31','07:50:20','11:59:10'

    DECLARE @Table2 TABLE

    (ID INT, Name VARCHAR(10), [Date] DATE, LeaveType VARCHAR(30)

    ,Note VARCHAR(3), [Start Time] DATETIME, [End Time] DATETIME)

    INSERT INTO @Table2

    SELECT 596184,'john','2013/03/06','Sick Leave','22x','2013-04-04 18:00:00.000','2013-04-04 20:00:00.000'

    UNION ALL SELECT 596185,'john','2013/03/11','Sick Leave','22x','2013-04-04 18:00:00.000','2013-04-04 20:00:00.000'

    SELECT * FROM @Table1

    UPDATE t1

    SET total = total + 1

    FROM @Table1 t1

    INNER JOIN @Table2 t2

    ON t1.Name = t2.name AND t1.[Date] = t2.[Date] AND Note = '22x'

    SELECT * FROM @Table1


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St