【问题标题】:how to import 2 row from txt or EXCEL into the same ROW in SQL server如何将 2 行从 txt 或 EXCEL 导入 SQL Server 中的同一行
【发布时间】:2012-08-20 12:14:32
【问题描述】:

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

就在这个模块,我没有办法导出,只能复制粘贴信息

在同一个l中看起来像这样

1。 MANUF MODEL YEAR MFG SERIAL REG OS DATE DESC LISTED 1。年 DLV 2。怪物 4r25 1988 23547248 Waka001 7/23/2012 出售 7/22/2009 2。 1989 3。福特 12SE 1994 6262552 DBZRLZ0 7/26/2012 出售 7/9/2009 3。 1994

我按行获取数据,但年份 mfg 和年份 dlv 位于一行中的 2 行中(或同一字段中的 2 行)。当粘贴到 excel 上时,它首先生成 2 行,其中包含行中的所有数据,包括年份 mng,第二行仅用于年份 dlv(在同一列中)。

我可以通过添加额外的列并处理该额外的字段并删除空白等来在 excel 中解析此信息。但我想省略 excel 部分并从 TXT 文件中导入它,该文件在粘贴时也会每行创建 2 行,并使用制表符作为分隔符(如 txt 文本制表符分隔)。

当我使用批量插入导入时,它会导入两倍的行,但我无法想象将第二行解析为新列的方法。

有人可以帮忙吗?在 t-sql 中(每一行只有一行信息,但在 year mfg /year dlv 列中,有两行)。 或者指出我要阅读什么或哪种方法更好?也许一次导入 2 行 ETC。

【问题讨论】:

    标签: sql-server-2008 excel tsql text import


    【解决方案1】:

    你知道如何使用 VBA 吗?您可以在 Excel 中运行此代码 (FixData()),然后在 TSQL 中使用它,这样它就可以解决额外的行问题。希望这会有所帮助

    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
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        您可以将文本文件中的数据集导入到包含空白行的临时表中。这将为您提供具有 2 种记录类型的 SQL 数据集。 1. 包含交货日期以外的所有数据的记录。 2. 只有交货日期而没有其他字段的记录。 (添加唯一的自增键)

        因为相关记录会分开一条记录,所以Record N和Record N+1实际上是同一条记录。

        然后选择查询通过 RecID = RecId+1 将临时表连接到其自身将给出包含所有字段的完整记录

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

        您可以从此数据集中插入主数据。

        【讨论】:

        • 嘿,谢谢!!这就像一个魅力,我刚刚做到了
          在 Mainrecord.P_Id = MissingField.P_Id -1 和 (MissingField.P_Id -1)%2=1 然后选择主记录* 但缺少记录 dlv ...
        猜你喜欢
        • 1970-01-01
        • 2016-03-16
        • 1970-01-01
        • 1970-01-01
        • 2014-08-19
        • 2016-03-01
        • 2011-05-01
        • 2021-09-05
        • 2014-10-26
        相关资源
        最近更新 更多