I'm having trouble with Coalesce in building a comma, delimited string. Basically, I'm building a table with 2 key columns, a major_class and the string of minor classes associated with each major_class.Here's the code: SET NOCOUNT ONTRUNCATE TABLE TBL_UMHS_FGP_DRUG_INDEX --must truncate to reset counterINSERT INTO TBL_UMHS_FGP_DRUG_INDEX (major_ctg, major_ctg_code) SELECT major_class_desc, major_class FROM outpt_drug_costs_majorclasses ORDER BY major_class_sortorder, major_class_descDECLARE @minorctgstring varchar(7000)DECLARE @iRow2 intDECLARE @iMax2 intSELECT @iMax2 = count(*) from TBL_UMHS_FGP_DRUG_INDEXSELECT @iRow2 = 1While @iRow2 < @iMax2BEGIN--build the csv stringSELECT @minorctgstring = nullSelect @minorctgstring = Coalesce(@minorctgstring + ', ','')+ minor_class_descFROM outpt_drug_costs_minorclassesWHERE major_class_code = (SELECT major_ctg_code FROM TBL_UMHS_FGP_DRUG_INDEX WHERE sys_id = @iRow2)SELECT @minorctgstring--UPDATE THE TABLEUPDATE TBL_UMHS_FGP_DRUG_INDEX SET minor_ctg_string = @minorctgstring WHERE sys_id = @iRow2SELECT @iRow2 = @iRow2 + 1END
This works great except the last value for the minor_ctg_string is always Null and not the string it's supposed to be. Any ideas how to make this code finish the last row?