存储过程

zhang23yan 2003-11-18 08:06:43
有一个表,里面有个字段是可以比较的,我想写个存储过程,在一个新表中第一插入这个表中的第一个记录,然后拿这个表中的第2个记录和已经插入新表的第一个记录比那个字段,如果大于则修改新表中的第一个记录中的那个可比较的字段,反之则在新表中插入第2个记录,重复将表的第三的记录与新表中的最后一个记录进行比较,最后得到一个新表,谢谢
...全文
44 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
伍子V5 2003-11-18
  • 打赏
  • 举报
回复
create procedure pd_name
as
declare @colname int
declare @new_colname int
declare my_cursor cursor for select colname from 旧表
open my_cursor
fetch next from my_cursor into @colname
select top 1 id=identity(int,1,1),colname into 新表 from 旧表
while @@fetch_status=0
begin
fetch next from my_cursor into @colname
select top 1 @new_colname=colname from 新表 order by id desc
if(@new_colname>=@colname)
exec('insert into 新表(colname) values('+@colname+')'
else
exec('update 新表 set colname='+@new_colname+' where id=(select max(id) from 新表)'
end
close my_cursor
deallocate my_cursor
alter table 新表 drop column id
zjcxc 元老 2003-11-18
  • 打赏
  • 举报
回复
create proc p_test
as
declare @a varchar(100),@b varchar(100)
select top 0 * into 新表 from 旧表
set @b=''
declare tb cursor for select 可比较的字段 from 旧表
open tb
fetch next from tb into @a
while @@fetch_status=0
begin
if @a>@b
insert into 新表 select * from 旧表 where 可比较的字段=@a
else
update 新表 set 可比较的字段=@a where 可比较的字段=@b
set @b=@a
fetch next from tb into @a
end
close tb
deallocate tb
go
pengdali 2003-11-18
  • 打赏
  • 举报
回复
游标:

declare cursor_insert cursor for select c# from cs
declare @i int
open cursor_insert
fetch cursor_insert into @i
while @@fetch_status=0
begin
print @i
fetch cursor_insert into @i
end
close cursor_insert
deallocate cursor_insert

34,875

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧