473,386 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How do you see if a table already exists in a database?

bob
I am writing some code to create new tables in a SQL database. However,
I don't want to try to create a table if it already exists. How can I
test beforehand to see if a particular named table already exists in
the database, without actually trying to read records into a dataset?

Thanks.

Jun 13 '06 #1
8 11618
Something like this:

IF NOT EXISTS (
SELECT *
FROM MyDatabase.dbo.sysobjects
WHERE Name='MyTable'
AND TYPE='u')
BEGIN
CREATE TABLE ...
END

"bo*@datasync.com" wrote:
I am writing some code to create new tables in a SQL database. However,
I don't want to try to create a table if it already exists. How can I
test beforehand to see if a particular named table already exists in
the database, without actually trying to read records into a dataset?

Thanks.

Jun 14 '06 #2
bob
Well, I was hoping that the SqlConnection object would have some
collection like "Tables" or something. Is there really no such
collection?
tlkerns wrote:
Something like this:

IF NOT EXISTS (
SELECT *
FROM MyDatabase.dbo.sysobjects
WHERE Name='MyTable'
AND TYPE='u')
BEGIN
CREATE TABLE ...
END

"bo*@datasync.com" wrote:
I am writing some code to create new tables in a SQL database. However,
I don't want to try to create a table if it already exists. How can I
test beforehand to see if a particular named table already exists in
the database, without actually trying to read records into a dataset?

Thanks.


Jun 14 '06 #3
CT
The only Tables collection exists in the DataSet object and it is only
filled on request using a DataAdapter, which wouldn't suit your requirement
I'm sure. tlkerns suggestion is a good one with SQL Server.

--
Carsten Thomsen
Communities - http://community.integratedsolutions.dk
---------
Voodoo Programming: Things programmers do that they know shouldn't work but
they try anyway, and which sometimes actually work, such as recompiling
everything. (Karl Lehenbauer)
---------
<bo*@datasync.com> wrote in message
news:11**********************@y41g2000cwy.googlegr oups.com...
Well, I was hoping that the SqlConnection object would have some
collection like "Tables" or something. Is there really no such
collection?
tlkerns wrote:
Something like this:

IF NOT EXISTS (
SELECT *
FROM MyDatabase.dbo.sysobjects
WHERE Name='MyTable'
AND TYPE='u')
BEGIN
CREATE TABLE ...
END

"bo*@datasync.com" wrote:
> I am writing some code to create new tables in a SQL database. However,
> I don't want to try to create a table if it already exists. How can I
> test beforehand to see if a particular named table already exists in
> the database, without actually trying to read records into a dataset?
>
> Thanks.
>
>

Jun 14 '06 #4
tlkerns wrote:
Something like this:


A much better way to implement this IMO is:

\\\
if not exists(
select *
from INFORMATION_SCHEMA.tables
where TABLE_NAME = 'MyTable')
begin
create table ...
end
///

This is using a well-documented ANSI/ISO standard view to retrieve data on
the schema rather than hacking around in system tables and using "magic"
values. Works in SQL Server 2000 and later (and in many non-SQL Server DBMSs
too).

I continue to be very surprised to see how many people turn to system tables
to retrieve schema data when these views have been around for years and
provide all the same data in a much more easily obtainable and future-proof
fashion.

--

(O)enone
Jun 14 '06 #5
bob
Hi.

As I understand your example, this would have to be placed into an
sqlcommand and executed with the SqlExecuteNonQuery method. But it
would then CREATE the table if it didn't exist. Whereas, I only want to
find out if the Table exists, I don't want to necessarily create it.

Can you execute an SQL command and get it to return True or False,
depending on whether the table exists? How do you handle the "result"
of such a query?

Thanks.
Oenone wrote:
tlkerns wrote:
Something like this:


A much better way to implement this IMO is:

\\\
if not exists(
select *
from INFORMATION_SCHEMA.tables
where TABLE_NAME = 'MyTable')
begin
create table ...
end
///

This is using a well-documented ANSI/ISO standard view to retrieve data on
the schema rather than hacking around in system tables and using "magic"
values. Works in SQL Server 2000 and later (and in many non-SQL Server DBMSs
too).

I continue to be very surprised to see how many people turn to system tables
to retrieve schema data when these views have been around for years and
provide all the same data in a much more easily obtainable and future-proof
fashion.

--

(O)enone


Jun 16 '06 #6
bo*@datasync.com wrote:
Hi.

As I understand your example, this would have to be placed into an
sqlcommand and executed with the SqlExecuteNonQuery method. But it
would then CREATE the table if it didn't exist. Whereas, I only want to
find out if the Table exists, I don't want to necessarily create it.

Can you execute an SQL command and get it to return True or False,
depending on whether the table exists? How do you handle the "result"
of such a query?

Thanks.


If you just execute this:
SELECT *
FROM MyDatabase.dbo.sysobjects
WHERE Name='MyTable'
AND TYPE='u'

It will give you a resultset. You can check the recordcount and see if
it's > 0. if >0 then the table exists.
--
Rinze van Huizen
C-Services Holland b.v
Jun 16 '06 #7
bob
THANKS!!

C-Services Holland b.v. wrote:
bo*@datasync.com wrote:
Hi.

As I understand your example, this would have to be placed into an
sqlcommand and executed with the SqlExecuteNonQuery method. But it
would then CREATE the table if it didn't exist. Whereas, I only want to
find out if the Table exists, I don't want to necessarily create it.

Can you execute an SQL command and get it to return True or False,
depending on whether the table exists? How do you handle the "result"
of such a query?

Thanks.


If you just execute this:
SELECT *
FROM MyDatabase.dbo.sysobjects
WHERE Name='MyTable'
AND TYPE='u'

It will give you a resultset. You can check the recordcount and see if
it's > 0. if >0 then the table exists.
--
Rinze van Huizen
C-Services Holland b.v


Jun 16 '06 #8
C-Services Holland b.v. wrote:
If you just execute this:
SELECT *
FROM MyDatabase.dbo.sysobjects
WHERE Name='MyTable'
AND TYPE='u'


....or, once again, you could go with the ANSI/ISO approach instead of
hacking around in system tables, and use the same approach with this SQL:

\\\
select *
from INFORMATION_SCHEMA.tables
where TABLE_NAME = 'MyTable'
///

--

(O)enone
Jun 17 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jonathan | last post by:
I am looking for a simple way to check if a database table exists. I keep getting advice to use "Try.. Catch" and other error handling methods, but I obviously don't want to have to display an...
0
by: MLH | last post by:
I used the following SQL to create a new table in a database on a remote MySQL server by copying one already there. I know the table exists SOMEWHERE in cyberspace. I can read its data, write to...
1
by: Neo | last post by:
Hi, I have dumped a database into the file named dbase.sql when I am trying to recreate the database its giving the following error. can any one help me in this regard? # vim dbase.sql # mysql...
3
by: Michael Magg | last post by:
Hello! I wanna import a table with a makro, but a table with this name already exists. So the imported table (f. i. "table") is saved as "table1". How can I check, if such a table already exists...
2
by: Alicia | last post by:
Does anyone know why I am getting a "Syntax error in Create Table statement". I am using Microsoft Access SQL View to enter it. Any other problems I may run into? CREATE TABLE weeks (...
6
by: Jchick | last post by:
Im a newbie with a sql table in a MSDE database that contains fields of CustName, BrokerName, Type, Status. I am trying to write a trigger that does this: 1. When a new record in a table is...
2
by: RLN | last post by:
Re: Access 2003 I have code to check to see if a table exists. There are several other tables (tblQ12, tblQ13, etc) that do already exist & this code runs fine; msgbox says the value returned is...
1
by: delusion7 | last post by:
Trying to create a table and insert records from a webform and I keep getting this message: "Successfully created the registration table. Unable to execute the query. Error code...
1
by: dwasler | last post by:
Try every thing I know to remove this alias I know there been other posting I read each one none seem to work. Thank You DLWasler dwasler@yahoo.com OS Window db2 V 8.2.X
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.