【问题标题】:Errors while trying to import data from Excel to SQL tables尝试将数据从 Excel 导入 SQL 表时出错
【发布时间】:2016-08-08 19:09:01
【问题描述】:

我正在尝试将数据从 Excel 导入 SQL Server 表,但出现此错误。你能建议如何避免这些吗?

【问题讨论】:

    标签: import ssis task import-from-excel dataflow


    【解决方案1】:

    这可能是由 ACYR_ISN 列被设置为身份引起的。

    请参阅此帖子了解如何解决此问题: https://stackoverflow.com/a/20953512/2280148

    【讨论】:

    • 此处的帖子中不允许使用标语,尤其是那些宣传产品的帖子,因此我已删除了您答案的那部分。如果您愿意,欢迎您将此信息放在您的个人资料中。
    【解决方案2】:

    您可以使用 T-SQL 将 excel 文件导入 MSSQL 数据库。

    DECLARE @strQuery AS VARCHAR(600),
            @strFilePath as VARCHAR(200),
            @TableName AS VARCHAR(50),
            @EmpCode varchar(8), 
            @Name varchar(50)
    
    Declare @DataTable as table
        (
           EmpCode varchar(8), 
           Name varchar(50)
         )
    
    
    SET @strFilePath='\\Path\Excel.xlsx'
    SET @TableName='Sheet1'
    
    SET @strQuery='select * from openrowset('
        SET @strQuery=@strQuery+'''Microsoft.ACE.OLEDB.12.0'''+','
        set @strQuery=@strQuery+ '''Excel 12.0;Database='+@strFilePath+''''+','
        SET @strQuery=@strQuery+'''select * from ['+@TableName+'$]'''+')'
    
    insert into @DataTable 
    EXECUTE(@strQuery)
    
    
    declare curStaff cursor for
    select * from @DataTable
    open curStaff
    fetch from curStaff into @EmpCode, @Name
    while @@FETCH_STATUS =0
        begin
          //Do your work here by using data
        fetch from curStaff into  @EmpCode, @Name 
    end
    close curStaff
    deallocate curStaff
    

    【讨论】:

      猜你喜欢
      • 2015-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      • 2013-05-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      相关资源
      最近更新 更多