Solved

Creating a string for email body

Posted on 2015-01-06
1
98 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
1 Comment
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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