|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
If table exists...
Using ASP I'm trying to determine whether or not a table exists already. If it exists I want to send the user to another page. How can I determind if a table exists?
__________________
If you found a post of mine helpful, please click on the on my post to add to my reputation.
|
|
#2
|
||||
|
||||
|
In SQL Server
Code:
SELECT name FROM sysObjects WHERE name = Tablename |
|
#3
|
||||
|
||||
|
What kind of value does this return? If I am using ASP would I just create a connection and a recordset, then open the recordset with:
rs.Open "SELECT name FROM sysObjects WHERE name = Tablename", myConn How will it notify me if it exists or not? |
|
#4
|
||||
|
||||
|
try something like this
Code:
tableName = "MyTable"
Set rs = Conn.Execute("SELECT COUNT(*) As 'tableCount' FROM sysObjects WHERE name = '" & tableName & "'")
if rs("tableCount") <> 0 Then
The table already exists
else
The table doesn't exist
end if
|
|
#5
|
||||
|
||||
|
Before I begin, thank you for your help. When I run this Subroutine I don't get to execute all the code because an error occurs and I have On Error Resume Next set for all the pages. This is the code I use to determine whether a table exists:
Code:
Sub endIfTableExists
varTableName = CStr(Request.Form("tableName"))
Dim conTestExist
Dim rsTestExist
'--Set the connection
Set conTestExist = Server.CreateObject("ADODB.Connection")
conTestExist.Open "myDSN"
'--Set the recordset
'Set rsTestExist = Server.CreateObject("ADODB.Recordset")
'rsTestExist.Open "SELECT * FROM " & varTableName, conTestExist
Set rsTestExist = conTestExist.Execute("SELECT COUNT(*) As 'tableCount' FROM sysObjects WHERE name = '" & varTableName & "'")
if rsTestExist("tableCount") <> 0 Then
'The table already exists, send to this URL
Response.Redirect("categoryAlreadyExists.asp")
end if
Set rsTestExist = Nothing
Set conTestExist = Nothing
End Sub
This is the error I get: -2147217865 [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot find the input table or query 'sysObjects'. Make sure it exists and that its name is spelled correctly. I don't know whether or not it matters that I'm using MySQL |
|
#6
|
|||
|
|||
|
Yes is does matter.
You will have to find which table (hidden) in mySQL all the tables names are stored in. I don't know SQL Server uses sysObjects MS Acess uses MSysObjects mySQL uses ???? S- |
|
#7
|
||||
|
||||
|
Thanks for all your help. Much appreciated
![]() |
|
#8
|
||||
|
||||
|
I switched from MySQL to Access and used what you proposed. My issues are different now. The MSysObjects seems to be restricted from running me running
Code:
Set rsTestExist = conTestExist.Execute("SELECT COUNT(*) As 'tableCount' FROM MSysObjects WHERE name = '" & varTableName & "'")
against it. Before this escapes your mind too much I wanted to ask about what to do with this Access error... [Microsoft][ODBC Microsoft Access Driver] Record(s) cannot be read; no read permission on 'MSysObjects' Any ideas? |
|
#9
|
||||
|
||||
|
I fixed that upon further reading. That was an Access issue. I now get:
3265 Item cannot be found in the collection corresponding to the requested name or ordinal. When executing Set rsTestExist = conTestExist.Execute("SELECT COUNT(*) AS 'tableCount' FROM MSysObjects WHERE NAME = '" & varTableName & "'") I know that the value in varTableName is the same as the name of aan existing table. |
|
#10
|
||||
|
||||
|
try this. My original code was for Sql Server.
Code:
Set rsTestExist = conTestExist.Execute("SELECT COUNT(*) AS tableCount FROM MSysObjects WHERE [NAME] = [" & varTableName & "]")
|
|
#11
|
||||
|
||||
|
I guess we're getting closer. I now get:
-2147217904 [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1. |
|
#12
|
||||
|
||||
|
I've been playing with this for a bit but can't get it to work still.
This is what I have: Code:
Sub endIfTableExists
varTableName = CStr(Request.Form("tableName"))
Dim conTestExist
Dim rsTestExist
'--Set the connection
Set conTestExist = Server.CreateObject("ADODB.Connection")
conTestExist.Open "myDSN"
Set rsTestExist = conTestExist.Execute("SELECT COUNT(*) AS tableCount FROM MSysObjects WHERE [NAME] = [" & varTableName & "]")
if rsTestExist("tableCount") <> 0 Then
'The table already exists
Response.Redirect("categoryAlreadyExists.asp")
end if
Set rsTestExist = Nothing
Set conTestExist = Nothing
End Sub
I still get error -2147217904 |
|
#13
|
||||
|
||||
|
Found a solution here:
No clue why it works though... http://forums.devshed.com/showthread.php?t=137168 |
![]() |
| Viewing: ASP Free Forums > Database > SQL Development > If table exists... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |