Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Building a varchar(max) from multi query results
Message
 
 
To
16/07/2012 20:53:59
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2008
Miscellaneous
Thread ID:
01548693
Message ID:
01548699
Views:
39
>Really need help with this one.
>
>As I process records in my SP I am building a cursor representing conditions that will go into a VARCHAR(MAX) column in a batch table to show the results of the processing
>
>I am getting a syntax error on the SELECT I am trying to append to my @text variable.
>
>Obviously what I am trying to do is build a varchar(max) including CRLFs, inserting category headings and then info from all rows in my @textbody that fix that category.
>
>Is there a problem using CHAR(13)+CHAR(10) pairs for CRLF ? This varchar(max) column is going to be used as the body of an email.
>
>If it is just a syntax tweak would appreciate guidance or if there is some other slick solution ...
>
>
>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
>
>
>
>Naomi, I'll owe you a dinner. <g>
>
>UPDATE: Naomi I'll still owe you the dinner but I think I figured out I need to use SELECT instead of SET as in
>
>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
>
>
>Yeah?

Right, just noticed your final note while I was going to point out that this syntax is wrong
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


and should be either
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.
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform