/* Start the table */
SET @tmpBody = '<table align="center"><tr><th>Item #</th><th>Qty</th><th>Description</th><th>Price</th><th>Ext. Price</th></tr>' + CHAR(13) + CHAR(10)
/* From now on only grab 1 record when using SELECT statements */
SET rowcount 1
/* While there is still something in our temp Item table continue */
WHILE exists(SELECT * FROM temp_OrderItems)
BEGIN
/* Get our values
SELECT @id=my_id, @itemnum=rtrim(ITEMNMBR), @qty=CAST(QUANTITY AS numeric(19,0)), @desc=rtrim(ITEMDESC), @price=CAST(UNITPRCE AS numeric(19,2)), @xprice=CAST(XTNDPRCE AS numeric(19,2)) FROM temp_OrderItems
/* Build them into our table adding on each time */
SET @tmpBody = @tmpBody + '<tr align="center">' +
'<td>' + @itemnum + '</td>' +
'<td>' + @qty + '</td>' +
'<td align="left">' + @desc + '</td>' +
'<td>$' + @price + '</td>' +
'<td align="right">$' + @xprice + '</td>' +
'</tr>' + CHAR(13) + CHAR(10)
/* Delete the record from the temporary table */
DELETE FROM temp_OrderItems WHERE my_id=@id
/* Update our other temporary table to carry our changes in here out with us */
UPDATE temp_Details SET BODY=@tmpBody
END
/* Grab table of items and do what we need with it */
SELECT @tmpBody=BODY FROM temp_Details