0

我正在尝试在 SQL Server 2008 中运行存储过程,然后通过电子邮件发送存储过程的结果,但收到以下错误:

Msg 22050, Level 16, State 1, Line 0
格式查询错误,可能参数无效
Msg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 504
查询执行失败:Msg 102, Level 15, State 1, Server XXYYZZ, Line 1
'@returnvalue' 附近的语法不正确。

这是一些要复制的示例代码:

CREATE PROCEDURE pTestEmail 
AS
        -- Create the result table - Stores the results of the stored procedure -- 

        DECLARE @returnvalue TABLE (
        ClientID varchar(5)
        );

BEGIN
    SET NOCOUNT ON;

    -- Insert some fake data -- 

    INSERT INTO @returnvalue
    VALUES ('001'),
    ('002'),
    ('003'),
    ('004'),
    ('005');

    -- Test that the fake data is in there
    -- Uncomment the next line to see it works --
    -- SELECT * FROM @returnvalue;


    -- Email the results in the @returnvalue table -- 
    EXEC msdb.dbo.sp_send_dbmail
    @execute_query_database='MainDB',
    @recipients=N'me@null.com',
    @body='Message Body', 
    @subject ='The Resultset',
    @profile_name ='Reports',
    @query ='SELECT * @returnvalue',
    @attach_query_result_as_file = 1,
    @query_attachment_filename ='Results.txt'


END
GO

我已经测试了该DBmail功能并使其正常工作。你能像我一样在存储过程中使用@标量,还是我需要使用全局临时表?

4

0 回答 0