sqlserver常用操作——判断关键字段是否重复插入记录
2011-03-30 11:10
411 查看
sqlserver常用操作(2)——判断关键字段是否重复插入记录
插入数据,判断这条数据是否重复,游标中采用的思想是逐条循环判断是否添加。方法一:
insert into
dbo.News (NewsTitle,Typeid,TypeName,[content])
select NewsTitle,Typeid,TypeName,[content]
from dbo.News2 b
where b.NewsTitle not in(select NewsTitle from dbo.News)
方法二:
insert into
dbo.News (NewsTitle,Typeid,TypeName,[content])
select NewsTitle,Typeid,TypeName,[content]
from dbo.News2 b
where not exists
(select * from dbo.news where NewsTitle=b.NewsTitle)
游标操作示例一:
declare @cur_pos int
declare mycursor cursor for select NewsTitle,Typeid from dbo.News
declare @col1 char(10)
declare @col2 char(10)
open mycursor
fetch next from mycursor into @col1,@col2
while @@fetch_status<>-1
begin
print @col1+@col2
--select @cur_pos=@cur_pos+1
fetch next from mycursor into @col1,@col2
end
close mycursor
deallocate mycursor
游标操作示例二:
declare @cur_pos int
declare @newsid int
declare @newstitle varchar(20)
declare mycursor cursor for select Newsid,NewsTitle from dbo.News2
open mycursor
fetch next from mycursor into @newsid,@newstitle
while @@fetch_status<>-1
begin
if not exists
(select * from dbo.News where NewsTitle=@newstitle)
begin
insert into dbo.News(NewsTitle,Typeid,TypeName,[content])
select NewsTitle,Typeid,TypeName,[content]
from dbo.News2 where NewsId=@newsid
end
fetch next from mycursor into @newsid,@newstitle
end
close mycursor
deallocate mycursor
转自:http://www.thaught.cn/go.php/category/16/1/2/
相关文章推荐
- SqlServer和Mysql插入记录前判断是否存在,存在则插入,不存在则修改。
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
- Sql 查询当天、本周、本月记录和日期操作、判断表和字段是否存在
- Oracle: DELETE前不需SELECT判断记录是否存在,INSERT前不需SELECT判断是否有若干字段值重复的记录。
- SqlServer判断数据库、表、字段、存储过程、函数是否存在
- 删除表中多余的重复记录,重复记录是根据单个字段(Id)来判断,只留有id最小的记录
- C#.NET对注册表的常用操作(增加项,判断指定项是否存在,增加键(键的名称,键的数值),判断指定的键是否存在)
- ACCESS数据库操作:重复记录仅显示其中一个,并显示多个字段
- 并发插入insert,根据状态判断重复记录的问题
- oracle sql 判断字段非空,数据不重复,插入多跳数据
- FlowPortal-BPM——功能:判断数据库表中字段是否重复并阻止提交或保存
- RecyclerView的一些操作(记录一下)判断是否到达顶部和尾部
- sqlserver查询某一字段重复超5次的所有记录
- 常用判断重复记录的SQL语句
- 在数据库表中插入字段,插入之前会判断该字段是否存在
- 如何判断Oracle中某个字段的值是否重复
- 常用数据库查询判断表和字段是否存在
- php判断数据库中某个字段是否有值去执行excel表格写入操作
- 如果把记录从数据库里面删除,判断是否重复?
- Oracle 删除重复数据只留一条 查询及删除重复记录的SQL语句 1、查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断 select * from 表 wher