SQL Server 2008 でストアド プロシージャを実行し、ストアド プロシージャの結果を電子メールで送信しようとしていますが、次のエラーが表示されます。
メッセージ 22050、レベル 16、状態 1、行 0
クエリのフォーマット エラー、おそらく無効なパラメーター
メッセージ 14661、レベル 16、状態 1、プロシージャ sp_send_dbmail、行 504
クエリの実行に失敗しました: メッセージ 102、レベル 15、状態 1、サーバー XXYYZZ、行 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
機能をテストし、正しく動作するようになりました。私のようにストアド プロシージャで @ スカラーを使用できますか、それともグローバル一時テーブルを使用する必要がありますか?