附件中使用的查询生成的文件名

时间:2013-05-20 15:43:52

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

我有这个在SQL Server Agent中运行的查询,我需要能够获取正确的文件。目前我让它拿起D驱动器上的随机文件,但我需要它来获取刚刚创建的文件。该代码如下所示:

'D:\Argosy_import_'+CONVERT(char(8),GETDATE(),1)+'_0001.txt','/',''

我怎样才能在这里工作:

@file_attachments = 'D:Argosy_import_051713_0001.txt';

此外,我尝试使用参数作为主题中的日期,但也无效。我怎样才能使这个工作/;

     @subject = 'Import file for @PeriodStart',

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

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 


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';

0 个答案:

没有答案