我试图将多个csv文件加载到mysql中以创建这些文件的单个表。我在mysql中加载了一个名为symbol的表,该表的一列名为ticker,所有行值都表示诸如A,B,C等的符号。我想遍历这些行以创建路径为/path/A.csv
这样我就可以从所有这些文件中读取数据以创建新表all_tickers。
之前,我在SQL Server中使用以下代码进行了此操作:
CREATE TABLE dbo.all_tickers
(
ticker_symbol varchar(32) NOT NULL,
[Date] datetime2(7) NOT NULL,
[Open] float NULL,
High float NULL,
Low float NULL,
[close] float NULL,
Volume float NULL,
PRIMARY KEY (ticker_symbol, [Date])
);
GO
DECLARE @Path nvarchar(255) = 'D:\ASX';
DECLARE @SQL nvarchar(MAX);
CREATE TABLE #ticker_staging
(
[Date] datetime2(7) NOT NULL,
[Open] float NULL,
High float NULL,
Low float NULL,
[close] float NULL,
Volume float NULL
);
SET @SQL = (SELECT N'
TRUNCATE TABLE #ticker_staging;
BULK INSERT #ticker_staging
FROM ''' + @Path + N'\' + ticker + N'.csv''
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = '','', --CSV field delimiter
ROWTERMINATOR = ''\n'', --Use to shift the control to next row
TABLOCK
);
INSERT INTO dbo.all_tickers WITH (TABLOCKX)(
ticker_symbol,
[Date],
[Open],
High,
Low,
[close],
Volume
)
SELECT
''' + ticker + N''',
[Date],
[Open],
High,
Low,
[close],
Volume
FROM #ticker_staging;'
FROM dbo.symbols
FOR XML PATH(''), TYPE).value('.',N'nvarchar(MAX)');
EXECUTE sp_executesql @SQL;
GO
要在MySQL中复制结果,我将上面的代码更改为如下:
CREATE TABLE dbo.all_tickers
(
ticker_symbol varchar(32) NOT NULL,
Date datetime NOT NULL,
Open float NULL,
High float NULL,
Low float NULL,
close float NULL,
Volume float NULL,
PRIMARY KEY (ticker_symbol, Date)
);
set @Path = 'D:\ASX';
set @SQL = nvarchar(MAX);
CREATE TABLE #ticker_staging
(
Date1 datetime NOT NULL,
Open float NULL,
High float NULL,
Low float NULL,
close float NULL,
Volume float NULL
);
SET @SQL = (SELECT N'
TRUNCATE TABLE #ticker_staging;
LOAD DATA INFILE #ticker_staging
FROM ''' + @Path + N'\' + ticker + N'.csv''
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = '','', #CSV field delimiter
ROWTERMINATOR = ''\n'', #Use to shift the control to next row
TABLOCK
);
INSERT INTO dbo.all_tickers WITH (TABLOCKX)(
ticker_symbol,
[Date],
[Open],
High,
Low,
[close],
Volume
)
SELECT
''' + ticker + N''',
[Date],
[Open],
High,
Low,
[close],
Volume
FROM #ticker_staging;'
FROM dbo.SCRIP
FOR XML PATH(''), TYPE).value('.',N'nvarchar(MAX)');
EXECUTE sp_executesql @SQL;
我在处理SQL Server中用于将其转换成MySQL的WITH
子句时遇到问题。
由于我是MySQL的新手,所以我认为这是主要问题。如果代码中还有其他问题,请提出建议。