SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old April 23rd, 2004, 02:22 PM
shamrog12's Avatar
shamrog12 shamrog12 is offline
Newton's Apple Wizard
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 1,661 shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 2 h 39 m 22 sec
Reputation Power: 35
Send a message via AIM to shamrog12
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.


Reply With Quote
  #2  
Old April 23rd, 2004, 02:27 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
In SQL Server
Code:
SELECT name
FROM sysObjects
WHERE name = Tablename

Reply With Quote
  #3  
Old April 23rd, 2004, 02:32 PM
shamrog12's Avatar
shamrog12 shamrog12 is offline
Newton's Apple Wizard
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 1,661 shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 2 h 39 m 22 sec
Reputation Power: 35
Send a message via AIM to shamrog12
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?

Reply With Quote
  #4  
Old April 23rd, 2004, 02:36 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
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

Reply With Quote
  #5  
Old April 23rd, 2004, 04:07 PM
shamrog12's Avatar
shamrog12 shamrog12 is offline
Newton's Apple Wizard
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 1,661 shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 2 h 39 m 22 sec
Reputation Power: 35
Send a message via AIM to shamrog12
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

Reply With Quote
  #6  
Old April 23rd, 2004, 05:36 PM
sbaxter sbaxter is offline
Moderator: Access, SQL
ASP Free God (5000 - 5499 posts)
 
Join Date: Oct 2003
Posts: 5,126 sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 1 h 2 m 51 sec
Reputation Power: 13
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-

Reply With Quote
  #7  
Old April 23rd, 2004, 05:43 PM
shamrog12's Avatar
shamrog12 shamrog12 is offline
Newton's Apple Wizard
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 1,661 shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 2 h 39 m 22 sec
Reputation Power: 35
Send a message via AIM to shamrog12
Thanks for all your help. Much appreciated

Reply With Quote
  #8  
Old April 23rd, 2004, 05:51 PM
shamrog12's Avatar
shamrog12 shamrog12 is offline
Newton's Apple Wizard
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 1,661 shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 2 h 39 m 22 sec
Reputation Power: 35
Send a message via AIM to shamrog12
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?

Reply With Quote
  #9  
Old April 23rd, 2004, 06:09 PM
shamrog12's Avatar
shamrog12 shamrog12 is offline
Newton's Apple Wizard
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 1,661 shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 2 h 39 m 22 sec
Reputation Power: 35
Send a message via AIM to shamrog12
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.

Reply With Quote
  #10  
Old April 23rd, 2004, 06:50 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,781 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 45 m 55 sec
Reputation Power: 470
try this. My original code was for Sql Server.
Code:
Set rsTestExist = conTestExist.Execute("SELECT COUNT(*) AS tableCount FROM MSysObjects WHERE [NAME] = [" & varTableName & "]")

Reply With Quote
  #11  
Old April 23rd, 2004, 08:33 PM
shamrog12's Avatar
shamrog12 shamrog12 is offline
Newton's Apple Wizard
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 1,661 shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 2 h 39 m 22 sec
Reputation Power: 35
Send a message via AIM to shamrog12
I guess we're getting closer. I now get:

-2147217904
[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.

Reply With Quote
  #12  
Old April 24th, 2004, 11:53 PM
shamrog12's Avatar
shamrog12 shamrog12 is offline
Newton's Apple Wizard
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 1,661 shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 2 h 39 m 22 sec
Reputation Power: 35
Send a message via AIM to shamrog12
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

Reply With Quote
  #13  
Old April 25th, 2004, 12:54 AM
shamrog12's Avatar
shamrog12 shamrog12 is offline
Newton's Apple Wizard
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2003
Location: Los Angeles
Posts: 1,661 shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level)shamrog12 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Weeks 2 Days 2 h 39 m 22 sec
Reputation Power: 35
Send a message via AIM to shamrog12
Found a solution here:

No clue why it works though...

http://forums.devshed.com/showthread.php?t=137168

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > If table exists...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread