使用vbscript和SQL创建临时表

时间:2014-03-23 14:59:54

标签: sql vbscript

我创建了vbscript,我需要找到sql server的错误日志大小并显示。

strConnectString1 = "PROVIDER=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master;Data Source=" & Server & ";"

Set cn1 = CreateObject("ADODB.Connection")
    'Establish Connection with Server
    cn1.Open strConnectString1
    'MsgBox(Server)
    If Err.Number <> 0 Then
        MsgBox "Error while Connecting to server" & Server & vbCRLF & "Error Detail: " & err.description,16,"Error"
    end if

    set cmd1 = createobject("ADODB.Command")
    set rs1 = createobject("ADODB.Recordset")
    set cmd2= createobject("ADODB.Command")
    set rs2 = createobject("ADODB.Recordset")
    set cmd3= createobject("ADODB.Command")
    set rs3 = createobject("ADODB.Recordset")


    cmd1.activeconnection =cn1
    cmd2.ActiveConnection=cn1
    cmd3.ActiveConnection=cn1
    'msgbox(server)
    cmd1.CommandTimeout= 20


    cmd1.commandtext = "select @@version"                           
    set rs1 = cmd1.execute

    str= InStr(rs1(0),"-")
    'MsgBox(str)
    version=mid(rs1(0),str+1,5)     

        if version=9.00 then
          mssqlversion="MSSQL Server 2005"      
        elseif  version=10.0 then               
          mssqlversion="MSSQL Server 2008"    
        elseif version=10.5 then
           mssqlversion="MSSQL Server 2008 R2" 
        elseif version=11.0  then
            mssqlversion="MSSQL Server 2012"
        else 
             mssqlversion="MSSQL Server"
        end if



        message1="<B>Server :" & Server & "</B><BR><BR>" & _
        "<Table border =""1""><tr><td><b>Server</b></td><td><b>SQL Version</b></td><td><b>Path</b></td><td><b>Size</b></td><td><b>Recycle(Y\N)</b></td></B></tr>"

        message = message & "<font color='#0F00CD'><tr>"

while rs1.eof<>true and rs1.bof<>true


        cmd2.commandtext="xp_readerrorlog 1,1,N'Logging SQL Server Messages'"
        set rs2=cmd2.Execute


    if rs2.EOF<> true and rs2.BOF <> true then


                path=instr(rs2(2),"file")


                filepath=mid(rs2(2),path+6,len(rs2(2))-(path+7))


                MsgBox(filepath)

               strsql =strsql & "use tempdb set nocount on"
               strsql = strsql  & " declare @line varchar(255) declare @path varchar(255) declare @command varchar(255) declare @size varchar(255)"
               strsql = strsql & " if object_id('#output') is not null and objectproperty(object_id('#output'),'IsTable') = 1"                
               strsql= strsql & " if exists(select * from tempdb.sys.sysobjects where name like '#output%')"
               strsql= strsql & " drop table #output"               
               strsql = strsql & " create table #output (line varchar(255))"
               strsql = strsql & " set @path = "&"'"& filepath & "'"
               strsql = strsql & " set @command = "& "'" &"dir """ & filepath & """"& "'" &""                 
               strsql = strsql & " insert into #output exec master.dbo.xp_cmdshell @command"
               strsql = strsql & " select @line = ltrim(replace(substring(line, charindex(')', line)+1,len(line)), ',', ''))from #output where line like '%File(s)%bytes'" 
               strsql = strsql & " set @size='100' select @size 'Size'"             


                    MsgBox(strsql)
                    MsgBox(cmd3.State)
                    MsgBox(cn1.State)
                    cmd3.CommandText=strsql
                    set rs3=cmd3.Execute

                    while rs3.EOF<> true and rs3.BOF<> true 
                        MsgBox(rs3(0))
                        rs3.MoveNext
                   wend

                fontColour="#FF3300"    
                    message1 = message1 & "<td><font color='" & fontColour & "'>" & Server & "</font></td><td><font color='" & fontColour & "'>" & mssqlversion & "</font></td><td><font color='" & fontColour & "'>" & _
                        filepath & "</font></td><td><font color='" & fontColour & "'>" & Server & "</font></td><td><font color='" & fontColour & "'>" & Server & "</font></td>"
                rs1.movenext
                rs2.MoveNext

                message1=message1 & "</tr></font>"          
    end if              
    wend                                
    message=message & message1 &"</Table><br>"
    cn1.close
Loop

我收到错误对象已关闭,在strsql中我正在执行创建临时表的代码并分配errorlog文件的大小,但它给我的错误对象是关闭的,有时是临时表错误。 帮助将非常感激,自从过去3天以来我一直在努力使用此代码

1 个答案:

答案 0 :(得分:0)

'strConnectString1 = "PROVIDER=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master;Data Source="deepti-PC\deepti";
strConnectString1 = "PROVIDER=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master;Data Source=DEEPTI-PC;"

Set cn1 = CreateObject("ADODB.Connection")
    'Establish Connection with Server
    cn1.Open strConnectString1
    msgbox(cn1.state)
        MsgBox(Server)
    msgbox(Err.Number)
    If Err.Number <> 0 Then
        MsgBox "Error while Connecting to server" & Server & vbCRLF & "Error Detail: " & err.description,16,"Error"
    end if

 set cmd1 = createobject("ADODB.Command")
    set rs1 = createobject("ADODB.Recordset")
     set cmd2= createobject("ADODB.Command")
    set rs2 = createobject("ADODB.Recordset")
     set cmd3= createobject("ADODB.Command")
    set rs3 = createobject("ADODB.Recordset")


cmd1.activeconnection =cn1
cmd2.activeconnection=cn1
cmd3.activeconnection=cn1
cmd1.commandtext = "select @@version"                           
    set rs1 = cmd1.execute
    msgbox(rs1(0))
    str= InStr(rs1(0),"-")

        version=mid(rs1(0),str+1,5) 
        msgbox(version)  



        while rs1.eof<>true and rs1.bof<>true

            msgbox(rs1(0))
            rs1.movenext

            cmd2.commandtext="xp_readerrorlog 1,1,N'Logging SQL Server Messages'"
                set rs2=cmd2.Execute

            while rs2.eof<>true and rs2.eof<> true 

                            msgbox(rs2(2))
                             path=instr(rs2(2),"file")
                             filepath=mid(rs2(2),path+6,len(rs2(2))-(path+7))

                                  MsgBox(filepath)

                    'strsql =strsql & "use tempdb set nocount on"
                        strsql = strsql  & " declare @line varchar(255) declare @path varchar(255) declare @command varchar(255) declare @size varchar(255)"
                           strsql = strsql & " if object_id('#output') is not null and objectproperty(object_id('#output'),'IsTable') = 1"                
                           strsql= strsql & " if exists(select * from tempdb.sys.sysobjects where name like '#output%')"
                           strsql= strsql & " drop table #output"               
                          strsql = strsql & " create table #output (line varchar(255))"
                           strsql = strsql & " set @path = "&"'"& filepath & "'"
                           strsql = strsql & " set @command = "& "'" &"dir """ & filepath & """"& "'" &""                 
                           strsql = strsql & " insert into #output exec master.dbo.xp_cmdshell @command"
                          ' strsql = strsql & " select @line = ltrim(replace(substring(line, charindex(')', line)+1,len(line)), ',', ''))from #output where line like '%File(s)%bytes'" 
                          strsql = strsql & " set @size='100' select @size 'Size'"             


                    MsgBox(strsql)
                                cmd3.commandtext=strsql
                    set rs3=cmd3.execute


                     while rs3.eof<> true and rs3.bof<> true

                        msgbox(rs3(0))
                        rs3.movenext
                     wend




            rs2.movenext
            wend


        wend