如何从txt或EXCEL导入2行到SQL服务器中的同一行

我需要从我有权访问的页面中提取信息。

就在这个模块中,我没有办法导出,只是为了复制和粘贴信息

看起来像在同一个l

  1. MANUF模型年份MFG序列寄存器OSdateDESC列出 
  1.年份DLV 
  2. monster 4r25 1988 23547248 Waka001 7/23/2012出售7/22/2009 
  2. 1989年 
  3. FORD 12SE 1994 6262552 DBZRLZ0 7/26/2012出售7/9/2009 
  3. 1994年 

我得到我的数据在行中,但年份制造和年份dlv是在一行内的2行(或在同一领域2行)。 在excel上粘贴时,首先使用2行(包括年份mng)和第二行(仅限于同一列)中的所有数据。

我可以通过添加额外的列和应付额外的领域和删除空白等parsing这些信息在Excel中。 但是我想省略excel部分,并从一个TXT文件中导入,在粘贴时每行创build2行,并使用制表符作为分隔符(如txt文本制表符分隔)。

当我用大容量插入导入时,它导入了两倍的行数,但我无法想象一种将第二行parsing成新列的方法。

有人可以帮忙吗? 在t-sql中(每一行只有一行信息,但是在列年制作/年份dlv中,有两行)。 或者指出我要读什么或者哪一个更好? 也许一次导入2行等。

您可以将文本文件中的数据集导入包含空白行的临时表中。 这会给你一个SQL数据集,有两种types的logging。 1.除交货date之外的所有数据的logging。 2.logging只有交货date,没有其他字段。 (添加一个唯一的自动递增键)

因为相关的logging将是一个logging,loggingN和loggingN + 1实际上是相同的logging。

然后,select查询通过RecID = RecId + 1将临时表join其自己将为所有字段提供完整的logging

SELECT * FROM tmpTable AS MainRecord
INNER JOIN tmpTable AS MissingField
ON MainRecord.RecId = MissingField.RecId +1

从这个数据集,你可以embedded到你的主要数据。

你知道如何使用VBA吗? 在使用TSQL之前,您可以在Excel中运行此代码(FixData()),以便修复额外的行问题。 希望这可以帮助

Option Explicit Public Sub FixData() Dim ws As Excel.Worksheet Dim iCurRow As Long Dim iLastRow As Long Set ws = ActiveSheet iLastRow = ws.Cells.SpecialCells(xlCellTypeLastCell).Row ' move through the spreadsheet from bottom to top For iCurRow = iLastRow To 1 Step -1 If (isCurrentRowMessedUp(ws, iCurRow) = True) Then Call AppendDataToPreviousRow(ws, iCurRow) ' delete the row since we moved the data out of there ws.Rows(iCurRow).EntireRow.Delete End If Next End Sub Private Sub AppendDataToPreviousRow(ByRef ws As Excel.Worksheet, ByVal currentRow As Long) Dim firstCellInRow As Excel.Range Dim lastCellInRow As Excel.Range Dim previousRowRangeToPaste As Excel.Range ' check if first column has data in it, otherwise find the first column that has data If (ws.Cells(currentRow, 1).Value = vbNullString) Then Set firstCellInRow = ws.Cells(currentRow, 1).End(xlToRight) Else Set firstCellInRow = ws.Cells(currentRow, 1) End If Set lastCellInRow = ws.Cells(currentRow, ws.Columns.Count).End(xlToLeft) Set previousRowRangeToPaste = ws.Cells(currentRow - 1, getNextColumnAvailableInPreviousRow(ws, currentRow)) ws.Range(firstCellInRow, lastCellInRow).Cut previousRowRangeToPaste End Sub Private Function isCurrentRowMessedUp(ByRef ws As Excel.Worksheet, ByVal currentRow As Long) As Boolean Dim cellCountInRow As Long Dim firstCellInRow As Excel.Range Dim lastCellInRow As Excel.Range Set firstCellInRow = ws.Cells(currentRow, 1) Set lastCellInRow = ws.Cells(currentRow, ws.Columns.Count).End(xlToLeft) cellCountInRow = Application.WorksheetFunction.CountA(ws.Range(firstCellInRow, lastCellInRow)) If (cellCountInRow <= 1) Then isCurrentRowMessedUp = True Else isCurrentRowMessedUp = False End If End Function Private Function getLastColumnInPreviousRow(ByRef ws As Excel.Worksheet, ByVal currentRow As Long) As Long Dim rng As Excel.Range Set rng = ws.Cells(currentRow - 1, 1).End(xlToRight) getLastColumnInPreviousRow = rng.Column End Function Private Function getNextColumnAvailableInPreviousRow(ByRef ws As Excel.Worksheet, ByVal currentRow As Long) As Long getNextColumnAvailableInPreviousRow = getLastColumnInPreviousRow(ws, currentRow) + 1 End Function 

您可以使用SQL Server Integration Service (SSIS)将任何源数据(如excel SQL Server Integration Service (SSIS)中的数据转换为任何目标数据,如SQL Server

在这里输入图像说明