问题描述
嗨..
我是游标概念的初学者.我搜索了许多链接,但不清楚主意,也不知道如何使用游标执行时看到输出.
我从下面提到的链接中做了一个小例子,请告诉这个 [ ^ ]
创建表工人
(
first_name varchar(30),
last_name varchar(30)
)
创建表人
(
person_id int,
person_name varchar(30)
)
Hi..
Im the beginner to cursor concept. I searched many links,but not getting clear idea,and dont know how see the output when executed using cursor.
I did small example from the below mentioned link ,plz tell what wil be the output for this http://abhiwebdevelepor.blogspot.in/2012/03/simple-sql-cursor-for-beginners.html#!/2012/03/simple-sql-cursor-for-beginners.html[^]
create table workers
(
first_name varchar(30),
last_name varchar(30)
)
create table person
(
person_id int,
person_name varchar(30)
)
declare @variable varchar(30)
declare cursor1 cursor
for
select person_name from person where person_name like 'k%' order by person_id
open cursor1
fetch next from cursor1
into @variable
while @@FETCH_STATUS=0
begin
declare @name varchar(50)
set @name='select last_name from workers where last_name='"+@variable+"''
print @name
exec(@name)
fetch next from cursor1 into @variable
end
close cursor1
deallocate cursor1
go
我在''"+ @ variable +"''''
中遇到错误不知道如何解决如何执行游标,需要您的帮助....
Im getting error in ''"+@variable+"''''
Dont know how to solve & how to execute cursor,need your help....
推荐答案
create table workers
(
first_name varchar(30),
last_name varchar(30)
)
create table person
(
person_id int,
person_name varchar(30)
)
insert into workers values ('san','kum')
insert into person values (23,'kum')
declare @variable varchar(35)
declare cursor1 cursor
for
select person_name from person where person_name like 'k%' order by person_id
open cursor1
fetch next from cursor1
into @variable
while @@FETCH_STATUS=0
begin
declare @name varchar(100)
set @name='select last_name from workers where last_name='''+@variable+''''
print @name
exec(@name)
fetch next from cursor1 into @variable
end
close cursor1
deallocate cursor1
go
这篇关于如何找到使用游标执行的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!