【问题标题】:PowerShell Function to Insert data in DB does not work在数据库中插入数据的 PowerShell 函数不起作用
【发布时间】:2021-12-13 16:22:01
【问题描述】:

这是我找到的一个函数,但它给出了错误:“”找不到存储过程 'stp_CommaBulkInsert'。”

此函数创建表和键但不插入数据

你能帮帮我吗?

Function AutoImportCommaFlatFiles($location, $file, $extension, $server, $database)
{
    $full = $location + $file + $extension
    $all = Get-Content $full
    $columns = $all[0]
    $columns = $columns.Replace(" ","")
    $columns = $columns.Replace(",","] VARCHAR(255) NULL, [")
    $table = "CREATE TABLE " + $file + " ([" + $columns + "] VARCHAR(255))"
    $connection = New-Object System.Data.SqlClient.SqlConnection
    $buildTable = New-Object System.Data.SqlClient.SqlCommand
    $insertData = New-Object System.Data.SqlClient.SqlCommand
    $connection.ConnectionString = "Data Source=" + $server + ";Database=" + $database + ";integrated security=true"
    $buildTable.CommandText = $table
    $buildTable.Connection = $connection
    ## Added to function
    $x = 0
    $insertData.CommandText = "EXECUTE stp_CommaBulkInsert @1,@2"
    $insertData.Parameters.Add("@1", $full)
    $insertData.Parameters.Add("@2", $file)
    $insertData.Connection = $connection
    $connection.Open()
    $buildTable.ExecuteNonQuery()
    $connection.Close()
    ## Added to function
    $x = 1
    if ($x = 1)
    {
        $connection.Open()
        $insertData.ExecuteNonQuery()
        $connection.Close()
    }
}

【问题讨论】:

  • 错误信息是不言自明的。存储过程是否存在?您是否可能错过了架构名称?
  • 另一种可能是 SQL 实例有区分大小写的排序规则,而 sproc 的名称与命令不完全匹配。

标签: sql database function powershell datatable


【解决方案1】:

问题已解决:此 PowerShell 函数在 SQL 中创建一个对象并调用它来执行 BULK Insert。当您运行此脚本时,它将尝试创建存在的相同对象。我刚刚删除了一些我不需要的代码行,它现在可以工作了。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2017-10-07
  • 2015-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多