Solved

Creating a string for email body

Posted on 2015-01-06
1
103 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

733 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