我正在运行 SQL 作业 (SQL Server 20208 R2),我希望将脚本的结果保存到以管道分隔的文件夹中。有没有办法通过添加到脚本中来做到这一点?下面是脚本。如果不行我可以用其他方法吗?
SELECT Distinct
(SELECT REPLACE(CONVERT(VARCHAR(10),PatientDemographics.DateofBirth,101),'/','')) as DOB
, (SELECT REPLACE(CONVERT(VARCHAR(10),ScheduleEntry.ScheduleDate,101),'/','')) as DateofService
, RTRIM(LTRIM(Insurances.EligibilityPayorNumber)) as InsurancePayorCode
, RTRIM(LTRIM(ContractFacilityProviders.NPI)) as ProviderID
, RTRIM(LTRIM(PatientInsuranceProfiles.Insurance1PolicyNumber)) as SubscriberInsuranceID
, RTRIM(LTRIM(PatientInsuranceProfiles.Insurance1PolicyGroupNumber)) as SubscriberGroupNumber
, RTRIM(LTRIM(PatientDemographics.firstname)) as SubscriberFirstName
, RTRIM(LTRIM(PatientDemographics.MiddleInitial)) as SubscriberMiddleInitial
, RTRIM(LTRIM(PatientDemographics.Lastname)) as SubscriberLastName
FROM
ScheduleEntry
LEFT JOIN
PatientDemographics ON ScheduleEntry.PatientAccount = PatientDemographics.AccountNumber
LEFT JOIN
Providers ON ScheduleEntry.ResourceCode = Providers.MedStarProviderIdentifier
LEFT JOIN
Facilities ON ScheduleEntry.FacilityCode = Facilities.MedStarFacilityIdentifier
LEFT JOIN
AddedResource ON ScheduleEntry.ResourceCode = AddedResource.AddedResourceCode
LEFT JOIN
Caregiver ON ScheduleEntry.ResourceCode = Caregiver.CaregiverCode
LEFT JOIN
PatientInsuranceProfiles ON ScheduleEntry.PatientAccount = PatientInsuranceProfiles.PatientAccountNumber
LEFT JOIN
Insurances ON PatientInsuranceProfiles.Insurance1Mnemonic = Insurances.Mnemonic
LEFT JOIN
ContractFacilityProviders ON PatientDemographics.PrimaryPhysician = ContractFacilityProviders.ProviderIdentifier
WHERE
ScheduleEntry.ScheduleDate >= getdate()
AND ScheduleEntry.ScheduleDate <= getDate() + .50
AND PatientInsuranceProfiles.Insurance1ContractIdentifier = ContractFacilityProviders.ContractIdentifier
AND PatientinsuranceProfiles.ActiveFlag = 1
AND EligibilityPayorNumber > 1
AND ContractFacilityProviders.NPI > 1
ORDER BY
SubscriberLastName
您可以使用 xp_cmdshell 之类的东西......
SELECT DISTINCT
--your columns
INTO ##PIPE_FILE
FROM YourTables
INNER JOIN YourOtherTables
EXECUTE master.dbo.xp_cmdshell
'bcp "SELECT * FROM ##PIPE_FILE " queryout C:\Folder\Pipes.txt -t"|" -c -T '
几个想法
1)编写一个SSIS包导出到一个平面文件,使用管道作为分隔符。 2)如前所述使用BCP 3)使用sqlcmd输出结果
SSIS 将是 SQL 作业的一个很好的解决方案。
我也会回应之前的发帖者,他们建议检查您是否没有通过使用 where 子句中的表来否定左连接的影响
编辑:如果您无权访问 xp_cmdshell(许多 DBA 将其 关闭)
以下内容将获取 #TempPipe 的结果,并将其动态保存为分隔的管道(带有标题 - 可选)到您所需的位置。
现在,您将需要下面列出的存储过程以及适当的写入。
-- Assumes your query will be saved to #TempPipe
Select * Into #TempPipe from OD
-- Set Destination File
Declare @Destination varchar(250) = 'C:\Working\Test_Pipe.txt'
-- Construct Field Name and Header from #TempPipe
Declare @Header varchar(max) = '>>>'
Select @Header = @Header +']|['+Name from (select * from tempdb.sys.columns where object_id = object_id('tempdb..#TempPipe') ) A Order by column_id
Select @Header = Replace(@Header,'>>>]|','')+']'
Declare @SelectHead varchar(max) = 'Select Pipe=cast(Replace(Replace('''+@Header+''',''['',''''),'']'','''') as varchar(max)) '
Declare @SelectData varchar(max) = 'Select Pipe=cast(concat('+Replace(@Header,'|',',''|'',')+') as varchar(max)) from #TempPipe '
--If Version<2012 Declare @SelectData varchar(max) = 'Select Pipe=cast(cast('+Replace(@Header,'|',' as varchar(500))+''|''+cast(')+' as varchar(500)) as varchar(max)) from #TempPipe '
Declare @SQL varchar(max) ='
Declare @String varchar(max) = '''';
Select @String = @String + Pipe + char(13)+char(10) From ('+@SelectHead+' Union All '+@SelectData+' ) A
Exec [dbo].[prc-Write-To-File] @String, ''' + @Destination +'''
'
--Select @SQL
Exec(@SQL)
将任何字符串写入任何文件的存储过程
EXEC sp_configure 'show advanced options', 1
GO
EXEC sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
CREATE Procedure [dbo].[prc-Write-To-File] (@text as Varchar(Max), @Filename Varchar(200)) AS
Begin
Declare @Object int,@rc int,@FileID Int
EXEC @rc = sp_OACreate 'Scripting.FileSystemObject', @Object OUT
EXEC @rc = sp_OAMethod @Object , 'OpenTextFile' , @FileID OUT , @Filename , 2 , 1
Set @text = Replace(Replace(Replace(@text,'&','&'),'<' ,'<'),'>','>')
EXEC @rc = sp_OAMethod @FileID , 'WriteLine' , Null , @text
Exec @rc = [YourDatabaseName].dbo.sp_OADestroy @FileID
Declare @Append bit
Select @Append = 0
If @rc <> 0
Begin
Exec @rc = [YourDatabaseName].dbo.sp_OAMethod @Object, 'SaveFile',null,@text ,@Filename,@Append
End
Exec @rc = [YourDatabaseName].dbo.sp_OADestroy @Object
End
生成的文本文件的小样本
ODNr|ODID|ODClass|ODTitle|ODLMUsr|ODLMUTC|ODDeleted
0|UNDEF|UNDEF|Undefined|2|Apr 5 2016 1:40PM|0
1|SYSTEM|UNDEF|System|2|Apr 5 2016 1:40PM|0
2|UNDEF|USER|Cappelletti, John|2|Apr 12 2016 7:04PM|0
3|UNDEF|UNDEF|Daily Production Summary|2|Apr 14 2016 12:28PM|0