我正在使用远程Microsoft SQL Server。
在Ubuntu上使用DataGrip执行以下代码后,我收到错误:
CREATE TABLE #TempNullClass (
id nvarchar(10),
classCode nvarchar(10)
);
BULK INSERT #TempNullClass
FROM '/home/user/Downloads/data1.csv'
WITH (
FIELDTERMINATOR = ',',
ROWTERMINATOR = '0x0a'
);
[S0001] [4861]无法批量加载,因为无法打开文件“/home/user/Downloads/data1.csv”。操作系统错误代码3(系统找不到指定的路径。)。
文件具有以下所有权限:
-rwxrwxrwx 1 user user 73399 may 15 15:11 data1.csv
我无法将文件放在远程服务器上。
答案 0 :(得分:1)
您可以使用本地文件,文件路径需要相对于服务器,因此另一台计算机上的文件需要是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
答案 1 :(得分:0)
该文件应放在远程服务器上或共享到远程服务器的文件夹中。