February 25, 2015 at 5:14 am
select * from emp
select * from dept
select a.empno,a.ename,job,sal,b.deptno,b.dname
from emp a
join dept b
on a.deptno=b.deptno
where a.deptno=10
Declare
@Empno int,@Ename nvarchar(20),@Job nvarchar(20),@Sal decimal(10,2),@Deptno int,@DeptName nvarchar(15)
------------------------------------------------------cursor1
declare Dept_Cur cursor for
select deptno,dname from dept
open Dept_Cur
fetch next from Dept_Cur into @Deptno,@DeptName
while @@Fetch_status=0
begin
-------------------------------------------------------cursor2
declare Emp_Cur cursor for
select a.empno,a.ename,job,sal
from emp a
join dept b
on a.deptno=b.deptno
where a.deptno=@Deptno
open Emp_Cur
fetch next from Emp_cur into @Empno,@Ename,@Job,@Sal
while @@fetch_status=0
begin
cast(@Deptno as varchar(10)) + ' ' +
@DeptName + ' ' +
cast(@Empno as varchar(10)) + ' ' +
@Ename + ' ' + @Job + ' '+ cast(@Sal as varchar(10))
fetch next from Emp_Cur into @Empno,@Ename,@Job,@Sal
end
close Emp_Cur
deallocate Emp_cur
-----------------------------------------------------------cursor2
fetch next from Dept_Cur into @Deptno,@DeptName
End
close Dept_Cur
deallocate Dept_Cur
-------------------------------------------------------cursor1
February 25, 2015 at 5:20 am
Quick question, what is the question?
😎
February 25, 2015 at 6:51 am
Eirikur Eiriksson (2/25/2015)
Quick question, what is the question?😎
Right. Without an actual question, it's difficult to give an actual answer. 😉 I don't like the cursors. Why not just fire a SELECT statement instead of using nested RBAR?
February 26, 2015 at 1:54 pm
@Eirikur Eiriksson LOL, this was a good one!
February 26, 2015 at 5:03 pm
There's no question here. The third select on the original post shows the set-based replacement for the cursors. The title truly reflects what was posted... it's an example. Cool, huh?
--Jeff Moden
Change is inevitable... Change for the better is not.
February 27, 2015 at 1:47 am
Jeff Moden (2/26/2015)
There's no question here. The third select on the original post shows the set-based replacement for the cursors. The title truly reflects what was posted... it's an example. Cool, huh?
Totally missed that, cannot even use my normal lack of industrial strength espresso excuse:-D
😎
February 27, 2015 at 5:25 am
Eirikur Eiriksson (2/27/2015)
Jeff Moden (2/26/2015)
There's no question here. The third select on the original post shows the set-based replacement for the cursors. The title truly reflects what was posted... it's an example. Cool, huh?Totally missed that, cannot even use my normal lack of industrial strength espresso excuse:-D
😎
That's why I asked about firing a single SELECT statement instad. The nested RBAR would be even worse than simple RBAR.
February 27, 2015 at 7:48 am
This would make a great article. You should write it up and submit it to the folks on this site.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply