将SQL Server代理创建的文本文件附加到电子邮件中

时间:2013-05-20 15:06:02

标签: tsql sql-server-2005 smtp sql-server-agent

您好我正在尝试弄清楚如何设置一个SQL Server代理进程,该进程将获取该步骤创建的文件并发送带有附件的电子邮件。第一步如下。我会立即执行发送邮件还是另一步?如果是这样,我该怎么做呢。目前我只是拿起文件并通过电子邮件发送,但我们现在有多个客户/他们更频繁地想要文件,所以我需要自动化它。

任何方向或帮助都会很棒。

     Declare @PeriodStart Datetime
Declare @PeriodEnd Datetime
SELECT @PeriodEnd = GetDate()
,@PeriodStart = dateadd(hour,-168,getdate())

;WITH outpQ
AS
(
SELECT 1 AS grpOrd, CAST(null AS VARCHAR(255)) AS posInGrp, 'A' AS Ord, CaseNumberKey, 'A|' + ClientsKey + '|' + CAST(BRTNumber AS VARCHAR(11)) + '|' + IsNull(replace(convert(char(10), CoverDate, 101), '/', ''), '') + '|' + IsNull(replace(convert(char(10), CoverDate, 101), '/', ''), '') + '|' + IsNull(ParcelNumber, '') + '|' + IsNull(AssessedBeg, '') + '|' + IsNull(AssessedDim, '') + '|' + IsNull(AbbrLegal, '') + '|' + IsNull(WaterFrom, '') + '|' + IsNull(WaterTo, '') + '|' + IsNull(Cast(WaterOpen AS VARCHAR(50)), '') + '|' + IsNull(TaxFrom, '') + '|' + IsNull(TaxTo, '') + '|' + IsNull(Cast(TaxOpen AS VARCHAR(50)), '')  AS Extract
FROM   newCityCollection.dbo.PropertyInformation
WHERE  DateFinished BETWEEN @PeriodStart AND @PeriodEnd AND ClientKey = 2 
UNION ALL 
SELECT 1 As grpOrd, null AS posInGrp, 'A1', A.CaseNumberKey, 'A1|' + '|' + '|' + B.LienNumber + '|' +  IsNull(Cast(B.LienAmt AS VARCHAR(50)), '') + '|' + IsNull(replace(LienDate, '/', ''), '') + '|' + IsNull(B.LienReason, '') AS Extract
FROM   newCityCollection.dbo.PropertyInformation A
JOIN   newCityCollection.dbo.muniLiens B ON B.CaseNumberKey = A.CaseNumberKey
WHERE  A.DateFinished BETWEEN @PeriodStart AND @PeriodEnd AND ClientKey = 2 
UNION ALL 
SELECT 2 AS grpOrd, CAST(C.InterestsKey AS VARCHAR(11)) AS posInGrp, 'B', A.CaseNumberKey, 'B|' + '|' + IsNull(C.First, '') + '|' +  IsNull(C.Middle, '') + '|' + IsNull(C.Last, '') + '|' + IsNull(C.Alias, '') + '|' + IsNull(C.ComName, '') + '|' + IsNull(C.DocRel, '') + '|' + Cast(C.InterestsKey AS VARCHAR(11)) AS Extract
FROM   newCityCollection.dbo.PropertyInformation A
JOIN   newCityCollection.dbo.Interests C ON C.CaseNumberKey = A.CaseNumberKey
WHERE  A.DateFinished BETWEEN @PeriodStart AND @PeriodEnd AND ClientKey = 2  
UNION ALL  
SELECT 2 AS grpOrd, CAST(C.InterestsKey AS VARCHAR(11)) AS posInGrp, 'B1', A.CaseNumberKey, 'B1|' + IsNull(FullAdd, '') + '|' + IsNull(D.City, '') + '|' + IsNull(D.State, '') + '|' + IsNull(D.Zip, '') AS Extract
FROM   newCityCollection.dbo.PropertyInformation A
JOIN   newCityCollection.dbo.Interests C ON C.CaseNumberKey =  A.CaseNumberKey 
JOIN   newCityCollection.dbo.InterestAdd D ON D.CaseNumberKey = A.CaseNumberKey AND D.InterestsKey = C.InterestsKey
WHERE  A.DateFinished BETWEEN @PeriodStart AND @PeriodEnd AND ClientKey = 2 
UNION ALL 
SELECT 2 AS grpOrd, CAST(C.InterestsKey AS VARCHAR(11)) AS posInGrp, 'B2', A.CaseNumberKey, 'B2|' + '|' + IsNull(E.SuitNumber, '') + '|' +  Cast(E.BDate AS VARCHAR(11)) + '|' + IsNull(E.Chapter, '') + '|' + IsNull(E.VS, '') AS Extract
FROM   newCityCollection.dbo.PropertyInformation A
JOIN   newCityCollection.dbo.Interests C ON C.CaseNumberKey = A.CaseNumberKey
JOIN   newCityCollection.dbo.Banks E ON E.CaseNumberKey = A.CaseNumberKey  AND E.InterestsKey = C.InterestsKey
WHERE  A.DateFinished BETWEEN @PeriodStart AND @PeriodEnd AND ClientKey = 2 
UNION ALL
SELECT 3 As grpOrd3, null AS posInGrp, 'B3', A.CaseNumberKey, 'B3|' + '|' + F.DocType + '|' + IsNull(Cast(F.DocAmt AS VARCHAR(50)), '') + '|' + IsNull(replace(convert(char(10), DocDate, 101), '/', ''), '') + '|' + IsNull(replace(convert(char(10), RecDate, 101), '/', ''), '') + '|' + IsNull(F.DocID, '') + '|' + IsNull(F.Grantee,'') + '|' + IsNull(F.Grantor,'') + Cast(F.DocIDKey AS VARCHAR(11)) AS Extract
FROM   newCityCollection.dbo.PropertyInformation A                                                                                                                                                                                                                                                                                                                            
JOIN   newCityCollection.dbo.Documents F ON F.CaseNumberKey = A.CaseNumberKey
WHERE  A.DateFinished BETWEEN @PeriodStart AND @PeriodEnd AND ClientKey = 2                                          
UNION ALL
SELECT 4 AS grpOrd, null AS posInGrp, 'C', A.CaseNumberKey, 'C|' + IsNull(J.CType, '') + '|' + IsNull(J.plaintiffName,'') + '|' + IsNull(J.plaintiffAdd1, '') + '|' + IsNull(J.plaintiffCity, '') + '|' + IsNull(J.plaintiffState, '') + '|' + IsNull(J.plaintiffZip, '') + '|' + '|' + IsNull(J.defendantName, '') + '|' + IsNull(J.defendantAdd1, '') + '|' + IsNull(J.defCity, '') + '|' + IsNull(J.defState, '') + '|' + IsNull(J.defZip, '') + '|' +  '|' + IsNull(J.Court, '') + '|' + IsNull(J.CaseID, '') + '|' + IsNull(J.JAmt, '') + '|' + IsNull(replace(convert(VarChar(10), JDate, 101), '/', ''), '') + '|' + IsNull(replace(convert(VARCHAR(10), revivedDate, 101), '/', ''), '') AS Extract
FROM   newCityCollection.dbo.PropertyInformation A 
JOIN   Acme.new_judgment_system.dbo.selected_compiled_clean J ON J.CaseNumber = A.CaseNumberKey
WHERE  A.DateFinished BETWEEN @PeriodStart AND @PeriodEnd AND ClientKey = 2 AND J.plaintiffName NOT IN (SELECT Plaintiff FROM newCityCollection.dbo.excluded_Plaintiffs)
)

--Extract data set into a table -- dump table in .txt with current date as part of name then delete that table

SELECT Extract INTO datadump FROM outpQ ORDER BY CaseNumberKey, grpOrd, posInGrp, Ord
DECLARE @FileName varchar(50),
        @bcpCommand varchar(2000)

SET @FileName = REPLACE('D:\Argosy_import_'+CONVERT(char(8),GETDATE(),1)+'_0001.txt','/','')

SET @bcpCommand = 'bcp "SELECT Extract FROM datadump" QUERYOUT "'
SET @bcpCommand = @bcpCommand + @FileName + '" -U ******* -P *********** -T -c'

EXEC master..xp_cmdshell @bcpCommand

DROP table datadump 

1 个答案:

答案 0 :(得分:1)

我在Drop Table Dump之后添加了它,它似乎有效。

EXEC msdb.dbo.sp_send_dbmail
 @recipients=N'******',
@body='Message Body', 
 @subject ='Import file for @PeriodStart',
@profile_name ='Mail',
@file_attachments ='D:Argosy_import_051713_0001.txt';