2013-05-20 65 views
1

你好,我想弄清楚如何設置一個SQL Server代理進程,它將選取一個由該步驟創建的文件併發送附件的電子郵件。第一步如下。我會立即執行發送郵件嗎?如果是這樣,我該如何去做。目前,我只是拿起文件併發送電子郵件,但現在我們有多個客戶端/他們希望文件更頻繁,所以我需要將其自動化。將由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

我在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'; 
相關問題