1
I have a problem at the time of putting the query in the body of the email, this giving me error asking for EXISTS in the query how I can solve it.
Follows my code.
DECLARE @p_body as nvarchar(max), @p_subject as nvarchar(max)
DECLARE @p_recipients as nvarchar(max), @p_profile_name as nvarchar(max)
SET @p_profile_name = N'Estoque Minimo'
SET @p_recipients = N'[email protected]'
SET @p_subject = N'Teste de envio'
SET @p_body = '<b>Segue em anexo o Relatório de Produtos abaixo doestqoeu mínimo.</b>'
+ '<table border=1 width="100%"><tr><th colspan="8" bgcolor="darkblue">'
+ '<h3>Relatorio de Estoque para Compras </h3></th></tr>' --titulo da tabela
+ '<tr bgcolor="lightblue"><th>Produto<th>Saldo<th><th>Saldo em Compras<th><th><th>Estque Minimo<th></tr>' --colunas do email
+ ( SELECT DISTINCT
SB1.B1_DESC AS PRODUTO,
SUM(SB8.B8_SALDO) AS SALDO,
(SC7.C7_QUANT - SC7.C7_QUJE) AS [SALDO DE COMPRAS],
SC7.C7_NUM AS PEDIDO,
SB1.B1_EMIN AS [SALDO MINIMO EM ESTOQUE]
FROM DADOSADV12.dbo.SB1010 AS SB1
INNER JOIN DADOSADV12.dbo.SB8010 AS SB8 WITH(NOLOCK) ON SB8.B8_PRODUTO = SB1.B1_COD
INNER JOIN DADOSADV12.dbo.SC7010 AS SC7 WITH(NOLOCK) ON SC7.C7_PRODUTO = SB1.B1_COD
WHERE SB1.B1_COD = '000070'
AND SB8.B8_LOCAL IN ('01', '95')
AND SC7.C7_ENCER <> 'E'
AND SB1.D_E_L_E_T_ = ''
AND SB8.D_E_L_E_T_ = ''
AND SC7.D_E_L_E_T_ = ''
GROUP BY SB1.B1_DESC, SB1.B1_EMIN, SC7.C7_QUANT, SC7.C7_QUJE, SC7.C7_NUM)
+ '<tr><td colspan="8" bgcolor="darkblue" align="right">'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = @p_profile_name,
@recipients = @p_recipients,
@body = @p_body,
@body_format = 'HTML',
@subject = @p_subject
GO
can throw error ?
– Marco Souza
Follow message "Message 116, Level 16, State 1, Line 27 ."
– Junior Guerreiro
if you run only your select it is working ? try to do only SET @p_body = ... so it seems your concatenation is in trouble.
– Marco Souza
If I just do @p_body works yes without problems, the error started to appear after I put the query in body. And select is working normally yes.
– Junior Guerreiro
is prq vc does select from several columns and does not concatenate them.
– Marco Souza
Mraconcilio may be yes, but I have never done this way a query, you can explain me.
– Junior Guerreiro