可以使用本地文件,文件路径需要是相对于服务器的,所以另一台计算机上的文件需要是UNC路径,服务器本地路径不一定是共享(安全问题以及您需要配置和监控的另一件事)。
有时我必须像所有其他 DBA 一样遍历导入文件。我这样解决。
您可以将其转储到 SQL 作业中并安排它。可以每秒或每天运行,这取决于您的业务需求。这是我的一个脚本的示例:
SET NOCOUNT ON
DECLARE @BackupDirectory SYSNAME = 'G:\'
IF OBJECT_ID('tempdb..#DirTree') IS NOT NULL
DROP TABLE #DirTree
CREATE TABLE #DirTree (
Id int identity(1,1),
SubDirectory nvarchar(255),
Depth smallint,
FileFlag bit,
ParentDirectoryID int
)
INSERT INTO #DirTree (SubDirectory, Depth, FileFlag)
EXEC master..xp_dirtree @BackupDirectory, 10, 1
UPDATE #DirTree
SET ParentDirectoryID = (
SELECT MAX(Id) FROM #DirTree d2
WHERE Depth = d.Depth - 1 AND d2.Id < d.Id
)
FROM #DirTree d
DECLARE
@ID INT,
@BackupFile VARCHAR(MAX),
@Depth TINYINT,
@FileFlag BIT,
@ParentDirectoryID INT,
@wkSubParentDirectoryID INT,
@wkSubDirectory VARCHAR(MAX)
if OBJECT_ID('dbo.ImportFiles')=0
create table dbo.ImportFiles
(
FileNamePath VARCHAR(MAX)
)
else
truncate table dbo.ImportFiles
DECLARE FileCursor CURSOR LOCAL FORWARD_ONLY FOR
SELECT * FROM #DirTree WHERE FileFlag = 1
OPEN FileCursor
FETCH NEXT FROM FileCursor INTO
@ID,
@BackupFile,
@Depth,
@FileFlag,
@ParentDirectoryID
SET @wkSubParentDirectoryID = @ParentDirectoryID
WHILE @@FETCH_STATUS = 0
BEGIN
--loop to generate path in reverse, starting with backup file then prefixing subfolders in a loop
WHILE @wkSubParentDirectoryID IS NOT NULL
BEGIN
SELECT @wkSubDirectory = SubDirectory, @wkSubParentDirectoryID = ParentDirectoryID
FROM #DirTree
WHERE ID = @wkSubParentDirectoryID
SELECT @BackupFile = @wkSubDirectory + '\' + @BackupFile
END
--no more subfolders in loop so now prefix the root backup folder
SELECT @BackupFile = @BackupDirectory + @BackupFile
INSERT INTO ImportFiles (FileNamePath) VALUES(@BackupFile)
FETCH NEXT FROM FileCursor INTO
@ID,
@BackupFile,
@Depth,
@FileFlag,
@ParentDirectoryID
SET @wkSubParentDirectoryID = @ParentDirectoryID
END
CLOSE FileCursor
DEALLOCATE FileCursor
SET NOCOUNT ON
print 'loading files from file table'
-- =============================================
-- Declare and using a READ_ONLY cursor
-- =============================================
DECLARE eoddata_cursor CURSOR
READ_ONLY
FOR SELECT FileNamePath FROM ImportFiles
where FileNamePath like '%.csv'
DECLARE @name nvarchar(4000), @sql nvarchar(2000), @rows int
OPEN eoddata_cursor
FETCH NEXT FROM eoddata_cursor INTO @name
WHILE (@@fetch_status <> -1)
BEGIN
IF (@@fetch_status <> -2)
BEGIN
print 'file '+@name
begin try
truncate table [Tickers].[DataClient]
set @sql = '
bulk insert [Tickers].[DataClient]
from N'''+@name+'''
with (
FIELDTERMINATOR ='','',
ROWTERMINATOR =''\n'' ,
FIRSTROW = 2,
CODEPAGE = ''RAW'',
MAXERRORS = 2000000,
ERRORFILE = '''+@name+'.err''
)'
insert into [Tickers].[DataClient]
exec(@sql)
exec [Tickers].[MoveDataClientData] @filename = @name, @rows = @rows output
delete from ImportFiles where FileNamePath = @name
print 'loaded :'+str(@rows)
end try
begin catch
print 'error:'+ error_message()
end catch
END
FETCH NEXT FROM eoddata_cursor INTO @name
END
CLOSE eoddata_cursor
DEALLOCATE eoddata_cursor
GO