判断SQL数据库是否存在表,是否存在记录
2014-04-04 15:39
239 查看
SQL数据库,当判断一条记录存在时,则更新更记录,当记录不存在时,则添加该记录
使用SQL语句在C#中实现,sql语句
if exists
(select * from 表 where 条件)
begin
Update 表 Set 字段=字段值 where 条件
end
else
begin
Insert into dbo.表名(字段) values
(字段值)
end
比如:
if exists
(select * from CollectRain_201404 where StationId='D6021' and CollectTime='20140406232000')
begin
Update CollectRain_201404 Set CollectRainFall='22222',IsValue='false' where StationId='D6021' and CollectTime='20140406232000'
end
else
begin
Insert into dbo.CollectRain_201404 (StationId,CollectTime,CollectRainFall,IsValue) values
('D6021','20140406232000','11111','true')
end
当判断一个表是否存在数据库时,当数据库不存在该表时,则需要新建该表
使用SQL在C# NET中实现方法
if exists
(select * from sysobjects where id = object_id(N'[表名]')
and OBJECTPROPERTY(id, N'IsUserTable') = 1)
print 'f'
else
begin
CREATE TABLE 表名
(
建表
)
end
比如
if exists
(select * from sysobjects where id = object_id(N'[Test]')
and OBJECTPROPERTY(id, N'IsUserTable') = 1)
print 'f'
else
begin
CREATE TABLE Test
(
Id int IDENTITY(1,1) PRIMARY KEY,
StationId nvarchar(MAX) NOT NULL,
CollectTime nvarchar(MAX) not null,
CollectRainFall float not null,
IsValue bit not null
)
end
使用SQL语句在C#中实现,sql语句
if exists
(select * from 表 where 条件)
begin
Update 表 Set 字段=字段值 where 条件
end
else
begin
Insert into dbo.表名(字段) values
(字段值)
end
比如:
if exists
(select * from CollectRain_201404 where StationId='D6021' and CollectTime='20140406232000')
begin
Update CollectRain_201404 Set CollectRainFall='22222',IsValue='false' where StationId='D6021' and CollectTime='20140406232000'
end
else
begin
Insert into dbo.CollectRain_201404 (StationId,CollectTime,CollectRainFall,IsValue) values
('D6021','20140406232000','11111','true')
end
当判断一个表是否存在数据库时,当数据库不存在该表时,则需要新建该表
使用SQL在C# NET中实现方法
if exists
(select * from sysobjects where id = object_id(N'[表名]')
and OBJECTPROPERTY(id, N'IsUserTable') = 1)
print 'f'
else
begin
CREATE TABLE 表名
(
建表
)
end
比如
if exists
(select * from sysobjects where id = object_id(N'[Test]')
and OBJECTPROPERTY(id, N'IsUserTable') = 1)
print 'f'
else
begin
CREATE TABLE Test
(
Id int IDENTITY(1,1) PRIMARY KEY,
StationId nvarchar(MAX) NOT NULL,
CollectTime nvarchar(MAX) not null,
CollectRainFall float not null,
IsValue bit not null
)
end
相关文章推荐
- mongoDB杂记
- oracle 数据库无法自动创建snapshot
- LINQ TO SQL 常用语法
- MySQL 模拟条件索引
- mysql 存储过程
- MySQL添加字段和修改字段的方法
- 基于C#分步式聊天系统的在线视频直播系统 之 数据库操作基类 - 通用于Windows及Linux的Mono环境
- Sql注入漏洞及绑定变量浅谈
- 部署 PHP 系列,第 3 部分: 加速用于 Oracle 的 PHP 代码运行速度
- ios--django 数据库的链接数据的返回(简单处理)
- SQL判断一个字段是否是数字的办法
- oracle存储过程学习(二)-调用存储过程和返回结果集
- oracle10g在windows中的5个服务的作用以及启动
- mongodb三节点同时创建副本集出现的bug
- mysql备份与恢复
- mysql忘记root密码
- 防止sql注入的参数化查询
- 关于sqlmap的异常退出
- mysql的数据类型
- How to enable remote connections in SQL Server 2008?