>DECLARE @textbody TABLE > >( symbol char(6), eff_date datetime, effdate char(8), >cntry_code char(2), ntype char(10), condition char(50), > eventid char(10), fcid char(10), fctid char(10), bsid char(10) ) > >--- @textbody gets properly filled > >declare @text varchar(max) > >SET @text = 'SPI FEED FILE '+ @csvfile + ' Processed '+@procdatetime+char(13)+char(10) >+char(13)+char(10) >+'New Corporate Actions' + char(13)+char(10) > >SET @text = @text + >SELECT >cntry_code + ' ' + effdate + ' ' + symbol + ' ' +fcid + ' ' + condition + char(13) + char(10) >from @textbody where ntype = 'New' >order by cntry_code, eff_date desc, symbol > >SET @text = @text + char(13)+char(10) >+'Updated Corporate Actions ' + char(13)+char(10)+char(13)+char(10) > >SET @text = @text + >SELECT >cntry_code + ' ' + effdate + ' ' + symbol + ' ' +fcid + ' ' + condition + char(13) + char(10) >from @textbody where ntype = 'Update' >order by cntry_code, eff_date desc, symbol > >SET @text = @text + char(13)+char(10) >+'DJI Created Corporate Actions Updated ' + char(13)+char(10)+char(13)+char(10) > >SET @text = @text + >SELECT >cntry_code + ' ' + effdate + ' ' + symbol + ' ' +fcid + ' ' + condition + char(13) + char(10) >from @textbody where ntype = 'UpdateDJI' >order by cntry_code, eff_date desc, symbol >.... >--- and finally > >Update FCA_Backfeed_batch >SET Notification_email_body = @text, >NumberOfRows = @stgcount >where fca_backfeed_batch_id = @batchid > >>
>SELECT @text = @text + char(13)+char(10) >+'Updated Corporate Actions ' + char(13)+char(10)+char(13)+char(10) > >SELECT @text = @text + >cntry_code + ' ' + effdate + ' ' + symbol + ' ' +fcid + ' ' + condition + char(13) + char(10) >from @textbody where ntype = 'Update' >order by cntry_code, eff_date desc, symbol >>
SELECT @text = @text + cntry_code + ' ' + effdate + ' ' + symbol + ' ' +fcid + ' ' + condition + char(13) + char(10) from @textbody where ntype = 'New' order by cntry_code, eff_date desc, symbol OR SET @text = @text + (SELECT top (1) cntry_code + ' ' + effdate + ' ' + symbol + ' ' +fcid + ' ' + condition + char(13) + char(10) from @textbody where ntype = 'New' order by cntry_code, eff_date desc, symbol)BTW, are you sure the types of the fields are all varchar/char? If not, you need to convert them to varchar first.