.dat
文件中的输入数据如下所示:
1| 2| 3| 4| 5| 6
11|22|33|44|55|66
最多300 000行
Dat文件包含6列我需要添加一个默认列并执行批量插入SQL Server
我该怎么做?
答案 0 :(得分:1)
为您的问题添加示例执行代码
CREATE TABLE stackoverflow_data (
no VARCHAR(50)
,Name VARCHAR(50)
,id VARCHAR(50)
,state VARCHAR(50)
,address VARCHAR(50)
,testdate DATETIME2(3)
);
SELECT sd.no
,sd.Name
,sd.id
,sd.state
,sd.address
INTO #stack_data
from stackoverflow_data sd
WHERE sd.no = 0
BULK INSERT #stack_data FROM '\\test\others\input.dat' WITH (FIRSTROW = 1, ROWTERMINATOR = '\n', FIELDTERMINATOR = '|', ROWS_PER_BATCH = 10000)
insert INTO stackoverflow_data (no, Name, id, state, address, testdate)
SELECT sd.no
,sd.Name
,sd.id
,sd.state
,sd.address
,'10-Dec-2014'
FROM #stack_data sd
答案 1 :(得分:0)
处理文件并将数据添加到临时表(#temp
)中,其日期与文件中的日期相同。然后从#temp
表插入原始表,默认值为默认列。
DECLARE @BulkInsertSql AS VARCHAR(MAX);
SELECT
column1
,column2 INTO #BulkInsertTempTable
FROM OriginalTable
WHERE OriginalTable.id = 0; -- this condition is required to carry forward any identity columns
然后批量插入
EXEC ('BULK INSERT #BulkInsertTempTable FROM ''' + @FileToProcess +
''' WITH (MAXERRORS = 0,FIELDTERMINATOR = ''|'',ROWTERMINATOR = ''\n'',FIRSTROW=2);')
然后从#temp table
插入到原始表中 SET @BulkInsertSql =
'INSERT INTO OriginalTable
(column1,
column2,
defaultcolumn3
)
SELECT
#BulkInsertTempTable.column1,
#BulkInsertTempTable.column2,
'defaultvalue' FROM #BulkInsertTempTable'
EXEC (@BulkInsertSql);