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. 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.
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.
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. > >
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
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 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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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/
|
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
|
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
)
| |
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...
|
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:
|
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...
|
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
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |