我有一个.txt
文件,其中包含必须存储在表格中的数据,我已经为此编写了一个查询。
问题是有很多.txt
个文件,可以添加更多文件,我认为可以创建一个包含.txt
个文件名的表。我需要为每个文件运行查询,使用这些名称作为变量。
这是我的代码:
CREATE TABLE #textfile (line varchar(8000))
BULK INSERT #textfile
FROM 'c:\MXAPT1_SNDA01_1_SSD.TXT'
GO
-- Now read it and insert
INSERT INTO san3parvols(disco, tipo, fecha, totcap, alloc, disp)
(SELECT
disco, tipo, fecha,
SUM(totcap) AS totcap, SUM(alloc) AS alloc,
SUM(totcap) - SUM(alloc) AS disp
FROM
(SELECT
'MXAPT1_SNDA01_TRIARA_1' as disco, 'SSD' AS tipo,
CASE
WHEN substring(line,3,4) = 'tal'
THEN 'Total Capacity'
EKSE 'Allocated'
END AS valor,
CAST(substring(line, PatIndex('%[0-9]%', line), LEN(line)) AS numeric(18,2)) AS cap,
CONVERT(DATE, getdate()) AS fecha,
CAST(CASE
WHEN substring(line, 3, 4) = 'tal'
THEN substring(line, PatIndex('%[0-9]%', line), LEN(line))
ELSE 0
END AS numeric(18,2)) AS totcap,
CAST(CASE
WHEN substring(line, 3, 4) = 'Allo'
THEN substring(line, PatIndex('%[0-9]%', line), len(line))
ELSE 0
END AS numeric(18,2)) AS alloc
FROM
#textfile
WHERE
substring(line, 3, 4) IN ('tal', 'Allo')) AS txt1
GROUP BY
disco, tipo, fecha)
-- And then clean up
DROP TABLE #textfile
GO
我知道你可以提供帮助,我提前谢谢你。
修改
以防有人需要我对M.Ali的最后一个问题的答案,我会分享有关如何添加多个变量的代码。
Declare @File Nvarchar(max), @sql Nvarchar(max), @name Nvarchar(max), @tipo Nvarchar(max);
Declare Cur CURSOR LOCAL FAST_FORWARD FOR
SELECT trespar, name, tipo
FROM sanparlist
OPEN Cur
FETCH NEXT FROM Cur INTO @File, @name, @tipo
WHILE (@@FETCH_STATUS = 0)
BEGIN
TRUNCATE TABLE Textfile_Stagging_table
SET @Sql = N' BULK INSERT Textfile_Stagging_table
FROM ''' + @File + ''''
Execute sp_executesql @Sql
INSERT INTO san3parvols(disco, tipo, fecha,totcap, alloc, disp)
(SELECT disco, tipo, fecha, sum(totcap) AS totcap, sum(alloc) AS alloc,sum(totcap)-sum(alloc) AS disp FROM
(SELECT @name AS disco, @tipo AS tipo,
CASE WHEN substring(line,3,4) = 'tal'THEN 'Total Capacity' ELSE 'Allocated' END AS valor,
cast(substring(line, PatIndex('%[0-9]%', line), len(line)) AS numeric(18,2) )AS cap,
convert(DATE,getdate()) AS fecha,
cast(CASE WHEN substring(line,3,4) = 'tal'THEN substring(line, PatIndex('%[0-9]%', line), len(line)) ELSE 0 END AS numeric(18,2)) AS totcap,
cast(CASE WHEN substring(line,3,4) = 'Allo'THEN substring(line, PatIndex('%[0-9]%', line), len(line)) ELSE 0 END AS numeric(18,2)) AS alloc
FROM Textfile_Stagging_table
WHERE substring(line,3,4) in('tal','Allo')) AS txt1
GROUP BY disco, tipo, fecha)
FETCH NEXT FROM Cur INTO @File, @name, @tipo
END
CLOSE Cur
DEALLOCATE Cur;
答案 0 :(得分:0)
是的,可以在表中包含文件名,并编写查询以从表中获取文本文件名并将其插入数据库。
-- Create a Table in SQL Server
CREATE TABLE Textfile_Stagging_table (line varchar(8000))
GO
Declare @File Nvarchar(max), @sql Nvarchar(max);
Declare Cur CURSOR LOCAL FAST_FORWARD FOR
SELECT [FileName]
FROM TxtFiles
OPEN Cur
FETCH NEXT FROM Cur INTO @File
WHILE (@@FETCH_STATUS = 0)
BEGIN
TRUNCATE TABLE Textfile_Stagging_table
SET @Sql = N' BULK INSERT Textfile_Stagging_table
FROM ''' + @File + ''''
Execute sp_executesql @Sql
-- Your original query to get data into final destination table
-- from the Stagging table can go here
-- insert into san3parvols(disco, tipo, fecha,totcap, alloc, disp)
-- Select bla bla bla
-- FROM Textfile_Stagging_table
FETCH NEXT FROM Cur INTO @File
END
CLOSE Cur
DEALLOCATE Cur;