具有 "select count(*)..." 的灵​​活参数的 EXEC 存储过程并发送电子邮件

EXEC stored procedure with flexible parameter for "select count(*)..." and sending an eMail

我想写一个存储过程来在 select count(*) 值大于值 Menge 时发送电子邮件。

由于更多表的参数,这个存储过程应该灵活使用and/or "where criteria"

不幸的是,我遇到了这个错误,我无法修复它:(

Msg 245, Level 16, State 1, Procedure sp_eMail_Test3, Line 23
Conversion failed when converting the varchar value 'select count(*) from test where not [sys_completed] is null' to data type int.

你能帮帮我吗?

我的存储过程:

create PROCEDURE [dbo].[sp_eMail_Test3]
 @recordCount as int = 0, 
 @FileN as varchar(max) =null,
 @DatSubj as varchar(20) =null,
 @DatFile as varchar(20) =null,
 @SQL_Count as varchar(max) =null,

 @MySQL as varchar(max)=null,
 @MyTable as varchar(max)=null,
 @MyWhere as varchar(max)=null,

 @Menge as int = 0,

 @eMail_TO varchar(max) =null,
 @eMail_Subject varchar(max) =null,
 @eMail_Body varchar(max) =null

AS
BEGIN
    SET NOCOUNT ON;     
    set @MySQL = 'select count(*) from ' +@MyTable + ' where ' + @MyWhere 
    set @SQL_Count = @MySQL 
    set @recordCount = convert(int, @SQL_Count ) -- <<--this is the error

    IF (@recordCount > @Menge)  
    begin           
        -- variablen zuweisung
        set @DatSubj =  CONVERT(varCHAR(20),  GETDATE() ,120) --datum fürs subject
        set @DatFile =  replace(convert(varchar, getdate(), 120), ':','_') --datum für filename
        set @FileN ='Eska_Report_' + @DatFile +'.csv' --filename

        EXEC msdb.dbo.sp_send_dbmail
            @body_format = 'HTML',
            @profile_name = 'testmailprofile',
            @recipients = @eMail_TO,         
            @subject =  @eMail_Subject ,
            @Body = @eMail_Body  ,
            @query = 'SET NOCOUNT ON;
                        select * from Test where sys_gueltig = ''Y'' 
                        and not sys_completed is null  ',
         @attach_query_result_as_file = 1       , 
         @query_attachment_filename= @FileN     , 
         @query_result_separator = ';'      ,
         @query_result_no_padding= 1,       
         @exclude_query_output =1,      
         @append_query_error = 1,   
         @query_result_header =1
    end    
END

我是这样调用SP的

exec sp_eMail_Test3
    @Menge = 0,
    @eMail_TO = 'testuser@test.xx' ,
    @eMail_Subject = 'test3 ',
    @eMail_Body = 'Hallo, das ist ein Test',
    @MyTable ='test'    ,
    @MyWhere = 'not [sys_completed] is null'

以后想在VBA中通过ADO连接调用存储过程

这是一个错误,因为:

set @recordCount = convert(int, @SQL_Count ) 

正在尝试将您的 SQL 语句 (select blah blah) 从字符串转换为整数。

执行存储过程后尝试执行Select @@ROWCOUNT;

我觉得有必要告诉您,像这样将变量传递给动态字符串会让您面临 SQL 注入。这是一种不好的做法,除非受到非常严格的控制,否则通常不受欢迎。

也就是说...

将您的@MySQL 从 varchar 更改为 nvarchar。

然后尝试改变这个:

set @MySQL = 'select count(*) from ' +@MyTable + ' where ' + @MyWhere 
set @SQL_Count = @MySQL 
set @recordCount = convert(int, @SQL_Count ) -- <<--this is the error

为此:

set @MySQL = 'select @recordCount=count(2) from ' + @MyTable + ' where ' + @MyWhere 
exec sp_execute @MySQL, N'@recordCount int OUTPUT', @recordCount=@recordCount OUTPUT