index > SQL Server Everywhere Edition > How to check for table existence before dropping it?

How to check for table existence before dropping it?

Apologies if this has been answered before, but the "Search" function doesn't seem to be working on these forums the last couple of days.

I'd just like to check if a table already exists before dropping it so that I can avoid an error if it doesn't exist. Doing a web search, I've tried along the lines of
"If (object_id(sensor_stream) is not null) drop table sensor_stream"
and
"If exists (select * from sensor_stream) drop table sensor_stream"

In both of these cases I get the error: "There was an error parsing the query. [ Token line number = 1,Token line offset = 1,Token in error = if ]"

Sooooo... what is the standard way to check for existence of a table before dropping it? Someone help? This seems like it should be simple, but I can't figure it out.

Thanks in advance!
Dana

dana_lotus

You can list all the database tables by issuing a simple SQL command:

SELECT * FROM INFORMATION_SCHEMA.TABLES

You can then loop through the resulting rows and search for the given table names or just add a WHERE condition.




João Paulo Figueira (Device Application Development MVP)
João Paulo Figueira

Hi@all

I've also one question to this problem I've to get solved very urgent.

Could you please post the whole codesample for the problem posted by dana?

I am too new to vb.net, sorry

I want to check for an existing tbl called tbl_inventory for my application. This should be dropped if it exists and then it should be recreated.

Heres my Codesample, that doesn´t work, and I don´t know why. On the Windowsapplication I tested the same codesample and it worked perfectly.

Please help me

createtable = "IF EXISTS (SELECT * FROM pocket_invis2.sdf WHERE object_id=object_id(N'[tbl_inventory]') " & _

" AND TYPE IN (N'U')) DROP TABLE [tbl_inventory]" & _

" INSERT INTO tbl_inventory (barcodeID, element, new_barcode, new_element) " & _

" SELECT computerID, 1 AS computername, NULL, NULL FROM tbl_computer " & _

" UNION " & _

" SELECT deviceID, 0 AS devicenameID, NULL, NULL FROM tbl_device "

Hannes Alp.
What I did ended up looking like this (in C#):

SQL = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME=MyTable";
dana_lotus

Hi dana

Thanks for you reply

After long testing, I found a way too. It seems that, the if exists, doesn't exist on the mobile sql.

I made it with a datareader to loop through the tablenames and then drop or create the table.

best regards

Hannes

Hannes Alp.

Why you guys are going for such complex logic. Call DROP TABLE and handle TABLE DOES NOT EXIST error.

Thanks,

Laxmi Narsimha Rao ORUGANTI, MSFT, SQL Server Everywhere, Microsoft Corp.




Please hit "Yes", if my post answered your question(s). All postings are as-is and confer no rights.
Laxmi Narsimha Rao ORUGANTI MSFT
reply 6

You can use google to search for other answers

 

More Articles

• SQL Everywhere Remote
• Merge replication and SSL
• Debuggin on PDA
• SQL Server 2005 Replication and views
• Accessing Mobile SQL Databases in VB2005 - Windows Apps
• Add connction Fails in VS.NET with SQL Server Everywhere
• [Ask]Getting files from pocket pc emulator to desktop pc
• SQL Server Mobile 's cryptographic algorithm?
• SqlServer Ev vs. MSDE Performance
• ANYONE CAN HELP ME PLEASE?
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• How do I install SQL Mobile 2005/Everywh
• IID_IDBInitialize interface of sql mobil
• Using Select In Push Command
• NEED HELP - Merge Replication with SQL S
• Synchronization Error
• connecting to sql server from mobile dev
• can pocpet pc direct connect to sql serv
• Sync of binary columns in SQL 2005 EV an
• sql mobile alter statement
• Can it be a bug in SQL CE?
• SDF Creation on Desktop
• Relations Error -
• Import data to SQL Server Mobile
• Connection to database takes up alot of
• insert, delete, update data in database

Hot Articles

• cannot generate ResultSet code
• SQL Mobile in pocketpc 2003 se desktop c
• Problem using SQL Server Mobile 2005 wit
• VS 2005 Invalid database version SQL CE
• Loading data in SQL Mobile 2005 from the
• Like in query
• Synchronizing Data Within SQLCE and SQL
• Wrong version of SQL Server Mobile deplo
• File size limit / offline cache
• Loosing database connection when device
• Am i going the right way about this
• I am not sure whether I can use sql mobi
• SQL 2005 Mobile vs .NET CF 2.0 SP1?
• How to create a SQL ev DB?
• SQL Server CE 3 Constraint Problem

Recommend Articles

• using sql Mobile Databases
• Cannot build SQL Server Mobile solution
• problem on running replication on pocket
• Converting bytes [] to an SQL CE 3 image
• Obtain sqlceresultset table schema with
• MDB to SDF
• How do I drop sql server mobile database
• Triggers
• Disarming IDENTITY generation
• System.AccessViolationException in Syste
• Problem on Sync Access database
• RDA SQL MOBILE ERROR 28037 HResult = -2
• Insert GUID into SQL Mobile Database tab
• Setting up publication for merge replica
• Using RDA to insert an image type from S