Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Cursor Issue Expand / Collapse
Author
Message
Posted Monday, October 14, 2013 2:21 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, November 26, 2013 10:49 PM
Points: 7, 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
Post #1504351
Posted Monday, October 14, 2013 2:29 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:41 PM
Points: 12,201, Visits: 9,156
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1504355
Posted Monday, October 14, 2013 2:37 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, November 26, 2013 10:49 PM
Points: 7, Visits: 49
I wanted to try using cursor. I know that performance is poor but still i want to implement the logic using cursor.
Post #1504360
Posted Monday, October 14, 2013 3:06 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:41 PM
Points: 12,201, Visits: 9,156
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1504367
Posted Monday, October 14, 2013 3:19 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, November 26, 2013 10:49 PM
Points: 7, 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.



Post #1504372
Posted Monday, October 14, 2013 3:20 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:41 PM
Points: 12,201, Visits: 9,156
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1504373
Posted Monday, October 14, 2013 4:00 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Monday, April 14, 2014 11:49 AM
Points: 739, Visits: 2,470
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
Post #1504385
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse