delcare @id int
delcare c1 cursor for
select id from Tab_Customeropen c1fetch next from c1 into @idwhile @@Fetch_Status=0
begin
-- 处理过程
fetch next from c1 into @id
end
close c1
deallocate c1
delcare c1 cursor for
select id from Tab_Customeropen c1fetch next from c1 into @idwhile @@Fetch_Status=0
begin
-- 处理过程
fetch next from c1 into @id
end
close c1
deallocate c1
但是我怎么才能获取我想要的结果集哪??? 我想过滤掉一些数据
就像我用select语句在客户端获取的数据哪样的集合哪???
delcare c1 cursor for
select id from Tab_Customer WHERE .......--就是这里啊,跟平常一样的SQL语句
但是我想通过c1对id结果集进行一下过滤,比如我只想得到偶数id的结果集 请问我要怎么写这个语句啊???
那你之前在select id from Tab_Customer WHERE ID%2=0 (偶数判断下)这样不就行了?
这样不就是偶数的ID了?
我把它放到@id中了 但是我要在
while @@Fetch_Status=0
begin
-- 处理过程 这里就是你说的判断 @id%2=0
fetch next from c1 into @id
end中进行处理 如果我用 select @id的话 他查询出来的是1个1个的结果 不是我想要的那种完全的结果集 请问怎么处理??
你可以定义一个表变量,把符合的id插入表变量中。如:declare @t table(id int) --定义表变量
delcare @id int
delcare c1 cursor for select id from Tab_Customer
open c1
fetch next from c1 into @id
while @@Fetch_Status=0
begin
-- 处理过程 这里就是你说的判断 @id%2=0
if(@id%2=0)
insert into @t select @id --将符合要求的id,插入表变量
fetch next from c1 into @id
end
close c1
deallocate c1
select * from @t --查询所有符合条件的id.
这两行:
delcare @id int
delcare c1 cursor for select id from Tab_Customer上面的delcare 统一改为declare