DROP TABLE IF EXISTS 发生错误,麻烦各位大侠给看看

ccccc_7 2010-04-24 04:35:26
各位高手:
我写了这样一段代码:
private static bool DeleteTable(SqlCeConnection cn, string TableName)
{
string strSQL = @"DROP TABLE IF EXISTS " + TableName ;
try
{
if (cn.State != ConnectionState.Open)
{
cn.Open();
}
SqlCeCommand SqlCeCmd = new SqlCeCommand(strSQL, cn);

SqlCeCmd.ExecuteNonQuery();
return true;
}
catch (SqlCeException ex)
{

MessageBox.Show( ex.Message);
return false;
}
}
报错:Token line number=1 ,token line offset=12,Token in error=IF
调用语句是DeleteTable(cn, "xzqy")
我跟进去了,看到连接都没问题,字符串是DROP TABLE IF EXISTS xx(我的表名),但是还是抛出异常

希望各位大侠赐教,不生感激阿!!
...全文
13558 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
basfgfcu 2011-08-27
  • 打赏
  • 举报
回复
if exists(select object_id('tt'))  
drop table tt
gooworld 2010-09-28
  • 打赏
  • 举报
回复
Oracle MySQL 唉..........
[Quote=引用 9 楼 happycell188 的回复:]

SQL code
drop table if exists address_book;这句SQL语句是什么意思
悬赏分:10 - 解决时间:2009-7-17 09:36
我备份的SQL语句中有这么一行,不知道是什么意思?

提问者: QQ41678438 - 二级最佳答案如果数据库中存在address_book表,就把它从数据库中drop掉。
备份sql中一般都有这样的语句,如果是数据……
[/Quote]
ccccc_7 2010-04-26
  • 打赏
  • 举报
回复
谢谢各位!我终于明白了呵呵,因为太菜,开始没明白里面的select是系统表。非常感谢大家!谢谢
永生天地 2010-04-24
  • 打赏
  • 举报
回复
if exists(select object_id('tt'))
drop table tt
htl258_Tony 2010-04-24
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 ccccc_7 的回复:]
引用 11 楼 htl258 的回复:
引用 6 楼 ccccc_7 的回复:
[Quote=引用 5 楼 htl258 的回复:]
首先谢过!
我想问一下,可是这个表如果是空的呢?也或者没有,select不久不成功了么?
不用担心,为空表示IF判断条件就为否,下面就不执行


我是不想让这个表存在啊。因为下面用到PULL,如果表存在它就失败了
[/Quote]也就是IF EXISTS。。。如果条件为真,那个表就会被DROP掉,条件为否,就不会执行DROP的动作。
ccccc_7 2010-04-24
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 htl258 的回复:]
引用 6 楼 ccccc_7 的回复:
[Quote=引用 5 楼 htl258 的回复:]
首先谢过!
我想问一下,可是这个表如果是空的呢?也或者没有,select不久不成功了么?
不用担心,为空表示IF判断条件就为否,下面就不执行
[/Quote]

我是不想让这个表存在啊。因为下面用到PULL,如果表存在它就失败了
htl258_Tony 2010-04-24
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ccccc_7 的回复:]
[Quote=引用 5 楼 htl258 的回复:]
首先谢过!
我想问一下,可是这个表如果是空的呢?也或者没有,select不久不成功了么?
[/Quote]不用担心,为空表示IF判断条件就为否,下面就不执行
喜-喜 2010-04-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 fredrickhu 的回复:]
引用 6 楼 ccccc_7 的回复:
[Quote=引用 5 楼 htl258 的回复:]
首先谢过!
我想问一下,可是这个表如果是空的呢?也或者没有,select不久不成功了么?


就是判断表是否为空 为空的话还DROP什么?
[/Quote]

就是嘛!不是有个 exists 么!!
喜-喜 2010-04-24
  • 打赏
  • 举报
回复
drop table if exists address_book;这句SQL语句是什么意思
悬赏分:10 - 解决时间:2009-7-17 09:36
我备份的SQL语句中有这么一行,不知道是什么意思?

提问者: QQ41678438 - 二级最佳答案如果数据库中存在address_book表,就把它从数据库中drop掉。
备份sql中一般都有这样的语句,如果是数据库中有这个表,先drop掉,然后create表,然后再进行数据插入。

'网上竟然也有“drop table if exists **”这样的写法,不知道是什么数据库中的语句??'
--小F-- 2010-04-24
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 ccccc_7 的回复:]
[Quote=引用 5 楼 htl258 的回复:]
首先谢过!
我想问一下,可是这个表如果是空的呢?也或者没有,select不久不成功了么?
[/Quote]

就是判断表是否为空 为空的话还DROP什么?
--小F-- 2010-04-24
  • 打赏
  • 举报
回复
if object_id('dbname..tbname') is not null
drop table tbname
go
ccccc_7 2010-04-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 htl258 的回复:]
首先谢过!
我想问一下,可是这个表如果是空的呢?也或者没有,select不久不成功了么?
htl258_Tony 2010-04-24
  • 打赏
  • 举报
回复
if exists(select 1 from sysobjects where name='tbname' and xtype='u')
drop table tbname
go
或这样
dawugui 2010-04-24
  • 打赏
  • 举报
回复
if object_id('dbname..tbname') is not null
drop table tbname
go
htl258_Tony 2010-04-24
  • 打赏
  • 举报
回复
if object_id('tbname','u') is not null
drop table tbname
go
dawugui 2010-04-24
  • 打赏
  • 举报
回复
if object_id('tbname') is not null
drop table tbname
go
dawugui 2010-04-24
  • 打赏
  • 举报
回复
IF EXISTS xx
DROP TABLE

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧