【问题标题】:Paste MS Excel data to SQL Server将 MS Excel 数据粘贴到 SQL Server
【发布时间】:2008-11-25 10:51:08
【问题描述】:

我在 Excel 中有一堆行,我想将它们粘贴到 MS SQL 中的新表中。有没有简单的方法?

【问题讨论】:

  • 这似乎与编程无关——你是想用 VBA 或其他东西来做这件事吗?
  • 不,只是大量数据以 excel 或轻松的方式即席到达,“优秀”

标签: sql-server excel copy-paste


【解决方案1】:

如果您有 SQL Server Management Studio,您只需使用鼠标从 Excel 复制并粘贴到 Management Studio 的表格中即可。只是

  1. 转到要粘贴到的表格。
  2. 选择“编辑前 200 行”。
  3. 右键单击任意位置并选择粘贴。

在执行此操作之前,您必须匹配 Excel 和 Management Studio 之间的列。此外,您必须使用 Management Studio 中的表设计器放置所有不可编辑的列last(最右侧)。

整个过程只需几秒钟(设置和启动 - 不一定要执行)并且不需要任何 SQL 语句。

关于empty database tables and SSMS v18.1+

【讨论】:

  • +1,但您不必选择“编辑前 1000 行”(SSMS 2005 中该选项在哪里?)。您只需要打开一个表格并将其粘贴到一个空行中。顺便说一句,如果列名相互匹配,它很简单并且可以工作,但是在我的服务器上使用此方法插入 40.000 行需要 45 分钟(而不是几秒)(插入触发器处于活动状态)。因此,它简单但不灵活和快速。
  • 只是对在 SSMS 2005 中执行此操作的任何人的说明,您需要右键单击左侧的空白行标记,而不是单元格本身,然后选择粘贴,否则它会粘贴最后一个值进入当前行。
  • 至少在 2012 年,您需要专门右键单击空白行第一列左侧的空白灰色“标题”区域。如果您右键单击一个单元格,它会尝试复制到该单元格。如果您右键单击左上角的空白、灰色“标题”块,就在任何行或列之前,它只是不会为您提供粘贴选项。虽然从技术上讲我不确定如果单击与列相关联的标题区域会发生什么,但不是与行相关联。
  • 如果第一列是标识列,则确保 Excel 中的第一列是空列,并在从 Excel 复制时也包括在内。 (您不必将其移动到末尾左右)正如其他人所指出的那样,在 Management Studio 中,请确保通过单击其选择器来选择“新行”。
【解决方案2】:

我过去曾成功使用过这种技术:

Using Excel to generate Inserts for SQL Server

(...) 跳过一列(或将其用作注释),然后键入类似 公式如下:

="insert into tblyourtablename (yourkeyID_pk, intmine, strval) values ("&A4&", "&B4&", N'"&C4&"')"

现在你已经有了插入语句 包含主键 (PK)、整数和 Unicode 字符串的表。 (...)

【讨论】:

  • bzlm 的回答更适合这个问题。这是一个巧妙的小发现,但大多数情况下,bzlm 的右键单击并复制过去的作品!
  • 这样更快更简单:mssqltips.com/sqlservertip/1430/…
  • 复制和粘贴对我不起作用,OPENROWSET 命令也不起作用。唯一的办法就是 Galwegian 的回答!
  • 要进行复制和粘贴,您需要确保您没有单击 SQL Server Management Studio 中的任一字段单元格 - 请改为单击灰色边框之一。
【解决方案3】:

Excel

  1. 在 Excel 中,突出显示并复制要粘贴到 SQL 中的数据。

SQL

  1. 使用所需的列名创建表并为表命名。 *确保Identity Specification 是Yes,所以它会自动增加你的 身份列。
  2. 找到您的表,右键单击它并从对话框中选择Edit Top 200 Rows
  3. 右键单击带有 * 号的空行并从对话框中选择粘贴

【讨论】:

    【解决方案4】:

    供日后参考:

    您可以通过这样做将数据从 excel-sheet 复制粘贴到 SQL 表中:

    1. 在 Excel 中选择数据并按 Ctrl + C
    2. 在 SQL Server Management Studio 中右键单击表并选择编辑前 200 行
    3. 滚动到底部并通过单击行标题选择整个空行
    4. Ctrl + V 粘贴数据

    注意:通常表的第一列是 ID 列,具有自动生成/递增的 ID。当您粘贴数据时,它将开始将 Excel 中最左侧的选定列插入 SSMS 中的最左侧列,从而将数据插入 ID 列。为了避免这种情况,请在选择的最左侧保留一个空列,以便在 SSMS 中跳过该列。这将导致 SSMS 插入默认数据,即自动生成的 ID。 此外,您可以通过在 Excel 工作表选择中与要跳过的列相同的序号位置设置空列来跳过其他列。这将使 SSMS 插入默认值(或 NULL,其中未指定默认值)。

    【讨论】:

    • 只能让我一次做一排。我在 Excel 中选择多行的那一刻,在 SSMS 中进行粘贴确实......什么都没有。我试过Ctrl+V,我试过右键+粘贴,我试过先把它放在记事本里,然后从最后删除多余的空白行,我试过在开头放一个空白列(就像您过去在企业管理器中必须做的那样)......没有任何作用。
    • 如果您要向已有数据的表中添加行,许多答案会忘记提及第 3 步。
    【解决方案5】:

    我开发了一个 Excel VBA 宏,用于将 Excel 中的任何选择剪切并粘贴到 SQL Server 中,从而创建一个新表。该宏非常适合快速创建多达几千行和多列的脏表(理论上它可以管理多达 200 列)。该宏尝试自动检测标题名称并为每一列分配最合适的数据类型(它处理最多 1000 个字符的 varchar 列)。

    推荐的安装程序:

    1. 确保启用 Excel 以运行宏。 (文件->选项->信任中心->信任中心设置->宏设置->启用所有宏..)
    2. 将下面的 VBA 代码复制到与您的个人工作簿关联的模块中(以便宏可用于所有工作表)
    3. 为宏分配适当的击键(我已分配 Ctrl Shift X)
    4. 保存您的个人工作簿

    宏的使用

    1. 在 Excel 中选择要传输到 SQL 的单元格(包括列标题,如果它们存在)
    2. 按已分配的关键字组合来运行宏
    3. 按照提示操作。 (默认表名是##Table)
    4. 将剪贴板内容粘贴到 SSMS 窗口中并运行生成的 SQL 代码。 BriFri 238

    VBA 代码:

    Sub TransferToSQL()
    '
    ' TransferToSQL Macro
    ' This macro prepares data for pasting into SQL Server and posts it to the clipboard for inserting into SSMS
    ' It attempts to automatically detect header rows and does a basic analysis of the first 15 rows to determine the most appropriate datatype to use handling text entries upto 1000 chars.
    '
    ' Max Number of Columns: 200
    '
    ' Keyboard Shortcut: Ctrl+Shift+X
    '
    ' ver   Date    Reason
    ' ===   ====    ======
    
    ' 1.6   06/2012 Fixed bug that prevented auto exit if no selection made / auto exit if blank Tablename entered or 'cancel' button pressed
    ' 1.5   02/2012 made use of function fn_ColLetter to retrieve the Column Letter for a specified column
    ' 1.4   02/2012 Replaces any Tabs in text data to spaces to prevent Double quotes being output in final results
    ' 1.3   02/2012 Place the 'drop table if already exists' code into a separate batch to prevent errors when inserting new table with same name but different shape and > 100 rows
    ' 1.2   01/2012 If null dates encountered code to cast it as Null rather than '00-Jan-1900'
    ' 1.1   10/2011 Code to drop the table if already exists
    ' 1.0   03/2011 Created
    
    Dim intLastRow As Long
    Dim intlastColumn As Integer
    Dim intRow As Long
    Dim intDataStartRow As Long
    Dim intColumn As Integer
    Dim strKeyWord As String
    Dim intPos As Integer
    Dim strDataTypeLevel(4) As String
    Dim strColumnHeader(200) As String
    Dim strDataType(200) As String
    Dim intRowCheck As Integer
    Dim strFormula(20) As String
    Dim intHasHeaderRow As Integer
    Dim strCellRef As String
    Dim intFormulaCount As Integer
    Dim strSQLTableName As String
    Dim strSQLTableName_Encap As String
    Dim intdataTypelevel As Integer
    Const strConstHeaderKeyword As String = "ID,URN,name,Title,Job,Company,Contact,Address,Post,Town,Email,Tele,phone,Area,Region,Business,Total,Month,Week,Year,"
    Const intConstMaxBatchSize As Integer = 100
        Const intConstNumberRowsToAnalyse As Integer = 100
    intHasHeaderRow = 0
    
    strDataTypeLevel(1) = "VARCHAR(1000)"
    strDataTypeLevel(2) = "FLOAT"
    strDataTypeLevel(3) = "INTEGER"
    strDataTypeLevel(4) = "DATETIME"
    
    
    
    ' Use current selection and paste to new temp worksheet
    
        Selection.Copy
        Workbooks.Add       ' add temp 'Working' Workbook
        ' Paste "Values Only" back into new temp workbook
        Range("A3").Select  ' Goto 3rd Row
        Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False ' Copy Format of Selection
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False  ' Copy Values of Selection
        ActiveCell.SpecialCells(xlLastCell).Select  ' Goto last cell
        intLastRow = ActiveCell.Row
        intlastColumn = ActiveCell.Column
    
    
    ' Check to make sure that there are cells which are selected
    If intLastRow = 3 And intlastColumn = 1 Then
        Application.DisplayAlerts = False       ' Temporarily switch off Display Alerts
        ActiveWindow.Close                      ' Delete newly created worksheet
        Application.DisplayAlerts = True        ' Switch display alerts back on
        MsgBox "*** Please Make selection before running macro - Terminating ***", vbOKOnly, "Transfer Data to SQL Server"
        Exit Sub
    End If
    
    ' Prompt user for Name of SQL Server table
    strSQLTableName = InputBox("SQL Server Table Name?", "Transfer Excel Data To SQL", "##Table")
    
    ' if blank table name entered or 'Cancel' selected then exit
    If strSQLTableName = "" Then
        Application.DisplayAlerts = False       ' Temporarily switch off Display Alerts
        ActiveWindow.Close                      ' Delete newly created worksheet
        Application.DisplayAlerts = True        ' Switch display alerts back on
        Exit Sub
    End If
    
    
    
    ' encapsulate tablename with square brackets if user has not already done so
    strSQLTableName_Encap = Replace(Replace(Replace("[" & Replace(strSQLTableName, ".", "].[") & "]", "[]", ""), "[[", "["), "]]", "]")
    
    ' Try to determine if the First Row is a header row or contains data and if a header load names of Columns
    Range("A3").Select
    For intColumn = 1 To intlastColumn
        ' first check to see if the first row contains any pure numbers or pure dates
        If IsNumeric(ActiveCell.Value) Or IsDate(ActiveCell.Value) Then
        intHasHeaderRow = vbNo
        intDataStartRow = 3
        Exit For
        Else
        strColumnHeader(intColumn) = ActiveCell.Value
        ActiveCell.Offset(1, 0).Range("A1").Select  ' go to the row below
        If IsNumeric(ActiveCell.Value) Or IsDate(ActiveCell.Value) Then
            intHasHeaderRow = vbYes
            intDataStartRow = 4
        End If
        ActiveCell.Offset(-1, 0).Range("A1").Select  ' go back up to the first row
        If intHasHeaderRow = 0 Then     ' if still not determined if header exists: Look for header using keywords
            intPos = 1
            While intPos < Len(strConstHeaderKeyword) And intHasHeaderRow = 0
            strKeyWord = Mid$(strConstHeaderKeyword, intPos, InStr(intPos, strConstHeaderKeyword, ",") - intPos)
            If InStr(1, ActiveCell.Value, strKeyWord) > 0 Then
                intHasHeaderRow = vbYes
                intDataStartRow = 4
            End If
            intPos = InStr(intPos, strConstHeaderKeyword, ",") + 1
            Wend
        End If
        End If
        ActiveCell.Offset(0, 1).Range("A1").Select  ' Goto next column
    Next intColumn
    
    ' If auto header row detection has failed ask the user to manually select
    If intHasHeaderRow = 0 Then
        intHasHeaderRow = MsgBox("Does current selection have a header row?", vbYesNo, "Auto header row detection failure")
        If intHasHeaderRow = vbYes Then
        intDataStartRow = 4
        Else
        intDataStartRow = 3
        End If
    
    End If
    
    
    
    
    ' *** Determine the Data Type of each Column ***
    
    ' Go thru each Column to find Data types
    If intLastRow < intConstNumberRowsToAnalyse  Then             ' Check the first intConstNumberRowsToAnalyse  rows or to end of selection whichever is less
        intRowCheck = intLastRow
    Else
        intRowCheck = intConstNumberRowsToAnalyse 
    End If
    
    For intColumn = 1 To intlastColumn
        intdataTypelevel = 5
    
        For intRow = intDataStartRow To intRowCheck
        Application.Goto Reference:="R" & CStr(intRow) & "C" & CStr(intColumn)
        If ActiveCell.Value = "" Then   ' ignore blank (null) values
        ElseIf IsDate(ActiveCell.Value) = True And Len(ActiveCell.Value) >= 8 Then
            If intdataTypelevel > 4 Then intdataTypelevel = 4
        ElseIf IsNumeric(ActiveCell.Value) = True And InStr(1, CStr(ActiveCell.Value), ".") = 0 And (Left(CStr(ActiveCell.Value), 1) <> "0" Or ActiveCell.Value = "0") And Len(ActiveCell.Value) < 10 Then
            If intdataTypelevel > 3 Then intdataTypelevel = 3
        ElseIf IsNumeric(ActiveCell.Value) = True And InStr(1, CStr(ActiveCell.Value), ".") >= 1 Then
            If intdataTypelevel > 2 Then intdataTypelevel = 2
        Else
            intdataTypelevel = 1
            Exit For
        End If
        Next intRow
        If intdataTypelevel = 5 Then intdataTypelevel = 1
        strDataType(intColumn) = strDataTypeLevel(intdataTypelevel)
    Next intColumn
    
    
    ' *** Build up the SQL
    intFormulaCount = 1
    If intHasHeaderRow = vbYes Then     ' *** Header Row ***
        Application.Goto Reference:="R4" & "C" & CStr(intlastColumn + 1)    ' Goto next column in first data row of selection
        strFormula(intFormulaCount) = "= ""SELECT "
        For intColumn = 1 To intlastColumn
        If strDataType(intColumn) = "DATETIME" Then         ' Code to take Excel Dates back to text
            strCellRef = "Text(" & fn_ColLetter(intColumn) & "4,""dd-mmm-yyyy hh:mm:ss"")"
        ElseIf strDataType(intColumn) = "VARCHAR(1000)" Then
            strCellRef = "SUBSTITUTE(" & fn_ColLetter(intColumn) & "4,""'"",""''"")" ' Convert any single ' to double ''
        Else
            strCellRef = fn_ColLetter(intColumn) & "4"
        End If
    
    
        strFormula(intFormulaCount) = strFormula(intFormulaCount) & "CAST('""& " & strCellRef & " & ""' AS " & strDataType(intColumn) & ") AS [" & strColumnHeader(intColumn) & "]"
        If intColumn < intlastColumn Then
            strFormula(intFormulaCount) = strFormula(intFormulaCount) + ", "
        Else
            strFormula(intFormulaCount) = strFormula(intFormulaCount) + " UNION ALL """
        End If
        ' since each cell can only hold a maximum no. of chars if Formula string gets too big continue formula in adjacent cell
        If Len(strFormula(intFormulaCount)) > 700 And intColumn < intlastColumn Then
            strFormula(intFormulaCount) = strFormula(intFormulaCount) + """"
            intFormulaCount = intFormulaCount + 1
            strFormula(intFormulaCount) = "= """
        End If
        Next intColumn
    
     ' Assign the formula to the cell(s) just right of the selection
        For intColumn = 1 To intFormulaCount
        ActiveCell.Value = strFormula(intColumn)
        If intColumn < intFormulaCount Then ActiveCell.Offset(0, 1).Range("A1").Select  ' Goto next column
        Next intColumn
    
    
     ' Auto Fill the formula for the full length of the selection
        ActiveCell.Offset(0, -intFormulaCount + 1).Range("A1:" & fn_ColLetter(intFormulaCount) & "1").Select
        If intLastRow > 4 Then Selection.AutoFill Destination:=Range(fn_ColLetter(intlastColumn + 1) & "4:" & fn_ColLetter(intlastColumn + intFormulaCount) & CStr(intLastRow)), Type:=xlFillDefault
    
     ' Go to start row of data selection to add 'Select into' code
       ActiveCell.Value = "SELECT * INTO " & strSQLTableName_Encap & " FROM (" & ActiveCell.Value
    
     ' Go to cells above data to insert code for deleting old table with the same name in separate SQL batch
       ActiveCell.Offset(-1, 0).Range("A1").Select  ' go to the row above
       ActiveCell.Value = "GO"
       ActiveCell.Offset(-1, 0).Range("A1").Select  ' go to the row above
       If Left(strSQLTableName, 1) = "#" Then      ' temp table
           ActiveCell.Value = "IF OBJECT_ID('tempdb.." & strSQLTableName & "') IS NOT NULL DROP TABLE " & strSQLTableName_Encap
       Else
           ActiveCell.Value = "IF OBJECT_ID('" & strSQLTableName & "') IS NOT NULL DROP TABLE " & strSQLTableName_Encap
       End If
    
    
    
    ' For Big selections (i.e. several 100 or 1000 rows) SQL Server takes a very long time to do a multiple union - Split up the table creation into many inserts
        intRow = intConstMaxBatchSize + 4   ' add 4 to make sure 1st batch = Max Batch Size
        While intRow < intLastRow
        Application.Goto Reference:="R" & CStr(intRow - 1) & "C" & CStr(intlastColumn + intFormulaCount)  ' Goto Row before intRow and the last column in formula selection
        ActiveCell.Value = Replace(ActiveCell.Value, " UNION ALL ", " ) a") ' Remove last 'UNION ALL'
    
        Application.Goto Reference:="R" & CStr(intRow) & "C" & CStr(intlastColumn + 1)    ' Goto intRow and the first column in formula selection
        ActiveCell.Value = "INSERT " & strSQLTableName_Encap & " SELECT * FROM (" & ActiveCell.Value
        intRow = intRow + intConstMaxBatchSize   ' increment intRow by intConstMaxBatchSize
        Wend
    
    
        ' Delete the last 'UNION AlL' replacing it with brackets to mark the end of the last insert
        Application.Goto Reference:="R" & CStr(intLastRow) & "C" & CStr(intlastColumn + intFormulaCount)
        ActiveCell.Value = Replace(ActiveCell.Value, " UNION ALL ", " ) a")
    
        ' Select all the formula cells
        ActiveCell.Offset(-intLastRow + 2, 1 - intFormulaCount).Range("A1:" & fn_ColLetter(intFormulaCount + 1) & CStr(intLastRow - 1)).Select
    Else    ' *** No Header Row ***
        Application.Goto Reference:="R3" & "C" & CStr(intlastColumn + 1)    ' Goto next column in first data row of selection
        strFormula(intFormulaCount) = "= ""SELECT "
    
        For intColumn = 1 To intlastColumn
        If strDataType(intColumn) = "DATETIME" Then
            strCellRef = "Text(" & fn_ColLetter(intColumn) & "3,""dd-mmm-yyyy hh:mm:ss"")"   ' Format Excel dates into a text Date format that SQL will pick up
        ElseIf strDataType(intColumn) = "VARCHAR(1000)" Then
            strCellRef = "SUBSTITUTE(" & fn_ColLetter(intColumn) & "3,""'"",""''"")"         ' Change all single ' to double ''
        Else
            strCellRef = fn_ColLetter(intColumn) & "3"
        End If
    
        ' Since no column headers: Name each column "Column001",Column002"..
        strFormula(intFormulaCount) = strFormula(intFormulaCount) & "CAST('""& " & strCellRef & " & ""' AS " & strDataType(intColumn) & ") AS [Column" & CStr(intColumn) & "]"
        If intColumn < intlastColumn Then
            strFormula(intFormulaCount) = strFormula(intFormulaCount) + ", "
        Else
            strFormula(intFormulaCount) = strFormula(intFormulaCount) + " UNION ALL """
        End If
    
        ' since each cell can only hold a maximum no. of chars if Formula string gets too big continue formula in adjacent cell
        If Len(strFormula(intFormulaCount)) > 700 And intColumn < intlastColumn Then
            strFormula(intFormulaCount) = strFormula(intFormulaCount) + """"
            intFormulaCount = intFormulaCount + 1
            strFormula(intFormulaCount) = "= """
        End If
        Next intColumn
    
        ' Assign the formula to the cell(s) just right of the selection
        For intColumn = 1 To intFormulaCount
        ActiveCell.Value = strFormula(intColumn)
        If intColumn < intFormulaCount Then ActiveCell.Offset(0, 1).Range("A1").Select  ' Goto next column
        Next intColumn
    
     ' Auto Fill the formula for the full length of the selection
        ActiveCell.Offset(0, -intFormulaCount + 1).Range("A1:" & fn_ColLetter(intFormulaCount) & "1").Select
        If intLastRow > 4 Then Selection.AutoFill Destination:=Range(fn_ColLetter(intlastColumn + 1) & "3:" & fn_ColLetter(intlastColumn + intFormulaCount) & CStr(intLastRow)), Type:=xlFillDefault
    
     ' Go to start row of data selection to add 'Select into' code
       ActiveCell.Value = "SELECT * INTO " & strSQLTableName_Encap & " FROM (" & ActiveCell.Value
    
     ' Go to cells above data to insert code for deleting old table with the same name in separate SQL batch
       ActiveCell.Offset(-1, 0).Range("A1").Select  ' go to the row above
       ActiveCell.Value = "GO"
       ActiveCell.Offset(-1, 0).Range("A1").Select  ' go to the row above
       If Left(strSQLTableName, 1) = "#" Then      ' temp table
           ActiveCell.Value = "IF OBJECT_ID('tempdb.." & strSQLTableName & "') IS NOT NULL DROP TABLE " & strSQLTableName_Encap
       Else
           ActiveCell.Value = "IF OBJECT_ID('" & strSQLTableName & "') IS NOT NULL DROP TABLE " & strSQLTableName_Encap
       End If
    
      ' For Big selections (i.e. serveral 100 or 1000 rows) SQL Server takes a very long time to do a multiple union - Split up the table creation into many inserts
       intRow = intConstMaxBatchSize + 3        ' add 3 to make sure 1st batch = Max Batch Size
        While intRow < intLastRow
        Application.Goto Reference:="R" & CStr(intRow - 1) & "C" & CStr(intlastColumn + intFormulaCount)  ' Goto Row before intRow and the last column in formula selection
        ActiveCell.Value = Replace(ActiveCell.Value, " UNION ALL ", " ) a") ' Remove last 'UNION ALL'
    
        Application.Goto Reference:="R" & CStr(intRow) & "C" & CStr(intlastColumn + 1)    ' Goto intRow and the first column in formula selection
        ActiveCell.Value = "INSERT " & strSQLTableName_Encap & " SELECT * FROM (" & ActiveCell.Value
        intRow = intRow + intConstMaxBatchSize   ' increment intRow by intConstMaxBatchSize
        Wend
    
        ' Delete the last 'UNION AlL'
        Application.Goto Reference:="R" & CStr(intLastRow) & "C" & CStr(intlastColumn + intFormulaCount)
        ActiveCell.Value = Replace(ActiveCell.Value, " UNION ALL ", " ) a")
    
        ' Select all the formula cells
        ActiveCell.Offset(-intLastRow + 1, 1 - intFormulaCount).Range("A1:" & fn_ColLetter(intFormulaCount + 1) & CStr(intLastRow)).Select
    End If
    
    
    ' Final Selection to clipboard and Cleaning of data
    Selection.Copy
    Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False              ' Repaste "Values Only" back into cells
    Selection.Replace What:="CAST('' AS", Replacement:="CAST(NULL AS", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False  ' convert all blank cells to NULL
    Selection.Replace What:="'00-Jan-1900 00:00:00'", Replacement:="NULL", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False  ' convert all blank Date cells to NULL
    Selection.Replace What:="'NULL'", Replacement:="NULL", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False  ' convert all 'NULL' cells to NULL
    Selection.Replace What:=vbTab, Replacement:=" ", LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False        ' Replace all Tabs in cells to Space to prevent Double Quotes occuring in the final paste text
    Selection.Copy
    
    
    MsgBox "SQL Code has been added to clipboard - Please Paste into SSMS window", vbOKOnly, "Transfer to SQL"
    
    Application.DisplayAlerts = False       ' Temporarily switch off Display Alerts
    ActiveWindow.Close                      ' Delete newly created worksheet
    Application.DisplayAlerts = True        ' Switch display alerts back on
    
    
    
    End Sub
    
    
    
    
    Function fn_ColLetter(Col As Integer) As String
    
    Dim strColLetter As String
    
    If Col > 26 Then
        ' double letter columns
        strColLetter = Chr(Int((Col - 1) / 26) + 64) & _
            Chr(((Col - 1) Mod 26) + 65)
    Else
        ' single letter columns
        strColLetter = Chr(Col + 64)
    End If
    fn_ColLetter = strColLetter
    End Function
    

    【讨论】:

    • 这是我正在寻找的解决方案类型。这会生成执行速度比粘贴到“编辑前 200 行”网格中快得多的 SQL 插入语句。此函数假定您正在创建一个新表。我希望通过显式命名列来插入它,这在将SET IDENTITY_INSERT [tablename] ON; 用于现有表时是必需的。如果您对此有更新,请分享或 PM 我。
    【解决方案6】:

    最简单的方法是在 XLS 中创建一个计算列,该列将生成插入语句的语法。 然后将这些插入复制到文本文件中,然后在 SQL 上执行。 其他选择是购买 Excel 的数据库连接插件并编写 VBA 代码来完成同样的任务。

    【讨论】:

      【解决方案7】:

      我认为一些数据库可以从 CSV(逗号分隔值)文件中导入数据,而您可以从 exel 中导出。或者至少使用 csv 解析器很容易(为您的语言找到一个,不要尝试自己创建一个 - 它比看起来更难)将其导入数据库。

      我不熟悉 MS SQL,但如果它直接支持它,我不会感到惊讶。

      无论如何,我认为要求必须是Exel表和数据库表中的结构相似。

      【讨论】:

        【解决方案8】:

        如果界面的工作方式与我上次使用的方式相同,您可以在 Excel 中选择区域,复制它,打开 SQL Server 并将数据粘贴到表中,就像使用 Access 一样。

        或者您可以在 Excel 和 SQL Server 之间建立一个 ODBC 链接。

        【讨论】:

          【解决方案9】:

          为什么不在 SSMS 中使用导出/导入向导?

          【讨论】:

            【解决方案10】:

            您不能使用 VBA 代码从 excel 复制并粘贴到 SSMS 操作中吗?

            【讨论】:

            • 这不是一个答案,它只是一个评论。答案应该是答案,而不是另一个问题。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多