游标问题: 游标 游标 咋写啊

ghostmdx1 2008-09-25 09:51:29
有两个表,A表和B表(B表存的是上次操作A表后的数据),当我登录之后,我可以对A表进行添加删除修改,当我修改完后。要将修改完后的A表每一条记录与B表每一条记录比较,如果比较后取得了返回值,则将修改完后的A表cpoy到B表中。
表中的字段有
USER_ID char
PASSWORD varchar
S_COD char
S_NAM varchar
.
.
.
TIME_STAMP_DT datetime
请各位大虾帮下忙,小弟刚学游标这个地方,谢谢!!!
...全文
76 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hyde100 2008-09-25
  • 打赏
  • 举报
回复

--游标
if object_id('tb') is not null
drop table tb
go
create table tb(name varchar(10),con int)
insert into tb select 'a',1
insert into tb select 'b',2
insert into tb select 'c',3

declare cursor_tb cursor for
select name ,con FROM tb
declare @name varchar(10),@con int
open cursor_tb--打开游标
fetch next from cursor_tb
into @name, @con
while @@FETCH_STATUS = 0 --取值正常的场合
begin

--sql

fetch next from cursor_tb
into @name, @con
end

close cursor_tb--关闭游标
deallocate cursor_tb--释放游标
ghostmdx1 2008-09-25
  • 打赏
  • 举报
回复
方法很好,但是必须要是游标哦~~~~[Quote=引用 5 楼 wufeng4552 的回复:]
SQL code--非游标方法
create table ta(userid int ,username varchar(10))
create table tb(userid int ,username varchar(10))
insert ta select 1,'A'
insert ta select 2,'B'
insert ta select 3,'C'
insert tb select 2,'D'
insert tb select * from ta a where not exists(select 1 from tb b where a.userid=b.userid)
select * from tb
drop table ta
drop table tb

userid username
----------- ---…
[/Quote]
水族杰纶 2008-09-25
  • 打赏
  • 举报
回复
--非游标方法
create table ta(userid int ,username varchar(10))
create table tb(userid int ,username varchar(10))
insert ta select 1,'A'
insert ta select 2,'B'
insert ta select 3,'C'
insert tb select 2,'D'
insert tb select * from ta a where not exists(select 1 from tb b where a.userid=b.userid)
select * from tb
drop table ta
drop table tb

userid username
----------- ----------
2 D
1 A
3 C

(3 行受影响)
yygyogfny 2008-09-25
  • 打赏
  • 举报
回复
1楼正解
hsie168518 2008-09-25
  • 打赏
  • 举报
回复

查看帮助,游标/打开
pl_mm 2008-09-25
  • 打赏
  • 举报
回复
--游标
if object_id('tb') is not null
drop table tb
go
create table tb(name varchar(10),con int)
insert into tb select 'a',1
insert into tb select 'b',2
insert into tb select 'c',3

declare cursor_tb cursor for
select name ,con FROM tb
declare @name varchar(10),@con int
open cursor_tb
fetch next from cursor_tb
into @name, @con
while @@FETCH_STATUS = 0
begin
select @name,@con--这里操作
fetch next from cursor_tb
into @name, @con
end
close cursor_tb
deallocate cursor_tb

34,591

社区成员

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

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