?
Solved

Creating a string for email body

Posted on 2015-01-06
1
Medium Priority
?
118 Views
Last Modified: 2015-01-07
I am using sql server 2008.
Please help me debug this code. I am trying to create a string for email body but it is coming to be null, when it should have several lines starting with a long sentence of (something, something....)

DECLARE @timeid INT,
@flag1 BIT, @flag2 BIT, @flag3 BIT, @flag4 BIT, @flag5 BIT, @flag6 BIT,

@PL DECIMAL(14,2),
@PL_Load DECIMAL(14,2),
@PL_Variance DECIMAL(14,2),

@TotalOSR DECIMAL(14,2),
@TotalOSR_Load DECIMAL(14,2),
@TotalOSR_Variance DECIMAL(14,2),

@LAExp DECIMAL(14,2),
@LAExp_Load DECIMAL(14,2),
@LAExp_Variance DECIMAL(14,2),

@Sub DECIMAL(14,2),
@Sub_Load DECIMAL(14,2),
@Sub_Variance DECIMAL(14,2),

@WPrem DECIMAL(14,2),
@WPrem_Load DECIMAL(14,2),
@WPrem_Variance DECIMAL(14,2),

@RenWrittenPrem DECIMAL(14,2),
@RenWrittenPrem_Load DECIMAL(14,2),
@RenWrittenPrem_Variance DECIMAL(14,2),

@NBWPrem DECIMAL(14,2),
@NBWPrem_Load DECIMAL(14,2),
@NBWPrem_Variance DECIMAL(14,2),

@NBPolCnt NUMERIC,
@NBPolCnt_Load NUMERIC,
@NBPolCnt_Variance NUMERIC

SET @flag1 = 0
SET @flag2 = 0
SET @flag3 = 0
SET @flag4 = 0
SET @flag5 = 0
SET @flag6 = 0

DECLARE @sup_body VARCHAR(max), @mainBody varchar(max)

UPDATE dbo.c_values
SET int_value = 0
WHERE name = 'validation_successfull'


SET @timeid = (SELECT int_value FROM c_values WHERE name = 'current_period')



 
 --Select * from [CFBDB].[dbo].[val_num_table]

SELECT @PL_Load =  isnull(Sum(paid_loss),0)
FROM dbo.fact_claim WITH(nolock)
WHERE calendartimeID = @timeid


SELECT @PL =  Isnull(CONVERT(DECIMAL(14,2), Paid_Losses),0)
FROM [dbo].[val_num_table]


Set @PL_Variance = Abs(isnull(@PL_Load,0)-isnull(@PL,0))
IF @PL_Variance BETWEEN 0 AND 1
SET @flag1= 1

SELECT @flag1

--============================================================================

SELECT @TotalOSR_Load = isnull(Sum(outstanding_reserves),0)
FROM dbo.fact_claim WITH(nolock)
WHERE calendartimeID = @timeid

SELECT @TotalOSR = Isnull(CONVERT(DECIMAL(14,2),[Total_OS_Reserve]),0)
FROM [dbo].[val_num_table]

Set @TotalOSR_Variance = Abs(isnull(@TotalOSR_Load,0) - isnull(@TotalOSR,0))
IF @TotalOSR_Variance BETWEEN 0 AND 1
SET @flag2=1

SELECT @flag2

--================================================================

SELECT @LAExp_Load = isnull(Sum(lae_paid_loss),0)
FROM dbo.fact_claim WITH(nolock)
WHERE calendartimeID = @timeid


SELECT @LAExp = Isnull(CONVERT(DECIMAL(14,2),[LAE_Expense]),0)
FROM [dbo].[val_num_table]


Set @LAExp_Variance = Abs(isnull(@LAExp_Load,0) - isnull(@LAExp,0))
IF @LAExp_Variance BETWEEN 0 AND 1
SET @Flag3 = 1

SELECT @Flag3


--===========================================================================

SELECT @Sub_Load = Abs(((isnull(Sum(salvage),0)) + (Sum( subro)) + (isnull(Sum(lae_subro),0))))
FROM dbo.fact_claim WITH(nolock)
WHERE calendartimeID = @timeid


SELECT @Sub = Isnull(CONVERT(DECIMAL(14,2),[Subro_Salvage_LAE_Subro]),0)
FROM [dbo].[val_num_table]


Set @Sub_Variance = Abs(isnull(@Sub_Load,0)-isnull(@Sub,0))

IF @Sub_Variance BETWEEN 0 AND 1
SET @flag4 = 1

SELECT @flag4

--============================================================================

--written premium
SELECT @WPrem_Load = isnull(Sum(written_premium),0)
FROM dbo.fact_written_premium WITH(nolock)
WHERE timeid = @timeid


SELECT @WPrem = Isnull(CONVERT(DECIMAL(14,2),[Written_Premium]),0)
FROM [dbo].[val_num_table]


Set @WPrem_Variance = Abs(isnull(@WPrem_Load,0)-isnull(@WPrem,0))
IF @WPrem_Variance BETWEEN 0 AND 1
SET @flag5=1

SELECT @flag5

--===============================================================================


SELECT @RenWrittenPrem_load = Isnull(Sum(written_premium),0)
FROM dbo.fact_written_premium WITH(nolock)
WHERE timeid = @timeid AND newbusinessID = 3
SELECT @NBWPrem_Load  = (@WPrem_Load - @RenWrittenPrem_load)



SELECT @NBWPrem = Isnull(CONVERT(DECIMAL(14,2),[New_Business_Written]),0)
FROM [dbo].[val_num_table]


Set @NBWPrem_Variance = Abs(isnull(@NBWPrem_Load,0)-isnull(@NBWPrem,0))
IF @NBWPrem_Variance BETWEEN 0 AND 1
SET @flag6 = 1

SELECT @flag6


		    select @sup_body = ''
		    select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'CFB P Count = ' + Convert(varchar, @NBPolCnt)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'CFB P Count = ' + Convert(varchar, @NBPolCnt_Load)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'Variance = ' + Convert(varchar, @NBPolCnt_Variance)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10)
              
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'CFB P Loss = ' + Convert(varchar, @PL)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'CFB P Loss = ' + Convert(varchar, @PL_Load)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'Variance = ' + Convert(varchar, @PL_Variance)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10)
              
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'CFB W Prem = ' + Convert(varchar, @WPrem)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'CFB W Prem = ' + Convert(varchar, @WPrem_Load)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'Variance = ' +  Convert(varchar, @WPrem_Variance)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10)
              
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'CFB NB W Prem = ' +Convert(varchar,  @NBWPrem)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'CFB NB W Prem = ' + Convert(varchar, @NBWPrem_Load)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'Variance = ' + Convert(varchar, Abs(@NBWPrem_Load - @NBWPrem))
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10)
              
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'CFB RW Prem  = ' + Convert(varchar, @RenWrittenPrem)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'CFB RW Prem = ' + Convert(varchar, @RenWrittenPrem_Load)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'Variance = ' + Convert(varchar, @RenWrittenPrem_Variance)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10)
              
              
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'CFB Sub = ' + Convert(varchar, @Sub)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'CFB Sub = ' + Convert(varchar, @Sub_Load)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'Variance = ' + Convert(varchar, @Sub_Variance)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10)
              
              
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'CFB L Expense  = ' + Convert(varchar, @LAExp)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'CFB L Expense  = ' + Convert(varchar, @LAExp_Load)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'Variance = ' + Convert(varchar, @LAExp_Variance)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10)
              
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'CFB Total OSR  = ' + Convert(varchar, @TotalOSR)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'CFB Total OSR  = ' + Convert(varchar, @TotalOSR_Load)
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'Variance = ' + Convert(varchar, @TotalOSR_Variance)
		    select @sup_body = @sup_body +  CHAR(13)+CHAR(10)

--IF (@flag1 = 1 AND @flag2 = 1 AND @flag3 = 1 AND @flag4 = 1 AND @flag5 = 1 AND @flag6 = 1 )
--BEGIN
            print 'data validated'
            
            UPDATE dbo.c_values
            SET int_value = 1
            , date_value = Getdate()
            WHERE name = 'validation_successfull'
            
            Select @mainBody = 'something something something something something something something something something something something something something something something something something something something something something  ' 
            +CHAR(13)+ CHAR(10)+ @sup_body           
            
            
--            
            
            select @mainBody as mainBody
            
            Select @sup_body as sup_body
            
            Print @mainBody
            
            Print @sup_body

Open in new window

0
Comment
Question by:patd1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 40534385
Any single NULL value being concatenated will cause the entire string to be NULL.

To be safe, you need to add an ISNULL() check on all variables concatenated to the string:

 select @sup_body = ''
                select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'CFB P Count = ' + ISNULL(Convert(varchar, @NBPolCnt), '0')
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'CFB P Count = ' + ISNULL(Convert(varchar, @NBPolCnt_Load), '0')
              select @sup_body = @sup_body +  CHAR(13)+CHAR(10) + 'Variance = ' + ISNULL(Convert(varchar, @NBPolCnt_Variance), '0')
...
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question