查询超时已过期80040E31

时间:2017-05-26 23:11:36

标签: sql sql-server vbscript

这个vbs脚本每晚都在运行几年。现在我收到超时错误。它会在几秒钟内超时。

DataSource = "127.0.0.1"
DatabaseName = "xxxxxxxx"
DBUser = "xx"
DBPassword = "xxxxxx"

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'This is the original SQL statement
    SQL = "SELECT top 1000000 CONVERT(VARCHAR(10),TimeStamp,101) + ' ' + CONVERT(CHAR(8),(CONVERT(DATETIME,TimeStamp,113)),114), LogicName, PointValue FROM Trends order by LogicName"
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' I tried to remove one of the '0's but it still times out.

SQL = "SELECT top 100000 CONVERT(VARCHAR(10),TimeStamp,101) + ' ' + CONVERT(CHAR(8),(CONVERT(DATETIME,TimeStamp,113)),114), LogicName, PointValue FROM Trends order by LogicName"
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Interesting that this query runs fine.  It takes a good 10 minutes to run
SQL = "SELECT top 100000 TimeStamp,LogicName, PointValue  from Trends
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim objConnection,objRecordset,strSearchCriteria
    Const adOpenStatic = 3
    Const adLockOptimistic = 3
    Const adUseClient = 3

    Set objConnection = CreateObject("ADODB.Connection")
    Set objRecordset = CreateObject("ADODB.Recordset")

    objConnection.Open "Provider=sqloledb;Data Source=" & Datasource & ";Initial Catalog=" & DatabaseName & ";User Id=" & DBUser & ";Password=" & DBPassword 

    'MsgBox "Connected"

    objRecordset.Open SQL, objConnection, adOpenStatic, adLockOptimistic

3 个答案:

答案 0 :(得分:0)

在连接字符串中,默认为30秒,至少将超时时间增加到900秒。

答案 1 :(得分:0)

问题是通过不要求1000000条记录来解决的。相反,我改变了他的脚本以获取所需日期之间的所有记录。

答案 2 :(得分:0)

您需要在ADO字符串中更改两个值以扩展ADO连接。

A)设置CommandTimeout = 300 B)设置ConnectionTimeout = 300

此外,如果查询很大,您可能需要设置 ARITHABORT ON (打开)。
下面的代码示例

<%
    Server.ScriptTimeout=600
    Set conn = Server.CreateObject("ADODB.Connection")
    conn.ConnectionString = "Provider=SQLOLEDB; Data Source = (local); Initial Catalog = Your-DB-Name; User Id = sa; Password=Your-Passoword"
    conn.ConnectionTimeout = 300
    conn.CommandTimeout = 300
    conn.open
    
    set rs= Server.CreateObject("ADODB.Recordset")
    rs.locktype=3
    rs.cursortype=2
    
    SQL="set ARITHABORT ON select * from table" 
    
    rs.Open SQL, conn
    while not rs.eof 
    response.write(rs(0))
    rs.movenext
    wend
    
    rs.close
    conn.close
%>

您还可以在rs连接示例response.write(rs(“ FirstName”))

中使用字段/列名。