CURSOR/FETCH Issue

Hey Everyone,

I have an issue with pulling data out of one table and concatinating it into another table. There is a table with a list of Work Orders (tblWorkOrders) that has a one-to-many relationship with a table that contains a list of Work Order Comments (tblComments).

I want to concatenate all the work order comments together into one Text field in the work orders table for each work order. For example, if tblWorkOrders.ReqNum matches three records with tblComments.ReqNum, I want to concatenate all three tblComments.Comment together and put them in tblWorkOrders.Comments for the one record in tblWorkOrders that matches that same ReqNum.

I've been playing around with the CURSOR/FETCH combo, but I am having no luck. Please help!!!!!

Here is my code:

USE SolidWaste;
GO
DECLARE C1
CURSOR
FOR
SELECT c.
Comment
FROM tblComments AS
c
INNER JOIN tblWorkOrders AS
w
ON c.ReqNum = w.ReqNum
;
OPEN C1
;
DECLARE @comments varchar(500
);
FETCH NEXT FROM C1 INTO @comments
;
WHILE (@@FETCH_STATUS <> -1
)
BEGIN
IF (@@FETCH_STATUS <> -2
)
BEGIN
UPDATE
tblWorkOrders
SET tblWorkOrders.Comments =
@comments
END
;
FETCH NEXT FROM C1 INTO @comments
;
END
;
CLOSE C1
;
DEALLOCATE C1;

The first problem with this code is that it pulls every single comment from tblComments and not just the comments for the corresponding work order (tblWorkOrders.ReqNum = tblComments.ReqNum). The second problem is that it does not concatenate at all; each FETCH replaces the contents of tblWorkOrder.Comments. The the results are that the contents of the Comments field for every single record in tblWorkOrders is the very last comment resulting from the join.

This is my first experience with using cursors so please don't laugh too hard at my code. I'm sure that the problem is in my loop, which is also a new thing for me. I assumed that populating the CURSOR with an INNER JOIN would just pull one matching set at a time, but I obviously don't know what I am doing.

Thanks in advance.

Lee.




Answer this question

CURSOR/FETCH Issue

  • exortech

    Nevermind. I just changed the data type to what it should be.

    The script works great. Thanks again.



  • sotnlk

    Thanks so much.

    I just tried the script, and I am getting the following message:

    Msg 403, Level 16, State 1, Line 21
    Invalid operator for data type. Operator equals add, type equals text.

    I tried changing '+' to '&', but it does not like that either. I'm not sure why it's giving me grief over this because I have concatenated using '+' dozens of times.

    Do you have any ideas

    Thanks,

    Lee



  • Toddley

    Hi Lee.

    I can see 2 problems with your update :

    1. You do not know what orders you are pulling your comments for. To fix that , difine another variable @OrderID and fetch its value when looping through cursor

    2. Concatenation will work if you add your @comments to already existing string , not replace it : <comments> = <comments> + @comments . Take care of the Null strings here.

    USE SolidWaste;
    GO
    DECLARE @comments varchar(500);
    DECLARE @OrderID int; -- field , that uniquely defines your order
    /*
    Optionally You can remove all comments from tblWorkOrders to begin with
    UPDATE

    */
    DECLARE C1 CURSOR
    FOR
    SELECT c.Comment , w.OrderID -- additional field to narrow your update
    FROM tblComments AS c
    INNER JOIN tblWorkOrders AS w
    ON c.ReqNum = w.ReqNum;
    OPEN C1;

    FETCH NEXT FROM C1 INTO @comments ,@OrderID ; -- include your identifier into Fetch
    WHILE (@@FETCH_STATUS <> -1)
    BEGIN
    IF (@@FETCH_STATUS <> -2)
    BEGIN
    UPDATE tblWorkOrders
    SET tblWorkOrders.Comments = ISNULL(tblWorkOrders.Comments,'') + ' '+ ISNULL(@comments,'') -- concatenate your comments
    WHERE OrderID = @OrderID -- limit Updated records
    END;
    FETCH NEXT FROM C1 INTO @comments ,@OrderID ;
    END;
    CLOSE C1;
    DEALLOCATE C1;


  • CURSOR/FETCH Issue