--建立测试环境
create table temp (id int identity(1,1),A varchar(10) default '')
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
create table #temp (id int)
--删除几条,产生不连续
delete from temp where id=6 or id=8 or id=13
--用游标
declare @p_id int,@e_id int
select @p_id=0,@e_id=0
declare id_cursor CURSOR
for
select id from tempopen id_cursor
fetch next from id_cursor into @p_id
while @@fetch_status=0
begin
fetch next from id_cursor into @e_id
if @e_id-@p_id>1 --判断连续
begin
insert into #temp values(@p_id)
end
set @p_id=@e_id
end
close id_cursor
deallocate id_cursor
--列结果
select id as '不连续开始ID号' from #tempdrop table #temp,temp
create table temp (id int identity(1,1),A varchar(10) default '')
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
create table #temp (id int)
--删除几条,产生不连续
delete from temp where id=6 or id=8 or id=13
--用游标
declare @p_id int,@e_id int
select @p_id=0,@e_id=0
declare id_cursor CURSOR
for
select id from tempopen id_cursor
fetch next from id_cursor into @p_id
while @@fetch_status=0
begin
fetch next from id_cursor into @e_id
if @e_id-@p_id>1 --判断连续
begin
insert into #temp values(@p_id)
end
set @p_id=@e_id
end
close id_cursor
deallocate id_cursor
--列结果
select id as '不连续开始ID号' from #tempdrop table #temp,temp
解决方案 »
- Uinon两个表后,如何查询出count数
- 关于E-R图的一个单选题
- 触发器的问题请教大家!
- 为什么SQL2008安装完成后没有客户端
- MAX的补充条件!!
- 求助:数据库还原中遇到的问题 ,提示空间不够 ,但我的空间是足够的.
- 如何用SQL语句简单实现 一条记录的复制?
- 关于复制问题?
- 请解释一句SQL?
- 请教大师,SQL2008R2配置好邮件后,如果设置数据库自动备份计划里的数据自动定时的发送到指定邮箱
- 可以给存贮过程传递这样一个参数吗? or src_id=+ ''' +cast('0189828' as nvarchar(30))+'''
- +++++++++这样的C/S多用户数据库系统能做得出来吗?++++++++
select pro_sec, pro_id, pro_name from product a
where not exists(select * from product where pro_sec=a.pro_sec-1)--第一个记录如果算是连续的记录
select pro_sec, pro_id, pro_name from product a
where not exists(select * from product where pro_sec=a.pro_sec-1)
and a.proc_sec<>(select isnull(min(pro_sec),0) from product)
有些开发工具的update可以设置为:先delete加再insert的等效操作。
这种情况下没有使用数据库的update!
if object_id('t1') is not null
drop table t1
go
create table t1(id int identity(1,1),a int)
go
create trigger tr_t1 on t1
for insert
as
if exists(select 1 from inserted where a = 10)
rollbackgoinsert t1 values(1)
insert t1 values(3)
go
insert t1 values(10) -- 此行会rollback
go
insert t1 values(15)
go
select * from t1
/*显示结果为:
id a
----------- -----------
1 1
2 3
4 15(所影响的行数为 3 行)
*/