SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Cursor Issue


Cursor Issue

Author
Message
pavithrareddyc
pavithrareddyc
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 49
Hello,

I am using cursor for the first time. I have to print employee first and last name whose salary is less than the average salary of the corresponding department and its start day before 1/1/1990 Using Cursor in sql server 2008.

I could able to display the result using below query,but problem is output is displaying 6 times (from this query result is 6 rows : select id, Fname, Lname from employees where startdate <='1990-01-01') same result.
I want to display it only once.
Please help me.

Thanks in advance.
Logic:

declare @id int
declare @Fname varchar (50)
declare @Lname varchar (50)

declare C_employees cursor for
select id, Fname, Lname from employees
where startdate <='1990-01-01'
--and id is not null and fname is not null and lname is not null

open C_employees
fetch next from c_employees into @id,@Fname,@Lname
WHILE (@@FETCH_STATUS = 0)
BEGIN

select distinct A.id,A.FName,A.Lname from Employees A
inner join
(
select department,AVG(Salary) as ASalary from employees
where startdate <='1990-01-01'
group by department
)B
on A.department=B.department
where A.salary<B.ASalary
and A.startdate <='1990-01-01'
order by A.id desc

FETCH NEXT from C_employees into @id,@Fname,@Lname

END
CLOSE C_employees

DEALLOCATE C_employees
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27715 Visits: 13268
Any reason why you need to use a cursor?


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
pavithrareddyc
pavithrareddyc
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 49
I wanted to try using cursor. I know that performance is poor but still i want to implement the logic using cursor.
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27715 Visits: 13268
pavithrareddyc (10/14/2013)
I wanted to try using cursor. I know that performance is poor but still i want to implement the logic using cursor.


You can start by providing table DDL, sample data in the form of INSERT statements and the desired output. :-)


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
pavithrareddyc
pavithrareddyc
Forum Newbie
Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)Forum Newbie (9 reputation)

Group: General Forum Members
Points: 9 Visits: 49
table :

id int Checked
FName varchar(50) Checked
LName varchar(50) Checked
Salary money Checked
department varchar(50) Checked
startdate date Checked

Sample data :select * from Employees

id FName LName Salary department startdate
1 pavi Gur 100.00 software developer 1991-01-01
2 pra cha 200.00 sr software developer 1989-01-01
3 tom cruise 300.00 pharmacist 1980-01-01
4 sandeep Gur 200.00 java developer 1994-01-01
5 pavi cha 300.00 sr software developer 1987-01-01
6 sandy maram 400.00 sr software developer 1984-01-01
7 indu maram 400.00 java developer 1984-01-01
8 henry chan 500.00 java developer 1982-01-01

output from Cursor:

repeating same output 6 times

id FName Lname
7 indu maram
2 pra cha

id FName Lname
7 indu maram
2 pra cha

id FName Lname
7 indu maram
2 pra cha

id FName Lname
7 indu maram
2 pra cha

id FName Lname
7 indu maram
2 pra cha

id FName Lname
7 indu maram
2 pra cha


Thanks.
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27715 Visits: 13268
Please read the first article in my signature on how to post questions. ;-)
It will give you more info on how to post table definitions, sample data etc.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Sean Pearce
Sean Pearce
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1792 Visits: 3432
You are executing exactly the same query 6 times because of the cursor. There is absolutely no reason to use a cursor for this. Why would you want to use a cursor when you have acknowledged that you are aware of the poor performance of using one.



The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search