给定一个表名,如何判断数据库中该表是否存在??
最好完全用SQL语句实现:
declare @Qty int
SELECT Count(*) AS Qty FROM dbo.SysObjects
WHERE (((dbo.Name) Like "MyTempTable "));
if Qty> 0
DROP TABLE MyTempTable
CREATE TABLE MyTempTable
{
id int NOT NULL,
company_name varchar(100) NULL,
setup_date datetime NULL,
use_times int NULL,
order_numers int NULL,
first_use_date datetime NULL,
first_order_date datetime NULL,
latest_order_date datetime NULL,
signup_date datetime NULL,
sum_cust_money datetime NULL
}
我这么写,在查询分析器里面报错,情大侠指正,感激不尽
------解决方案--------------------if EXISTS(select 1 from sysobjects where type= 'U ' and name= 'MyTempTable ')
DROP TABLE MyTempTable
------解决方案--------------------declare @Qty int
SELECT Count(*) FROM dbo.SysObjects WHERE Name = 'A '
if (SELECT Count(*) FROM dbo.SysObjects WHERE Name = 'A ')> 0
DROP TABLE a
CREATE TABLE MyTempTable
(
id int NOT NULL,
company_name varchar(100) NULL,
setup_date datetime NULL,
use_times int NULL,
order_numers int NULL,
first_use_date datetime NULL,
first_order_date datetime NULL,
latest_order_date datetime NULL,
signup_date datetime NULL,
sum_cust_money datetime NULL
)
------解决方案--------------------if objectproperty(object_id( '表名 '), 'IsUserTable ') is null
print '不存在 '
else
print '存在 '
------解决方案--------------------用object_id,返回NULL表示不存在
例如,
SELECT object_id( 'xx '),
object_id( 'syscolumns ')