House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  ColdFusion Talk (CF-Talk)

checking if a table exists?

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
> From: Mark Drew
Philip Arnold
06/08/04 08:51 A
That is a good point Phil. It is better practice to use
Robertson-Ravo, Neil (RX)
06/08/04 09:24 A
techmike wrote:
Jochem van Dieten
06/08/04 10:31 A
> From: techmike
Philip Arnold
06/08/04 10:33 A
Philip Arnold wrote:
Jochem van Dieten
06/08/04 10:49 A
> From: Jochem van Dieten
Philip Arnold
06/08/04 11:00 A
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
techmike
06/08/2004 08:23 AM

Is it possible and if so how to check if a table exists? I'm trying to add a automatic setup to my template, user specifies the DSN, and the template will look to see if the tables exist, and if they don't then create them with some basic entries..   I've never created tables in CFM, but I assume CREATE TABLE will work? -mike

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Mark Drew
06/08/2004 08:33 AM

> Is it possible and if so how to check if a table exists? Depending on the database but in MSSQL is: if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[que_log]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[que_log] GO AND in MySQL it is; DROP TABLE IF EXISTS que_log; Hope that helps -- Mark Drew http://cybersonic.blogspot.com mailto:mark.drew@gmail.com

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Philip Arnold
06/08/2004 08:51 AM

----- Excess quoted text cut - see Original Post for more ----- Why not use INFORMATION_SCHEMA.TABLES rather than sysobjects If exists (select * from INFORMATION_SCHEMA.TABLES where table_name = 'YourTable') Much more readable

Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Robertson-Ravo, Neil (RX)
06/08/2004 08:32 AM

Well, in CF you would and could make a call to the table name, but place it in a cftry block and getting the result try SELECT * FROM YOURTABLE catch    your code to check if it exists /catch /try BUT  you are best to perform any checking in CF (assuming you are using SQL Server) DECLARE @vcUserTable varchar(500) SET @vcUserTable = 'mychosentablename' IF EXISTS (SELECT * FROM sysobjects WHERE type = 'U' AND name = @vcUserTable)      BEGIN           SELECT 'TRUE'           -- PRINT 'TRUE' -- < this will be ok      END ELSE      BEGIN           SELECT 'FALSE'           -- PRINT 'FALSE'      END GO Adding this into an SP will be a doddle and you can use it anywhere... HTH N    _____ Sent: 08 June 2004 13:21 To: CF-Talk Subject: checking if a table exists? Is it possible and if so how to check if a table exists? I'm trying to add a automatic setup to my template, user specifies the DSN, and the template will look to see if the tables exist, and if they don't then create them with some basic entries..   I've never created tables in CFM, but I assume CREATE TABLE will work? -mike    _____  

Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Pascal Peters
06/08/2004 08:46 AM

I would also go with your second option, but if the db doesn't allow it and you have to use try/catch don't do "select *". It can possibly return a lot of info. You could use select count(*). In your catch use type="Database" and try to find the correct errorcode for a non-existing table. Pascal ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Mark Drew
06/08/2004 08:54 AM

> It can possibly return a lot of info. If you are doing a select (with a try catch) you can do SELECT * FROM table WHERE 1=2 Hope that helps Mark Drew -- Mark Drew http://cybersonic.blogspot.com mailto:mark.drew@gmail.com

Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Robertson-Ravo, Neil (RX)
06/08/2004 09:24 AM

That is a good point Phil.  It is better practice to use INFORMATION_SCHEMA.TABLES as there is no guarantee that the sysobjects table code will always work.... ________________________________________ Sent: 08 June 2004 13:54 To: CF-Talk Subject: RE: checking if a table exists? ----- Excess quoted text cut - see Original Post for more ----- Why not use INFORMATION_SCHEMA.TABLES rather than sysobjects If exists (select * from INFORMATION_SCHEMA.TABLES where table_name = 'YourTable') Much more readable ________________________________________

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
techmike
06/08/2004 09:45 AM

Will that work in MySql as well as MSSQL?   -Mike ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jochem van Dieten
06/08/2004 10:31 AM

techmike wrote: > Will that work in MySql as well as MSSQL?   No, MySQL doesn't support schema's. It will work in PostgreSQL though, and I think you can download a SQL script to add information_schema support to Oracle. Jochem

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Philip Arnold
06/08/2004 10:33 AM

> From: techmike > > Will that work in MySql as well as MSSQL?   No - INFORMATION_SCHEMA is a MSSQL feature

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jochem van Dieten
06/08/2004 10:49 AM

Philip Arnold wrote: > From: techmike >> >> Will that work in MySql as well as MSSQL?   > > No - INFORMATION_SCHEMA is a MSSQL feature The INFORMATION_SCHEMA is not a MS SQL Server specific feature, it comes straight from the SQL standard. MySQL is just not very compliant. Jochem

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Philip Arnold
06/08/2004 11:00 AM

> From: Jochem van Dieten > > The INFORMATION_SCHEMA is not a MS SQL Server specific feature, > it comes straight from the SQL standard. MySQL is just not very > compliant. Wow, I didn't know this - I've not seen it in all SQL apps, so I thought it was MSSQL Strange that something MS has and others hasn't, yet it's a standard <g>


<< Previous Thread Today's Threads Next Thread >>

Search cf-talk

September 09, 2010

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
       1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30