Email automatico no sql server 2012

Asked

Viewed 34 times

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 ?

  • Follow message "Message 116, Level 16, State 1, Line 27 ."

  • if you run only your select it is working ? try to do only SET @p_body = ... so it seems your concatenation is in trouble.

  • 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.

  • is prq vc does select from several columns and does not concatenate them.

  • Mraconcilio may be yes, but I have never done this way a query, you can explain me.

Show 1 more comment

1 answer

0


You will need to manipulate your query to be able to assemble your table the way you need, the way I see to do this is with a Cursor, would be like this.

declare @tabelaEx table 
(
  PRODUTO varchar(100),
  SALDO int,
  SALDODECOMPRAS int,
  PEDIDO int,
  SALDOMINIMOEMESTOQUE int
)

insert into @tabelaEx values
('produto 1',11,4,1,100),
('produto 2',11,4,2,5),
('produto 3',11,4,1,2);

DECLARE @dadosProduto varchar(max) = '' , @PRODUTO varchar(100), @SALDO int, @SALDODECOMPRAS int, @PEDIDO int, @SALDOMINIMOEMESTOQUE int


DECLARE cursor_ CURSOR FOR
select PRODUTO, SALDO, SALDODECOMPRAS, PEDIDO , SALDOMINIMOEMESTOQUE from @tabelaEx


OPEN cursor_


FETCH NEXT FROM cursor_ INTO  @PRODUTO, @SALDO, @SALDODECOMPRAS, @PEDIDO, @SALDOMINIMOEMESTOQUE 

WHILE @@FETCH_STATUS = 0
BEGIN

set @dadosProduto = @dadosProduto + '<tr> <td>' + (@PRODUTO +'</td> <td>' + cast(@SALDO as varchar) +'</td> <td>' + cast(@SALDODECOMPRAS as varchar) +'</td> <td>' + cast(@PEDIDO as varchar)  +'</td> <td>' + cast(@SALDOMINIMOEMESTOQUE as varchar) )+'</td> </tr>'


FETCH NEXT FROM cursor_ INTO  @PRODUTO, @SALDO, @SALDODECOMPRAS, @PEDIDO, @SALDOMINIMOEMESTOQUE 
END


CLOSE cursor_

DEALLOCATE cursor_

select @dadosProduto

The result would be the body of your table.

<tr> 
    <td>produto 1</td> 
    <td>11</td> 
    <td>4</td> 
    <td>1</td> 
    <td>100</td> 
</tr>
<tr> 
    <td>produto 2</td> 
    <td>11</td> 
    <td>4</td> 
    <td>2</td> 
    <td>5</td> 
</tr>
<tr> 
    <td>produto 3</td> 
    <td>11</td> 
    <td>4</td> 
    <td>1</td> 
    <td>2</td> 
</tr>

Change the example table table Ex by your select and then use the select @dadosProduto in place of the select you are making.

  • 1

    Thank you very much Marconcilio for your attention.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.