Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 Checking if a table exists
 New Topic  Topic Locked
 Printer Friendly
Next Page
Author Previous Topic Topic Next Topic
Page: of 2

Da_Stimulator
DEV Team Forum Moderator

USA
3373 Posts

Posted - 30 December 2004 :  17:21:49  Show Profile  Send Da_Stimulator an AOL message  Send Da_Stimulator a Yahoo! Message
For a setup script I'm working on, I need to check and see if a table exists in the database before creating that table... is there a way I can check this without producing an error?

I saw an article on 4Guys about something like:

if EXISTS (SELECT * FROM INFORMATION_SCHEMA.tables WHERE table_name='MyTable')
select * from MyTable

but that wasnt too informative to me... I'm working with sql server from asp point of view, and I dont know how to translate that into a query...

-Stim

D3mon
Senior Member

United Kingdom
1685 Posts

Posted - 30 December 2004 :  17:28:00  Show Profile  Visit D3mon's Homepage
that could be used as part of a Stored Procedure, what do you know of those?


Snitz 'Speedball' : Site Integration Mod : Friendly Registration Mod
"In war, the victorious strategist only seeks battle after the victory has been won"
Go to Top of Page

Da_Stimulator
DEV Team Forum Moderator

USA
3373 Posts

Posted - 30 December 2004 :  17:31:17  Show Profile  Send Da_Stimulator an AOL message  Send Da_Stimulator a Yahoo! Message
not much, and the application I'm writing isnt big enough to warrant a learning experience I dont think.... unless there's no other way

-Stim
Go to Top of Page

D3mon
Senior Member

United Kingdom
1685 Posts

Posted - 30 December 2004 :  17:37:33  Show Profile  Visit D3mon's Homepage
wait 1 and I'll try something out...


Snitz 'Speedball' : Site Integration Mod : Friendly Registration Mod
"In war, the victorious strategist only seeks battle after the victory has been won"
Go to Top of Page

D3mon
Senior Member

United Kingdom
1685 Posts

Posted - 30 December 2004 :  17:43:16  Show Profile  Visit D3mon's Homepage
You should be able to use that directly as a query with SQL Server:

strSQL = "IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.tables WHERE table_name='[name of your table here]') SELECT * from [same table name here]"

This will check if that table exists and, if so, it will pull all the records from it. Change the last part to do what you want like:

strSQL = "IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.tables WHERE table_name='[name of your table here]') CREATE TABLE Person ( LastName varchar, FirstName varchar, Address varchar, Age int )"

that help any?


Snitz 'Speedball' : Site Integration Mod : Friendly Registration Mod
"In war, the victorious strategist only seeks battle after the victory has been won"
Go to Top of Page

D3mon
Senior Member

United Kingdom
1685 Posts

Posted - 30 December 2004 :  17:44:29  Show Profile  Visit D3mon's Homepage
more info about creating tables by query


Snitz 'Speedball' : Site Integration Mod : Friendly Registration Mod
"In war, the victorious strategist only seeks battle after the victory has been won"
Go to Top of Page

Da_Stimulator
DEV Team Forum Moderator

USA
3373 Posts

Posted - 30 December 2004 :  17:45:16  Show Profile  Send Da_Stimulator an AOL message  Send Da_Stimulator a Yahoo! Message
thx alot D3mon

-Stim
Go to Top of Page

D3mon
Senior Member

United Kingdom
1685 Posts

Posted - 30 December 2004 :  17:52:34  Show Profile  Visit D3mon's Homepage
np


Snitz 'Speedball' : Site Integration Mod : Friendly Registration Mod
"In war, the victorious strategist only seeks battle after the victory has been won"
Go to Top of Page

Da_Stimulator
DEV Team Forum Moderator

USA
3373 Posts

Posted - 30 December 2004 :  22:55:00  Show Profile  Send Da_Stimulator an AOL message  Send Da_Stimulator a Yahoo! Message
Didnt work D3mon :( I dunno why, it never bothered creating the tables...


strsql = "IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.tables WHERE table_name='KPOLL_CONFIG') "
strsql1 = "CREATE TABLE KPOLL_CONFIG (" & _
		  "CONFIG_ID INT IDENTITY NOT NULL, " & _
		  "CONFIG_VARIABLE VARCHAR(100) NOT NULL, " & _
		  "CONFIG_VALUE VARCHAR(100) NOT NULL" & _
		  ")"

  pConn.execute(strsql & strsql1)

strsql = "IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.tables WHERE table_name='KPOLL') "
strsql1 = "CREATE TABLE KPOLL (" & _
          "POLL_ID INT IDENTITY NOT NULL, " & _
          "POLL_TITLE VARCHAR(100) NOT NULL, " & _
          "POLL_ACTIVE INT NOT NULL DEFAULT 0" & _
          ")"
          
  pConn.execute(strsql & strsql1)
strsql = "IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.tables WHERE table_name='KPOLL_OPTIONS') "
strsql1 = "CREATE TABLE KPOLL_OPTIONS ("
strsql1 = strsql1 & "OPTION_ID INT IDENTITY NOT NULL, " & _
                    "POLL_ID INT NOT NULL, " & _
                    "OPTION_VALUE VARCHAR(100) NOT NULL, " & _
                    "OPTION_VOTES INT NOT NULL DEFAULT 0" & _
                    ")"  
  pConn.execute(strsql & strsql1)

-Stim
Go to Top of Page

Da_Stimulator
DEV Team Forum Moderator

USA
3373 Posts

Posted - 30 December 2004 :  23:01:01  Show Profile  Send Da_Stimulator an AOL message  Send Da_Stimulator a Yahoo! Message
aha, got it working, works great, thx!!

was a bug in the function I had to check whether or not to get that far in the code to create the tables :P

Now... is there any way to load whether or not a table exists into an asp variable boolean?

-Stim

Edited by - Da_Stimulator on 30 December 2004 23:11:38
Go to Top of Page

D3mon
Senior Member

United Kingdom
1685 Posts

Posted - 31 December 2004 :  06:31:15  Show Profile  Visit D3mon's Homepage
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.tables WHERE table_name='KPOLL')
SELECT 1 ELSE SELECT 0

should work returning a 1 for 'exists' and 0 for 'doesnt'

then,

doesTableExist = objRS(0)


Snitz 'Speedball' : Site Integration Mod : Friendly Registration Mod
"In war, the victorious strategist only seeks battle after the victory has been won"

Edited by - D3mon on 31 December 2004 06:32:37
Go to Top of Page

Da_Stimulator
DEV Team Forum Moderator

USA
3373 Posts

Posted - 31 December 2004 :  07:04:33  Show Profile  Send Da_Stimulator an AOL message  Send Da_Stimulator a Yahoo! Message
so doesTableExist = objRS(0) will return a 0 or 1 value (false or true as boolean)?

-Stim
Go to Top of Page

D3mon
Senior Member

United Kingdom
1685 Posts

Posted - 31 December 2004 :  10:14:07  Show Profile  Visit D3mon's Homepage
if the table exists then you'll get a 1 back in your recordset, otherwise 0 (zero). ASP should be able to recognise this as True(1) and False(0) quite easily. If you still have problems, get SQL to return a string instead like:

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.tables WHERE table_name='KPOLL')
SELECT 'True' ELSE SELECT 'False'


Snitz 'Speedball' : Site Integration Mod : Friendly Registration Mod
"In war, the victorious strategist only seeks battle after the victory has been won"
Go to Top of Page

Da_Stimulator
DEV Team Forum Moderator

USA
3373 Posts

Posted - 05 January 2005 :  06:00:48  Show Profile  Send Da_Stimulator an AOL message  Send Da_Stimulator a Yahoo! Message
Ok, thanks alot for your help so far D3mon

Now some more inquiries... now that I have that down and working to my liking.

Can I combine statements like that to produce multiple records and/or an array of data?

For example, could I execute something like this, then retrieve the true/false bit for each table somehow?


IF EXISTS 
  (
  SELECT * FROM INFORMATION_SCHEMA.tables WHERE table_name='TABLE1' 
  OR table_name='TABLE2' 
  OR table_name='TABLE3'
  ) 
  SELECT 1 ELSE SELECT 0


Also, could this type of code be used to check if a stored procedure exists or not? if so how?

-Stim

Edited by - Da_Stimulator on 05 January 2005 06:06:58
Go to Top of Page

D3mon
Senior Member

United Kingdom
1685 Posts

Posted - 05 January 2005 :  06:18:45  Show Profile  Visit D3mon's Homepage
If it was me, I'd run a seperate query for each table, just to keep it simple.

have a look at this link: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_sys-o_4zll.asp

then try something like this:
if exists(select * from sysobjects where type = 'P' and name = 'my_storedprocedure')


Snitz 'Speedball' : Site Integration Mod : Friendly Registration Mod
"In war, the victorious strategist only seeks battle after the victory has been won"

Edited by - D3mon on 05 January 2005 06:19:54
Go to Top of Page

Da_Stimulator
DEV Team Forum Moderator

USA
3373 Posts

Posted - 07 January 2005 :  01:35:00  Show Profile  Send Da_Stimulator an AOL message  Send Da_Stimulator a Yahoo! Message
D3mon, I'm currently playign around with stuff from that link you posted, but I had a user today send me an email, containing error code saying there is a permissions problem accessing the INFORMATION_SCHEMA table.


Microsoft OLE DB Provider for ODBC Drivers error '80040e09'

[Microsoft][ODBC SQL Server Driver][SQL Server]SELECT permission denied on object 'TABLES', database 'master', owner 'INFORMATION_SCHEMA'.

/blah/blah/kpoll_config.asp, line 49


The sql string being queried is


  strsql = "IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.tables WHERE table_name='KPOLL') "
  strsql = strsql & "SELECT 1 ELSE SELECT 0"
  set rs = pConn.execute(strsql)


I have a feeling its permissions in the database somehow, but hopefully I can find a work around. I fear the sysobjects table probably holds the same restrictions.

-Stim
Go to Top of Page
Page: of 2 Previous Topic Topic Next Topic  
Next Page
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.4 seconds. Powered By: Snitz Forums 2000 Version 3.4.07