检查文件内容 SQL Server 2008 R2

Check file content SQL Server 2008 R2

我们有一个接口,每天创建三个文件,名称格式如下:

  1. FileAYYYYMMDD(示例:FileA20170925)
  2. FileBYYYYMMDD(示例:FileB20170925)
  3. FileCYYYYMMDD(示例:FileC20170925)

需要有人手动检查这些文件是否为空。我想自动执行此步骤。

我想知道的: 有没有办法通过 SQL 查询或 SQL 作业来确定这些文件是否为空(是或否就足够了)?

根据 MatSnow 的建议,我能够 find/create spFileDetails 存储过程。使用这个存储过程,我能够创建 SSRS 报告,这将帮助我自动化该过程。此外,我什至可以通过传递日期参数来评估历史文件:

declare @FileDATE AS VARCHAR(8) 
select @FileDATE = (SELECT CONVERT(VARCHAR(10), @ReportDate, 112))

DECLARE @FolderLocation as varchar(200) 
select @FolderLocation =  ( '\share\Validation\File' + @FileDATE + '.txt')

DECLARE @FolderLocationA as varchar(200) 
select @FolderLocationA = ( '\share\Validation\FileA' + @FileDATE + '.txt')

DECLARE @FolderLocationB as varchar(200) 
select @FolderLocationB = ( '\share\Validation\FileB' + @FileDATE + '.txt')


DECLARE @TempTable Table(
[Path] varchar(300),    
ShortPath   varchar(300),
[Type]  varchar(20),
DateCreated  date,
DateLastAccessed date,  
DateLastModified    date,
Attributes  int , 
size int)

INSERT INTO @TempTable
Execute dba.dbo.uspGetFileDetails  @FolderLocation
INSERT INTO @TempTable
Execute dba.dbo.uspGetFileDetails  @FolderLocationA
INSERT INTO @TempTable
Execute dba.dbo.uspGetFileDetails  @FolderLocationB

Select * from @TempTable

您应该能够使用存储过程 spFileDetails 从此 site.

获取文件大小

如果文件大小为 0,则文件为空。 这是存储过程的副本。感谢"Phil Factor"

/****** Object:  StoredProcedure [dbo].[spFileDetails]    Script Date: 03/28/2007 15:28:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spFileDetails]
@Filename VARCHAR(100)

/*
spFileDetails 'c:\autoexec.bat'
*/
AS
DECLARE @hr INT,         --the HRESULT returned from 
       @objFileSystem INT,              --the FileSystem object
       @objFile INT,            --the File object
       @ErrorObject INT,        --the error object
       @ErrorMessage VARCHAR(255),--the potential error message
       @Path VARCHAR(100),--
       @ShortPath VARCHAR(100),
       @Type VARCHAR(100),
       @DateCreated datetime,
       @DateLastAccessed datetime,
       @DateLastModified datetime,
       @Attributes INT,
       @size INT



SET nocount ON

SELECT @hr=0,@errorMessage='opening the file system object '
EXEC @hr = sp_OACreate 'Scripting.FileSystemObject',
                                       @objFileSystem OUT
IF @hr=0 SELECT @errorMessage='accessing the file '''
                                       +@Filename+'''',
       @ErrorObject=@objFileSystem
IF @hr=0 EXEC @hr = sp_OAMethod @objFileSystem,
         'GetFile',  @objFile out,@Filename
IF @hr=0 
       SELECT @errorMessage='getting the attributes of '''
                                       +@Filename+'''',
       @ErrorObject=@objFile
IF @hr=0 EXEC @hr = sp_OAGetProperty 
             @objFile, 'Path', @path OUT
IF @hr=0 EXEC @hr = sp_OAGetProperty 
             @objFile, 'ShortPath', @ShortPath OUT
IF @hr=0 EXEC @hr = sp_OAGetProperty 
             @objFile, 'Type', @Type OUT
IF @hr=0 EXEC @hr = sp_OAGetProperty 
             @objFile, 'DateCreated', @DateCreated OUT
IF @hr=0 EXEC @hr = sp_OAGetProperty 
             @objFile, 'DateLastAccessed', @DateLastAccessed OUT
IF @hr=0 EXEC @hr = sp_OAGetProperty 
             @objFile, 'DateLastModified', @DateLastModified OUT
IF @hr=0 EXEC @hr = sp_OAGetProperty 
             @objFile, 'Attributes', @Attributes OUT
IF @hr=0 EXEC @hr = sp_OAGetProperty 
             @objFile, 'size', @size OUT


IF @hr<>0
       BEGIN
       DECLARE 
               @Source VARCHAR(255),
               @Description VARCHAR(255),
               @Helpfile VARCHAR(255),
               @HelpID INT

       EXECUTE sp_OAGetErrorInfo  @errorObject, 
               @source output,@Description output,
                               @Helpfile output,@HelpID output

       SELECT @ErrorMessage='Error whilst '
                               +@Errormessage+', '
                               +@Description
       RAISERROR (@ErrorMessage,16,1)
       END
EXEC sp_OADestroy @objFileSystem
EXEC sp_OADestroy @objFile
SELECT [Path]=  @Path,
       [ShortPath]=    @ShortPath,
       [Type]= @Type,
       [DateCreated]=  @DateCreated ,
       [DateLastAccessed]=     @DateLastAccessed,
       [DateLastModified]=     @DateLastModified,
       [Attributes]=   @Attributes,
       [size]= @size
RETURN @hr