日期:2014-05-17  浏览次数:20787 次

asp 如何用语句查询视图是否存在?
下面的函数是查询表是否存在,但视图是否存在怎么查?

Function isTableExists(DbConn, TableName ) '检查表是否存在
 Dim chkRs
 Set chkRs = DbConn.openSchema(20)
 chkRs.MoveFirst
 Do Until chkRs.EOF
  If chkRs("TABLE_TYPE") = "TABLE" then
  If chkRs("TABLE_NAME") = TableName Then
  isTableExists = True
  chkRs.Close
  Set chkRs = Nothing
  Exit Function
  End if
  End if
 chkRs.MoveNext
 Loop
 chkRs.Close
 Set chkRs = Nothing
 isTableExists = False
End Function


------解决方案--------------------
VBScript code

<%
sTable = "view_xxx"
Response.Write isViewExists(sTable)

Function isViewExists(sTable)
    On Error Resume Next
    Dim conn, sConn, oCat, colTables, oTable
    sConn = "Provider=SQLOLEDB;Data source=127.0.0.1;Initial Catalog=db;User ID=sa;Password=xxx;"
    Set conn = CreateObject("ADODB.Connection")
    conn.Open sConn
    Set oCat = CreateObject("ADOX.Catalog")
    Set oCat.ActiveConnection = conn
    Set colTables   = oCat.Tables
    Set oTable      = colTables(sTable)
    If oTable Is Nothing Then
        isViewExists = False
    Else
        isViewExists = True
    End If
    Set oTable = Nothing
    Set colTables = Nothing
    Set oCat = Nothing
    conn.Close
    Set conn = Nothing
End Function
%>