SQL - IF COUNT > 0 BEGIN

 
Vista:
sin imagen de perfil
Val: 7
Ha aumentado su posición en 7 puestos en SQL (en relación al último mes)
Gráfica de SQL

IF COUNT > 0 BEGIN

Publicado por leo (3 intervenciones) el 29/04/2021 21:47:29
Buenas tardes para todos. Cómo hago para que esta query se ejecute si sólo si tiene registros el select que está en el cursor? De lo contrario me llegan mails sólo con el header que declaro en el HTML.


Muchas gracias!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
declare @HTMLbody nvarchar(max),
		@vtrmvh_codfor varchar(6),
		@vtrmvh_nrofor varchar(10),
		@vtrmvh_fchmov varchar(50),
		@SQL nvarchar(max)
 
set @SQL = ''
 
set @HTMLbody = '<html><head><title>Pendientes CAE</title></head><body>'
set @HTMLbody = @HTMLbody + '<center><h1>Pendientes CAE</h1></center>'
 
set @HTMLbody = @HTMLbody + '<center><table><TH style="background-color:black; color:white">CODFOR</TH><TH style="background-color:black; color:white">NROFOR</TH><TH style="background-color:black; color:white">FECHA</TH>'
 
DECLARE pendienteCAE cursor
read_only
for
 
 
select top 20 vtrmvh_codfor,convert(varchar(10),VTRMVH_NROFOR),vtrmvh_fchmov from VTRMVH
where VTRMVH_CODFOR in (
'CCA005',
'CB0004',
'FB0004',
'DA0005',
'DB0005',
'FB0005',
'CB0005',
'CE0003',
'FCA005',
'DB0004',
'FA0005',
'CA0005',
'DCA005',
'FE0003',
'DE0003') and
VTRMVH_NROCAE is null and
VTRMVH_FCHMOV >= DATEADD(DAY,-10,GETDATE())
order by VTRMVH_FCHMOV desc
 
 
open pendienteCAE
 
fetch next from pendienteCAE into @vtrmvh_codfor,@vtrmvh_nrofor,@vtrmvh_fchmov
 
 
 
WHILE (@@fetch_status <> -1)
 
BEGIN
 
set @SQL = @SQL + '<TR><TD style="border:1; border-style:ridge">' + @VTRMVH_CODFOR + '</TD><TD  style="border:1; border-style:ridge">' +  @VTRMVH_NROFOR + '</TD><TD  style="border:1; border-style:ridge">' +  @VTRMVH_FCHMOV + '</TD></TR>'
 
fetch next from pendienteCAE into @vtrmvh_codfor,@vtrmvh_nrofor,@vtrmvh_fchmov
 
END
 
CLOSE pendienteCAE
deallocate pendienteCAE
 
set @HTMLbody = @HTMLbody + @SQL +  '</table></center></body></html>'
 
 
 
 
 
exec msdb.dbo. sp_send_dbmail
@profile_name = 'Auto Verificaciones',
@recipients = 'lsordello@marlew.com.ar',
@subject = 'Pendientes CAE',
@body = @HTMLbody,
@body_format = 'HTML',
@attach_query_result_as_file = 0 ;
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Isaias
Val: 2.542
Oro
Ha mantenido su posición en SQL (en relación al último mes)
Gráfica de SQL

IF COUNT > 0 BEGIN

Publicado por Isaias (1921 intervenciones) el 30/04/2021 01:49:05
¿En que motor de base de datos estas trabajando?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar