需要协助存储过程

时间:2015-08-06 04:41:29

标签: sql sql-server stored-procedures

我编写了一个存储过程来向收件人发送警报。警报包含在该时间点尚未执行完整性测试的应用程序列表。但是我想让我的代码工作就像列表是空的意味着没有应用程序没有完成测试,它不应该触发警报。

目前,警报是通过一个空表触发的,只有标题。

有人可以帮帮我吗?提前谢谢。

以下是代码:

CREATE PROCEDURE [dbo].[sp_Alert_BellTV_Chip Report]
AS
    declare @HtmlBody varchar(max) = null

    select appname, metalplating 
    from v_Escalation_lvl_3 
    where LVL3_ESCLS = 'Bell TV'

BEGIN
       create table dbo.##BellTV_Application(Position int identity(1,1),html varchar(max))

       insert into dbo.##BellTV_Application(html)
       select distinct('<tr style="color:White;background-color:#666666;white-space:nowrap;"><td>'+appname+'</td><td>'+metalplating+'</td></tr>') 
       from dbo.v_Escalation_lvl_3
       where LVL3_ESCLS = 'Bell TV'

            DECLARE @cnt_2 INT = 1;
            declare @subject varchar(max)='Chip Report'

            set @HtmlBody=''
            set @HtmlBody='<table cellspacing="0" cellpadding="4" border="0" style="color:#333333;font-family:Century Gothic;width:50%;border-collapse:collapse;">
            <tr><td>Hi</td></tr><tr><td></td></tr><tr><td colspan=\"2\">Could you please perform a sanity test for the below applications.</td></tr><tr><td></td></tr>
            <tr style="color:White;background-color:#336699;"><td>Application Name</td><td>Metal Plating</td></tr>'

            while @cnt_2 <=(select  count(*) from dbo.##BellTV_Application)
            begin
            set @HtmlBody=@HtmlBody+(select html from dbo.##BellTV_Application where Position=@cnt_2)
            SET @cnt_2 = @cnt_2 + 1;
            end

            set @HtmlBody=@HtmlBody+'<tr><td>Thanks </td></tr><tr><td>Sanity Tool</td></tr></table>'
            select @HtmlBody
            drop table  dbo.##BellTV_Application                    

            end 
            if @HtmlBody<>'' or @HtmlBody is not null
            begin

            declare @aemail varchar(Max)
            set @aemail ='recipeints'

            EXEC sp_sendEmailToStartTesting
                    @mailRecipients = @aemail,
                    @mailbody = @htmlbody, 
                    @mailSubject = @subject
END
Go

2 个答案:

答案 0 :(得分:2)

这样的东西
BEGIN
   create table dbo.##BellTV_Application(Position int identity(1,1),html varchar(max))
   insert into dbo.##BellTV_Application(html)
   select distinct('<tr style="color:White;background-color:#666666;white-space:nowrap;"><td>'+appname+'</td><td>'+metalplating+'</td></tr>') 
   from dbo.v_Escalation_lvl_3
   where LVL3_ESCLS = 'Bell TV'

        DECLARE @cnt_2 INT = 1;
        DECLARE @cntTotal INT

        SELECT @cntTotal = count(*) from dbo.##BellTV_Application
  IF @cntTotal >0
      BEGIN
        declare @subject varchar(max)='Chip Report'
        set @HtmlBody=''
        set @HtmlBody='<table cellspacing="0" cellpadding="4" border="0" style="color:#333333;font-family:Century Gothic;width:50%;border-collapse:collapse;">
        <tr><td>Hi</td></tr><tr><td></td></tr><tr><td colspan=\"2\">Could you please perform a sanity test for the below applications.</td></tr><tr><td></td></tr>
        <tr style="color:White;background-color:#336699;"><td>Application Name</td><td>Metal Plating</td></tr>'

        while @cnt_2 <=@cntTotal
        begin
        set @HtmlBody=@HtmlBody+(select html from dbo.##BellTV_Application where Position=@cnt_2)
        SET @cnt_2 = @cnt_2 + 1;
        end

        set @HtmlBody=@HtmlBody+'<tr><td>Thanks </td></tr><tr><td>Sanity Tool</td></tr></table>'
        select @HtmlBody
        drop table  dbo.##BellTV_Application                    

        end 
        if @HtmlBody<>'' or @HtmlBody is not null
        begin

        declare @aemail varchar(Max)
        set @aemail ='recipeints'

        EXEC sp_sendEmailToStartTesting
                @mailRecipients = @aemail,
                @mailbody = @htmlbody, 
                @mailSubject = @subject
END   END  GO

答案 1 :(得分:1)

我认为不应该检查@HtmlBody,而应检查@ cnt_2值(最初是1)

if @HtmlBody<>'' or @HtmlBody is not null
            begin
            ..........

你应该使用。

 if  @cnt_2>1
            begin
            ..........