Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 调用SqlCmd QueryTimeout_Sql Server_Sql Server 2008_Powershell_Powershell 2.0 - Fatal编程技术网

Sql server 调用SqlCmd QueryTimeout

Sql server 调用SqlCmd QueryTimeout,sql-server,sql-server-2008,powershell,powershell-2.0,Sql Server,Sql Server 2008,Powershell,Powershell 2.0,有人知道如何将Invoke SqlCmd QueryTimeout设置为65535以上吗 微软说他们已经在Denali中修复了它,但我们仍然在使用SQL 2008 R2和最新的服务包 基本上,我们正在尝试使用powershell备份或恢复数据库。我们的一些数据库非常大,因此完成这项工作需要65535多个数据库 有人建议在powershell中使用带超时的ADO.NET。但是我想知道我们是否有任何解决办法来调用SqlCmd…您可以编写自己版本的Invoke-SqlCmd,直接使用System.Da

有人知道如何将Invoke SqlCmd QueryTimeout设置为65535以上吗

微软说他们已经在Denali中修复了它,但我们仍然在使用SQL 2008 R2和最新的服务包

基本上,我们正在尝试使用powershell备份或恢复数据库。我们的一些数据库非常大,因此完成这项工作需要65535多个数据库


有人建议在powershell中使用带超时的ADO.NET。但是我想知道我们是否有任何解决办法来调用SqlCmd…

您可以编写自己版本的Invoke-SqlCmd,直接使用System.Data.SqlClient对象,并对其执行任何您想要的操作。这里有很多关于如何实现这一点的示例,其中包括专门为绕过QueryTimeout bug而编写的、托管在Microsoft脚本库中的。如果不想部署这样的脚本,可以直接将相关代码集成到备份脚本中


或者,您应该能够使用SMO备份数据库。IIRC,querytimeout错误不会影响SMO。

您可以编写自己版本的Invoke SqlCmd,直接使用System.Data.SqlClient对象,并对其执行任何操作。这里有很多关于如何实现这一点的示例,其中包括专门为绕过QueryTimeout bug而编写的、托管在Microsoft脚本库中的。如果不想部署这样的脚本,可以直接将相关代码集成到备份脚本中


或者,您应该能够使用SMO备份数据库。IIRC,querytimeout错误不会影响SMO。

您可以使用以下函数自定义您自己的查询超时和会话超时限制,同时定义可以为您提供所需值的参数

function Invoke-SqlCommand 
{ 
    [CmdletBinding()] 
    param( 
    [Parameter(Position=0, Mandatory=$true)] [string]$ServerInstance, 
    [Parameter(Position=1, Mandatory=$false)] [string]$Database, 
    [Parameter(Position=2, Mandatory=$false)] [string]$Query, 
    [Parameter(Position=3, Mandatory=$false)] [string]$Username, 
    [Parameter(Position=4, Mandatory=$false)] [string]$Password, 
    [Parameter(Position=5, Mandatory=$false)] [Int32]$QueryTimeout=600, 
    [Parameter(Position=6, Mandatory=$false)] [Int32]$ConnectionTimeout=15, 
    [Parameter(Position=7, Mandatory=$false)] [ValidateScript({test-path $_})] [string]$InputFile, 
    [Parameter(Position=8, Mandatory=$false)] [ValidateSet("DataSet", "DataTable", "DataRow")] [string]$As="DataRow" 
    ) 

    if ($InputFile) 
    { 
        $filePath = $(resolve-path $InputFile).path 
        $Query =  [System.IO.File]::ReadAllText("$filePath") 
    } 

    $conn=new-object System.Data.SqlClient.SQLConnection 

    if ($Username) 
    { $ConnectionString = "Server={0};Database={1};User ID={2};Password={3};Trusted_Connection=False;Connect Timeout={4}" -f $ServerInstance,$Database,$Username,$Password,$ConnectionTimeout } 
    else 
    { $ConnectionString = "Server={0};Database={1};Integrated Security=True;Connect Timeout={2}" -f $ServerInstance,$Database,$ConnectionTimeout } 

    $conn.ConnectionString=$ConnectionString 

    #Following EventHandler is used for PRINT and RAISERROR T-SQL statements. Executed when -Verbose parameter specified by caller 
    if ($PSBoundParameters.Verbose) 
    { 
        $conn.FireInfoMessageEventOnUserErrors=$true 
        $handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] {Write-Verbose "$($_)"} 
        $conn.add_InfoMessage($handler) 
    } 

    $conn.Open() 
    $cmd=new-object system.Data.SqlClient.SqlCommand($Query,$conn) 
    $cmd.CommandTimeout=$QueryTimeout 
    $ds=New-Object system.Data.DataSet 
    $da=New-Object system.Data.SqlClient.SqlDataAdapter($cmd) 
    [void]$da.fill($ds) 
    $conn.Close() 
    switch ($As) 
    { 
        'DataSet'   { Write-Output ($ds) } 
        'DataTable' { Write-Output ($ds.Tables) } 
        'DataRow'   { Write-Output ($ds.Tables[0]) } 
    } 

}

希望有帮助。

您可以使用下面的函数自定义您自己的查询超时和会话超时限制,同时定义可以为您提供所需值的参数

function Invoke-SqlCommand 
{ 
    [CmdletBinding()] 
    param( 
    [Parameter(Position=0, Mandatory=$true)] [string]$ServerInstance, 
    [Parameter(Position=1, Mandatory=$false)] [string]$Database, 
    [Parameter(Position=2, Mandatory=$false)] [string]$Query, 
    [Parameter(Position=3, Mandatory=$false)] [string]$Username, 
    [Parameter(Position=4, Mandatory=$false)] [string]$Password, 
    [Parameter(Position=5, Mandatory=$false)] [Int32]$QueryTimeout=600, 
    [Parameter(Position=6, Mandatory=$false)] [Int32]$ConnectionTimeout=15, 
    [Parameter(Position=7, Mandatory=$false)] [ValidateScript({test-path $_})] [string]$InputFile, 
    [Parameter(Position=8, Mandatory=$false)] [ValidateSet("DataSet", "DataTable", "DataRow")] [string]$As="DataRow" 
    ) 

    if ($InputFile) 
    { 
        $filePath = $(resolve-path $InputFile).path 
        $Query =  [System.IO.File]::ReadAllText("$filePath") 
    } 

    $conn=new-object System.Data.SqlClient.SQLConnection 

    if ($Username) 
    { $ConnectionString = "Server={0};Database={1};User ID={2};Password={3};Trusted_Connection=False;Connect Timeout={4}" -f $ServerInstance,$Database,$Username,$Password,$ConnectionTimeout } 
    else 
    { $ConnectionString = "Server={0};Database={1};Integrated Security=True;Connect Timeout={2}" -f $ServerInstance,$Database,$ConnectionTimeout } 

    $conn.ConnectionString=$ConnectionString 

    #Following EventHandler is used for PRINT and RAISERROR T-SQL statements. Executed when -Verbose parameter specified by caller 
    if ($PSBoundParameters.Verbose) 
    { 
        $conn.FireInfoMessageEventOnUserErrors=$true 
        $handler = [System.Data.SqlClient.SqlInfoMessageEventHandler] {Write-Verbose "$($_)"} 
        $conn.add_InfoMessage($handler) 
    } 

    $conn.Open() 
    $cmd=new-object system.Data.SqlClient.SqlCommand($Query,$conn) 
    $cmd.CommandTimeout=$QueryTimeout 
    $ds=New-Object system.Data.DataSet 
    $da=New-Object system.Data.SqlClient.SqlDataAdapter($cmd) 
    [void]$da.fill($ds) 
    $conn.Close() 
    switch ($As) 
    { 
        'DataSet'   { Write-Output ($ds) } 
        'DataTable' { Write-Output ($ds.Tables) } 
        'DataRow'   { Write-Output ($ds.Tables[0]) } 
    } 

}
Invoke-Sqlcmd -query 'select * from largeDb' -QueryTimeout 0 希望能有帮助

Invoke-Sqlcmd -query 'select * from largeDb' -QueryTimeout 0 -QueryTimeout 0:将使您的cmdlet不超时

很快,这个错误就会被解决

-QueryTimeout 0:将使您的cmdlet不超时


很快,这个错误就会解决。

您决定使用Invoke SqlCmd吗?如果您无法将客户端升级到SQL 2012,您可以尝试通过使用cmd/c直接执行SQLCMD或使用SMO执行备份/还原来解决此问题。将查询超时设置为零将导致它等待命令完成,而不管需要多长时间。PS模块的版本是最重要的,不是服务器的版本。Windows/PowerShell的最新版本允许您更新模块SqlServer并使用-QueryTimeout 0。是否确定使用Invoke SqlCmd?如果您无法将客户端升级到SQL 2012,您可以尝试通过使用cmd/c直接执行SQLCMD或使用SMO执行备份/还原来解决此问题。将查询超时设置为零将导致它等待命令完成,而不管需要多长时间。PS模块的版本是最重要的,不是服务器的版本。最新版本的Windows/PowerShell允许您更新模块SqlServer并使用-QueryTimeout 0.SMO查询超时,其中包括备份,默认情况下为10分钟。如果你想要更长的时间,你需要设置它。嘿,6年后有更好的吗?MS已将SQLPS更新为$moduleName=SqlServer。。。导入模块$moduleName-详细,但我仍然得到10MB dB.SMO查询超时的超时,包括备份,默认情况下为10分钟。如果你想要更长的时间,你需要设置它。嘿,6年后有更好的吗?MS已将SQLPS更新为$moduleName=SqlServer。。。导入模块$moduleName-详细,但我仍然在10MB数据库上超时。在从脚本添加索引时对我不起作用。仍然默认为60秒“如果不存在,请从sys.indexes中选择名称,其中name=N'IX_TableName_ColumnName”开始在[dbo].[TableName][ColumnName]INCLUDE[ColumnName 2],[ColumnName 3]END GO]上创建非聚集索引IX_TableName_ColumnName”,这是我的SQL脚本中的内容。从脚本添加索引时,这对我不起作用。仍然默认为60秒“如果不存在,请从sys.indexes中选择名称,其中name=N'IX_TableName_ColumnName”开始在[dbo].[TableName][ColumnName]中创建非聚集索引IX_TableName_ColumnName]包括[ColumnName 2],[ColumnName 3]END GO'-这是我的SQL脚本中的内容。