[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Creating a string for email body

Posted on 2015-01-06
1
Medium Priority
?
126 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 70

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

650 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