Solved

If a cursor doesn't have data don't send a email

Posted on 2008-10-15
2
235 Views
Last Modified: 2012-05-05
I have a cursor that I have written with some help on here.  I would like to know what I need to change.  I have some agents that It sends data to even if there is no circuits for that agent to look at.

Here is a breif description of what this cursor does:

Send a email to agents if they are associated with circuits attaching the circuits they are responsible for.  I am sure we need to add a isnull but I don't know where.
USE [Billing Automation Processes]
GO
/****** Object:  StoredProcedure [dbo].[Z4032P]    Script Date: 10/15/2008 14:23:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[Z4032P]
as
begin
 
DECLARE @Email nvarchar(255)
DECLARE @Title nvarchar(100)
DECLARE @body  nvarchar(max) 
DECLARE @query nvarchar(max) 
DECLARE @attached nvarchar(max) 
DECLARE @agent nvarchar(60)
 
DECLARE c1 CURSOR READ_ONLY FOR SELECT agent, email from [Billing Automation Processes].dbo.fakeagent
 
OPEN c1
 
FETCH NEXT FROM c1 INTO @agent,@email
 
WHILE @@FETCH_STATUS = 0
 
BEGIN
 
    set @attached = @agent+'_Customer_List.txt'
    set @query = 'select convert(nchar(25),CustomerName) as CustomerName, convert(nchar(10),ANI) as ANI, convert(nvarchar(25),CreateDate) as CreateDate, convert(nchar(10),[INTER CIC]) as [INTER CIC], convert(nchar(10),[INTRA CIC]) as [INTRA CIC] from [Billing Automation Processes].dbo.[Z4032 - NO LD Notification] where agent = '''+@agent+''''
    set @body = 'Greetings '+@agent+',
 
The following circuits have been added to U4.  This email is to let you know that in the last 14 days since the circuit has been added we have not gotten traffic on the circuits.  We would like for you to look over these circuits as we are concerned that you are not getting commissions and or the customer could be getting third party billing from another carrier.  The following circuits have been setup with the following CIC codes listed below.  If you believe a CIRCUIT listed below is a non usage circuit such as a Security or Credit card line, please contact agent support and let them know the circuit and what type of line.
 
In addition provisioning has been notified about these circuits via an inquiry.
 
This report will continue to generate until the circuits receive traffic or you notify support (agentsupport@email.u4corp.com) of a change in circuit type to Switched  Low Usage.
 
 
'
 
    EXEC msdb.dbo.sp_send_dbmail
         @recipients=@Email,
		 @blind_copy_recipients= 'monitor@u4notifications.com',
         @body=@body,
         @subject='Customer List Attached',
         @profile_name='U4 Notifications.com',
         @query = @query,
         @execute_query_database = 'Billing Automation Processes',
         @attach_query_result_as_file = 0,                   
         @query_attachment_filename = @attached
     
    FETCH NEXT FROM c1 INTO @agent,@email
 
END
 
CLOSE c1
DEALLOCATE c1
 
END

Open in new window

0
Comment
Question by:binaryman101
2 Comments
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 250 total points
ID: 22724694
here you go:

I just copied the @query, and run it with an if exists() before doing the sp_send_dbmail
USE [Billing Automation Processes]
GO
/****** Object:  StoredProcedure [dbo].[Z4032P]    Script Date: 10/15/2008 14:23:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[Z4032P]
as
begin
 
DECLARE @Email nvarchar(255)
DECLARE @Title nvarchar(100)
DECLARE @body  nvarchar(max) 
DECLARE @query nvarchar(max) 
DECLARE @attached nvarchar(max) 
DECLARE @agent nvarchar(60)
 
DECLARE c1 CURSOR READ_ONLY FOR SELECT agent, email from [Billing Automation Processes].dbo.fakeagent
 
OPEN c1
 
FETCH NEXT FROM c1 INTO @agent,@email
 
WHILE @@FETCH_STATUS = 0
 
BEGIN
 
    set @attached = @agent+'_Customer_List.txt'
    set @query = 'select convert(nchar(25),CustomerName) as CustomerName, convert(nchar(10),ANI) as ANI, convert(nvarchar(25),CreateDate) as CreateDate, convert(nchar(10),[INTER CIC]) as [INTER CIC], convert(nchar(10),[INTRA CIC]) as [INTRA CIC] from [Billing Automation Processes].dbo.[Z4032 - NO LD Notification] where agent = '''+@agent+''''
    set @body = 'Greetings '+@agent+',
 
The following circuits have been added to U4.  This email is to let you know that in the last 14 days since the circuit has been added we have not gotten traffic on the circuits.  We would like for you to look over these circuits as we are concerned that you are not getting commissions and or the customer could be getting third party billing from another carrier.  The following circuits have been setup with the following CIC codes listed below.  If you believe a CIRCUIT listed below is a non usage circuit such as a Security or Credit card line, please contact agent support and let them know the circuit and what type of line.
 
In addition provisioning has been notified about these circuits via an inquiry.
 
This report will continue to generate until the circuits receive traffic or you notify support (agentsupport@email.u4corp.com) of a change in circuit type to Switched  Low Usage.
 
 
'
 
--all i did was copy the query from above.   
if exists (select convert(nchar(25),CustomerName) as CustomerName, convert(nchar(10),ANI) as ANI, convert(nvarchar(25),CreateDate) as CreateDate, convert(nchar(10),[INTER CIC]) as [INTER CIC], convert(nchar(10),[INTRA CIC]) as [INTRA CIC] from [Billing Automation Processes].dbo.[Z4032 - NO LD Notification] where agent = @agent)
    EXEC msdb.dbo.sp_send_dbmail
         @recipients=@Email,
                 @blind_copy_recipients= 'monitor@u4notifications.com',
         @body=@body,
         @subject='Customer List Attached',
         @profile_name='U4 Notifications.com',
         @query = @query,
         @execute_query_database = 'Billing Automation Processes',
         @attach_query_result_as_file = 0,                   
         @query_attachment_filename = @attached
     
    FETCH NEXT FROM c1 INTO @agent,@email
 
END
 
CLOSE c1
DEALLOCATE c1
 
END

Open in new window

0
 

Author Closing Comment

by:binaryman101
ID: 31506461
It worked!
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Date from a string 4 64
MessageHandler Database in SQL way too large 4 45
sql query help 2 51
Help with SQL - TOP 10 by date and by group 13 39
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

805 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