如何在这种情况下进行动态查询。 sql 2005
How can do a dynamic query in this scenario . sql 2005
我正在尝试从查询中输入用户 ID 以向他们发送电子邮件。
它不起作用并给我这个错误
Msg 14624, Level 16, State 1, Procedure sp_send_dbmail, Line 238 At
least one of the following parameters must be specified. "@recipients,
@copy_recipients, @blind_copy_recipients".
declare @bodymsg nvarchar(max)
select @bodymsg = '<font face="calibiri" size="4" >Dear Users</font><br><br>
<font face="calibiri" size="5" color="red">Please Explain the Exrta Locked Faxes</font><br><br>
<font face="calibiri" size="4" >Check the Last Hour Snapshot Details Attached.<br><br>
Thanks</font></end>'
declare @users_fetched varchar(max)
set @users_fetched = 'SELECT distinct Locked_Faxes_Last_Hour_Snap.userid from Locked_Faxes_Last_Hour_Snap'
declare @recipients varchar(max)
SELECT
@recipients = STUFF((SELECT ';' + concerned_staff.staff_email from concerned_staff where (concerned_staff.staff_id in ('@users_fetched'))
FOR XML PATH('')
), 1, 1, '')
EXEC msdb.dbo.sp_send_dbmail
@recipients = @recipients ,
@body= @bodymsg ,
@subject = 'Alert !!! Locked Faxes Violation Last Hour Snaps' ,
@profile_name = 'Database Profile 1',
@query = 'use qtel select * from dbo.Locked_Faxes_Last_Hour_Snap' ,
@attach_query_result_as_file = 1,
@query_attachment_filename ='Locked_Faxes_Last_Hour_Snap.csv',
@query_result_separator =',',
@query_result_no_padding=1,
@exclude_query_output=1,
@append_query_error=0,
@query_result_header =1,
@body_format ='HTML',
@importance= 'HIGH';
此处不需要动态 sql。当您创建收件人时,只需将获取用户的查询作为子查询中的 where 条件:
SELECT
@recipients = STUFF((SELECT ';' + concerned_staff.staff_email
from concerned_staff
where concerned_staff.staff_id in
(
SELECT Locked_Faxes_Last_Hour_Snap.userid
from Locked_Faxes_Last_Hour_Snap
)
FOR XML PATH('')
), 1, 1, '')
我正在尝试从查询中输入用户 ID 以向他们发送电子邮件。 它不起作用并给我这个错误
Msg 14624, Level 16, State 1, Procedure sp_send_dbmail, Line 238 At least one of the following parameters must be specified. "@recipients, @copy_recipients, @blind_copy_recipients".
declare @bodymsg nvarchar(max)
select @bodymsg = '<font face="calibiri" size="4" >Dear Users</font><br><br>
<font face="calibiri" size="5" color="red">Please Explain the Exrta Locked Faxes</font><br><br>
<font face="calibiri" size="4" >Check the Last Hour Snapshot Details Attached.<br><br>
Thanks</font></end>'
declare @users_fetched varchar(max)
set @users_fetched = 'SELECT distinct Locked_Faxes_Last_Hour_Snap.userid from Locked_Faxes_Last_Hour_Snap'
declare @recipients varchar(max)
SELECT
@recipients = STUFF((SELECT ';' + concerned_staff.staff_email from concerned_staff where (concerned_staff.staff_id in ('@users_fetched'))
FOR XML PATH('')
), 1, 1, '')
EXEC msdb.dbo.sp_send_dbmail
@recipients = @recipients ,
@body= @bodymsg ,
@subject = 'Alert !!! Locked Faxes Violation Last Hour Snaps' ,
@profile_name = 'Database Profile 1',
@query = 'use qtel select * from dbo.Locked_Faxes_Last_Hour_Snap' ,
@attach_query_result_as_file = 1,
@query_attachment_filename ='Locked_Faxes_Last_Hour_Snap.csv',
@query_result_separator =',',
@query_result_no_padding=1,
@exclude_query_output=1,
@append_query_error=0,
@query_result_header =1,
@body_format ='HTML',
@importance= 'HIGH';
此处不需要动态 sql。当您创建收件人时,只需将获取用户的查询作为子查询中的 where 条件:
SELECT
@recipients = STUFF((SELECT ';' + concerned_staff.staff_email
from concerned_staff
where concerned_staff.staff_id in
(
SELECT Locked_Faxes_Last_Hour_Snap.userid
from Locked_Faxes_Last_Hour_Snap
)
FOR XML PATH('')
), 1, 1, '')