1
私は助けが必要です、私は質問したい、電子メールの本文として2つのSQLテーブルで電子メールを送信します。しかし、最初のテーブルに結果がない場合、2番目のテーブルの結果が存在していても空のメールが私に送信されます。2つの選択結果からSQLの電子メールを送信
問題が@body = @ HTML1
はありがとうございましたとされ
IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[qdatum2]'))
DROP VIEW [dbo].[qdatum2]
GO
-- vytvorenie
CREATE VIEW qdatum2 AS
SELECT(A.Firma) AS FIRMA, A.ID, (A.OBEC) AS OBEC, (A.Ulice) AS Ulica, A.Obec2 as 'Obec dod.', A.Ulice2 AS 'Ulica dod.',
VP.IDS AS REGION,
CAST(SUM(CASE WHEN F.RelTpFak = 2 THEN -F.Kc2 ELSE F.Kc2 END) AS DECIMAL(36,2)) AS OBRAT, MAX(F.DATUM) AS MAXDATUM, MIN(F.DATUM) AS MINDATUM
FROM (StwPh_35760532_2015.dbo.AD as A INNER JOIN StwPh_35760532_2015.dbo.sVPULpol AS VP ON A.RefVPrRegion = VP.ID)
inner JOIN StwPh_35760532_2015.dbo.FA AS F ON A.ID = F.RefAD
WHERE DATEADD(MONTH, 6, F.Datum) > GETDATE()
GROUP BY A.ID, a.Firma, A.Obec, A.Ulice, A.Obec2, A.Ulice2, VP.IDS
union all
SELECT(AD.Firma) AS FIRMA, AD.ID, (AD.OBEC) AS OBEC, (AD.Ulice) AS Ulica, AD.Obec2 as 'Obec dod.', AD.Ulice2 AS 'Ulica dod.',
sVPULpol.IDS AS REGION,
CAST(SUM(CASE WHEN FA.RelTpFak = 2 THEN -FA.Kc2 ELSE FA.Kc2 END) AS DECIMAL(36,2)) AS OBRAT, MAX(FA.DATUM) AS MAXDATUM, MIN(FA.DATUM) AS MINDATUM
FROM (AD INNER JOIN sVPULpol ON AD.RefVPrRegion = sVPULpol.ID) inner JOIN FA ON AD.ID = FA.RefAD
WHERE DATEADD(MONTH, 6, FA.Datum) > GETDATE()
GROUP BY AD.ID, ad.Firma, ad.Obec, ad.Ulice,AD.Obec2,AD.Ulice2, sVPULpol.IDS
go
IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[qdatum1]'))
DROP VIEW [dbo].[qdatum1]
GO
-- vytvorenie
CREATE VIEW qdatum1 AS
select (AD.Firma) AS FIRMA, AD.ID, (AD.OBEC) AS OBEC, (AD.Ulice) AS Ulica, AD.Obec2 as 'Obec dod.', AD.Ulice2 AS 'Ulica dod.', qdatum2.REGION,
SUM(qdatum2.OBRAT) as OBRAT, max(qdatum2.MAXDATUM) as MAXDATUM, MIN(qdatum2.MINDATUM) AS MINDATUM
from AD INNER JOIN qdatum2 ON AD.ID = qdatum2.ID
group by ad.id, ad.Firma, ad.Obec, ad.Ulice, ad.Obec2, ad.Ulice2, qdatum2.region
go
IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[qdatum]'))
DROP VIEW [dbo].[qdatum]
GO
-- vytvorenie
CREATE VIEW qdatum AS
SELECT qDATUM1.*
FROM qdatum1
WHERE DATEADD(DAY, 30, qdatum1.MAXDATUM) < getdate()
and qdatum1.REGION like '%OR%'
and qdatum1.OBRAT > 50
GO
DECLARE @html varchar(max)
SET @html = '<html>'
+ '<H3><font id="Text" style="color: Green;">PREHLAD AKTIVNYCH ZAKAZNIKOV BEZ ODBERU ZA POSLEDNYCH 30 DNI </H3>'
+ '<table border="1" bordercolor="#3300FF" style="background-color:#DDF8CC" width="70%" cellpadding=3 cellspacing=3>'
+ '<tr><font color="Green"><th>Firma</th><th>Obec</th><th>Ulica</th><th>Obec dod.</th><th>Ulica dod.</th><th>Obrat v € za posledných 6 mesiacov</th><th>Posledna. Faktura</th>
</tr>' +
CAST ((SELECT
td = qdatum.FIRMA, ' ',
td = qdatum.OBEC, ' ',
td = CASE WHEN qdatum.Ulica IS NULL THEN '''' ELSE qdatum.Ulica END, ' ',
td = CASE WHEN qdatum.[Obec dod.] IS NULL THEN '''' ELSE qdatum.[Obec dod.] end , ' ',
td = CASE WHEN qdatum.[Ulica dod.] IS NULL THEN '''' ELSE qdatum.[Ulica dod.] END, ' ',
td = qdatum.OBRAT, ' ',
td = CONVERT(VARCHAR(11),qdatum.MAXDATUM, 106)
from StwPh_35760532_2016.dbo.qdatum
where qdatum.REGION like '%B1OR%'
order by qdatum.MAXDATUM
FOR XML PATH ('tr'),ELEMENTS
) AS VARCHAR(MAX))
+
'<table>'
DECLARE @html1 varchar(max)
SET @html1 = '<html>'
+ '<H3><font id="Text" style="color: Green;">PREHLAD KOMUNIKÁCIE SO ZÁKAZNÍKOM REGION B1OR </H3>'
+ '<table border="1" bordercolor="#3300FF" style="background-color:#DDF8CC" width="70%" cellpadding=3 cellspacing=3>'
+ '<tr><font color="Green"><th>Firma</th><th>Obec</th><th>Ulica</th><th>Obec dod.</th><th>Ulica dod.</th><th>Posledna navsteva</th><th>Posledne volanie</th></tr>' +
CAST((SELECT
td = AD.Firma , ' ',
td = AD.Obec, ' ',
td = CASE WHEN AD.Ulice IS NULL THEN '''' ELSE AD.Ulice END, ' ',
td = CASE WHEN AD.Obec2 IS NULL THEN '''' ELSE AD.Obec2 END, ' ',
td = CASE WHEN AD.Ulice2 IS NULL THEN '''' ELSE AD.Ulice2 END, ' ',
td = CASE WHEN CONVERT(VARCHAR (11), AD.VPrnavsteva, 106) IS NULL THEN '''' ELSE CONVERT(VARCHAR (11), AD.VPrnavsteva, 106) END, ' ',
td = CASE WHEN CONVERT(VARCHAR(11), AD.VPrvolane, 106) IS NULL THEN '''' ELSE CONVERT(VARCHAR(11), AD.VPrvolane, 106) END
FROM StwPh_35760532_2016.dbo.AD INNER JOIN StwPh_35760532_2016.dbo.sVPULpol ON AD.RefVPrRegion = sVPULpol.ID
WHERE AD.VPrnavsteva < DATEADD(DAY, -30 , GETDATE()) AND AD.VPrvolane < DATEADD(DAY, -30 , GETDATE())
AND AD.RefAD IS NULL
AND sVPULpol.IDS like '%B1OR%'
ORDER BY VPrnavsteva DESC
FOR XML PATH ('tr'),ELEMENTS
) AS VARCHAR(MAX))
+
'</table>'
+ @html
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'LACI_ADMINISTRATOR',
[email protected] = 'emailxx',
@copy_recipients = 'emailxy',
[email protected]_copy_recipients = 'emailxxy',
@subject ='B1OR report',
@body = @html1,
@body_format = 'HTML'