SQL 判断表是否存在?
如果不存在就创建表
存在就不创建了,大家帮忙看看
解决马上给分!!!
如果不存在就创建表
存在就不创建了,大家帮忙看看
解决马上给分!!!
解决方案 »
- RequiredFieldValidator
- 求救!asp.net中,list怎样绑定数据库里的一个表。
- 在ASP.NET中实现类似浮动层的显示框
- 大家在cookie里面都写入什么来验证登陆或者授权
- Repeater控制内容行数
- (超急)跪求高手:为什么我取不到网页的源代码?
- 请问这种代码,是系统生成的,还是人为打进去的?
- 请问谁有<asp.net web 站点高级编程-提出问题-设计方案-解决方案>的源码 c#版
- 怎么获取当前页面的服务器地址和虚拟目录下
- 各位高手,我用VS 2002做开发,有一个数据库字段是VARCHAR型(最大8000),后来改为NTEXT型,结果VS提示,表达式中数据类型错误?怎么办?
- 请问验证身份证的正则表达式
- 请教关于用gridview控件显示文件目录时自动换行的问题
create table [Table1] ([col1] int,[col2] int)
if not exists (select * from sysobjects where [name] = '表名' and xtype='U')
begin
--这里创建表
end
create table [Table1] ([col1] int,[col2] int)
if not exists(select name from sysobjects where name=''+@name+'' and type='u')
begin
-- 创建
end
drop table [dbo].[result]
GOCREATE TABLE [dbo].[result] (
[rid] [int] IDENTITY (1, 1) NOT NULL ,
[rdate] [datetime] NOT NULL ,
[name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[software_e] [numeric](18, 1) NOT NULL ,
[rule] [numeric](18, 1) NOT NULL ,
[platform] [numeric](18, 1) NOT NULL ,
[recorder] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL
) ON [PRIMARY]
GO
请教 xtype 和type 有什么区别
对象类型。可以是下列值之一:
C = CHECK 约束
D = 默认值或 DEFAULT 约束
F = FOREIGN KEY 约束
FN = 标量函数
IF = 内嵌表函数
K = PRIMARY KEY 或 UNIQUE 约束
L = 日志
P = 存储过程
R = 规则
RF = 复制筛选存储过程
S = 系统表
TF = 表函数
TR = 触发器
U = 用户表
V = 视图
X = 扩展存储过程XType
对象类型。可以是下列对象类型中的一种:
C = CHECK 约束
D = 默认值或 DEFAULT 约束
F = FOREIGN KEY 约束
L = 日志
FN = 标量函数
IF = 内嵌表函数
P = 存储过程
PK = PRIMARY KEY 约束(类型是 K)
RF = 复制筛选存储过程
S = 系统表
TF = 表函数
TR = 触发器
U = 用户表
UQ = UNIQUE 约束(类型是 K)
V = 视图
X = 扩展存储过程貌似在这里用哪个都行……
if object_id('表名') is not null
drop table 表名
go
create table 表名(colname coltype,....)
begin
--这里创建表
end
麻烦各位再看看
drop procedure [dbo].[表名]
GO
有没有select出1来?
use 你要使用的数据库
/*使用数据库*/
use ComManageDB
go/*判断该表是否已经存在*/
if exists ( select * from sys.objects where name ='UserInfo')
drop table UserInfo
go/*创建UserInfo表*/
create table UserInfo
(
........
这样看看
或者:
Select 1 from sysobjects where id = object_id('yourTableName');
顺便说一下:SYBASE ANYWHERE可以用object_id函数.
但是不能用
if object_id(‘yourTableName’) is null
create table yourTableName
…
这样的方法来处理.但是在SQL Server中可以.
use ComManageDB
go/*判断该表是否已经存在*/
if exists ( select * from sys.objects where name ='UserInfo')
drop table UserInfo
go/*创建UserInfo表*/
create table UserInfo
(
........你这样写那不是如果UserInfo存在,就先删除这个表
然后再创建?
表的if( object_id('LZ的表') is not null )
drop table LZ的表
go
create table LZ的表
(
字段1 int ,
字段2 int
)
go
存储过程的if( object_id('LZ的存储过程') is not null )
drop procedure LZ的存储过程
go
create procedure LZ的存储过程
@参数1 int ,
@参数2 int
as
begin tran-- 过程而已commit
go
beginend
GO
if not exists(select name from sysobjects where name='+@DBTableName+' and type='u')