473,809 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11673
Something like this:

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

"bo*@datasync.c om" 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.c om" 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.c om> wrote in message
news:11******** **************@ y41g2000cwy.goo glegroups.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.c om" 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_SCH EMA.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 SqlExecuteNonQu ery 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_SCH EMA.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.co m wrote:
Hi.

As I understand your example, this would have to be placed into an
sqlcommand and executed with the SqlExecuteNonQu ery 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.co m wrote:
Hi.

As I understand your example, this would have to be placed into an
sqlcommand and executed with the SqlExecuteNonQu ery 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_SCH EMA.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
11863
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 error message and stop the process every time someone loads the script after the table is created because that would mean the page could only ever run once which of course not the solution I was looking for. I simply want to know how I can check...
0
1673
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 it, delete from it. But I cannot see it. The only way I know it exists is by running this SQL from Access 97 pass through query: SELECT ALL new_tbl.name FROM new_tbl The database resides on a MySQL server that was created with a single table...
1
6194
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 -u "root" project < dbase.sql ERROR 1050 at line 19: Table 'columns_priv' already exists Vasundhar/
3
5913
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 and then delete it? So how can I check the existence of a table? ________________________________________ Function Importieren1() On Error GoTo Importieren1_Err
2
13935
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 ( weekstart datetime not null primary key, weekend datetime not null )
6
3625
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 added, I would like the selected fields to be copied to another table (call it Master). 2. If that Master table already contains a record where the CustName = CustName, then simply ignore. If CustName does not match anything, then add the record to...
2
6940
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 true when the table already exists prior to the run of this module. If the table does not exist, this line bombs varTemp = dbsTemp.TableDefs(prmTable).Name with this error:
1
10199
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 1050: Table 'registration' already exists" Then after the table is created I re-run the query/webform and I don't receive the error message, but it doesn't update my table? When I go to mySQL it shows the table 'registration' but has no updated...
1
9969
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
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10640
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10387
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7662
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.