我正在使用以下存储过程来获取位于同一目录中的几个excel文件,并通过列映射上载(追加/插入)到TestTable中。导入工作正常,但是我现在想输出到一个日志文件,其中将从每个上传文件中导入的记录数进行计数并反映在文件中。如果从文件之一上传失败(假设文件和文件名的路径仍然正确),则日志文件中的输出应为:
Import C NOT OK: [fail reason]; check C:\Directory\Quantities\ERROR\TestFileC.xlsx
未处理的文件应移动到如上所述的其他目录...
DECLARE @Path VARCHAR(2000)
DECLARE @Txt VARCHAR(2000)
DECLARE @YYYY INT
DECLARE @MM INT
DECLARE @DD INT
DECLARE @YYYY_Txt VARCHAR(2000)
DECLARE @MM_Txt VARCHAR(2000)
DECLARE @DD_Txt VARCHAR(2000)
DECLARE @DateStamp VARCHAR(2000)
SET @YYYY = DATEPART(yy,GETDATE())
SET @YYYY_Txt = CAST(@YYYY AS VARCHAR(2000))
SET @MM = DATEPART(mm,GETDATE())
SET @MM_Txt = CAST(@MM AS VARCHAR(2000))
SET @DD = DATEPART(dd,GETDATE())
SET @DD_Txt = CAST(@DD AS VARCHAR(2000))
SET @DateStamp = @YYYY_Txt + '_' + @MM_Txt + '_' + @DD_Txt
SET @Path = 'C:\Directory\Quantities\Logs\importLog_' + @DateStamp + '.txt'
--------------------------------------------------------------------------------------
USE TEST_DB;
--TEST A--
GO
INSERT INTO [dbo].[TestTable] ([Column_A], [Column_B], [Column_C], [Column_D])
SELECT A.[Int# Q1 (A)], A.[Int# Q2 (B)], A.[Int# Q3 (C)], A.[Int# Q4 (D)] FROM OPENROWSET
('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=C:\Directory\Quantities\TestFileA.xlsx;HDR=YES', 'select * from [Sheet$]') AS A;
SET @Txt = 'Import A OK'
EXEC WriteToFile @Path, @Txt
GO
--TEST B--
INSERT INTO [dbo].[TestTable] ([Column_A], [Column_B], [Column_C], [Column_D])
SELECT B.[Int# Q1 (A)], B.[Int# Q2 (B)], B.[Int# Q3 (C)], B.[Int# Q4 (D)] FROM OPENROWSET
('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=C:\Directory\Quantities\TestFileB.xlsx;HDR=YES', 'select * from [Sheet$]') AS B;
SET @Txt = 'Import B OK'
EXEC WriteToFile @Path, @Txt
GO
--TEST C--
INSERT INTO [dbo].[TestTable] ([Column_A], [Column_B], [Column_C], [Column_D])
SELECT C.[Int# Q1 (A)], C.[Int# Q2 (B)], C.[Int# Q3 (C)], C.[Int# Q4 (D)] FROM OPENROWSET
('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=C:\Directory\Quantities\TestFileC.xlsx;HDR=YES', 'select * from [Sheet$]') AS C;
SET @Txt = 'Import C NOT OK'
EXEC WriteToFile @Path, @Txt
GO
日志文件的最终输出如下所示:
Import A OK: 100 records
Import B OK: 200 records
Import C NOT OK: [fail reason]; check C:\Directory\Quantities\ERROR\TestFileC.xlsx
您能帮我修改我的程序,并告诉我如何解决此问题吗?目前,此时甚至还没有生成带有时间戳的importLog文件...我正在使用MS SQL Server-非常感谢!