May 9, 2012 at 3:22 pm
You aren't going to improve the performance of your stored procedure until you eliminate the cursor(s) and implement set-based logic to accomplish what you are doing in your cursor row by agonizing row.
May 9, 2012 at 3:27 pm
i remove cursor with while ,not improving still.
what other i need to use in place of cursor
May 9, 2012 at 3:36 pm
riya_dave (5/9/2012)
i remove cursor with while ,not improving still.what other i need to use in place of cursor
A WHILE loop is still row by agonizing row processing, just like a cursor. You need to develop a set-based solution to solve your problem.
May 9, 2012 at 3:39 pm
i got it,but anyhow i have to use while loop for condition
May 9, 2012 at 3:53 pm
riya_dave, I've not been here for a couple of months, but you have at least three of the best experts giving you free help here. You haven't given them any information as to what you want to do, as far as I can see
So I have a tip on that basis. Replace the whole SP with SELECT GETDATE(). This is quick and might return the results you want based on the information supplied.
Best of luck.
Tim
.
May 9, 2012 at 3:58 pm
riya_dave (5/9/2012)
i got it,but anyhow i have to use while loop for condition
Why?
Take a break, let me ask you to solve the following hypothetical problem.
I own a company that makes widgets. What a widget is does not matter. All that matters is my company is very good at making widgets. I have been in business for 10 years and my employee table (Emps) has 2135 records, of which 176 are active employees. The others are no longer employed by my company.
This past year our recurring revenue has more than doubled and I want to share some of this with my employees by giving them all a raise. There are 4 classes of employees, and based on their class I want to give either a 10% (1), 8% (2), 6% (3) or 4% (4) raise. The relevent columns in the Emps table for this problem are EmpId, Salary, Class, IsActive. Class is 1,2,3, or 4. IsActive is 0 or 1 with 1 indicating an active employee.
Write the SQL needed to give each employee the appropriate pay raise.
May 9, 2012 at 4:31 pm
riya_dave (5/9/2012)
i got it,but anyhow i have to use while loop for condition
Okay, riya_dave, do you have a solution yet? I have 3 cursor-based solutions and a set-based solution already written. Let me know if you want to see them.
May 9, 2012 at 9:28 pm
Lynn Pettis (5/9/2012)
riya_dave (5/9/2012)
i got it,but anyhow i have to use while loop for conditionOkay, riya_dave, do you have a solution yet? I have 3 cursor-based solutions and a set-based solution already written. Let me know if you want to see them.
Writing cursors now Lynn? Was working with Oracle that horrible?
The force is getting weak with this one...
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
May 9, 2012 at 9:34 pm
WayneS (5/9/2012)
Lynn Pettis (5/9/2012)
riya_dave (5/9/2012)
i got it,but anyhow i have to use while loop for conditionOkay, riya_dave, do you have a solution yet? I have 3 cursor-based solutions and a set-based solution already written. Let me know if you want to see them.
Writing cursors now Lynn? Was working with Oracle that horrible?
The force is getting weak with this one...
Yes, I wrote cursors. Trying to show riya_dave the difference between cursor-based solutions and set-based solution. I'm just waiting to see what kind of solution he develops for the problem I gave him above.
He wants help improving his stored procedure, he needs to give us the information we need to that. How better than showing him the difference?
May 9, 2012 at 9:41 pm
Lynn Pettis (5/9/2012)
WayneS (5/9/2012)
Lynn Pettis (5/9/2012)
riya_dave (5/9/2012)
i got it,but anyhow i have to use while loop for conditionOkay, riya_dave, do you have a solution yet? I have 3 cursor-based solutions and a set-based solution already written. Let me know if you want to see them.
Writing cursors now Lynn? Was working with Oracle that horrible?
The force is getting weak with this one...
Yes, I wrote cursors. Trying to show riya_dave the difference between cursor-based solutions and set-based solution. I'm just waiting to see what kind of solution he develops for the problem I gave him above.
He wants help improving his stored procedure, he needs to give us the information we need to that. How better than showing him the difference?
Well, how about a little bit of guidance? Let's show him how to make sample data for your "problem" in a set based way:
DECLARE @Emps TABLE (
EmpId INT IDENTITY,
Salary numeric(8,2),
Class tinyint,
IsActive bit);
with cte as
(
select top (2135)
RN = row_number() over (order by (select null))
from sys.all_columns sac
)
insert into @Emps (Class, IsActive, Salary)
select Class = NTILE(4) over (order by RN),
IsActive = CASE WHEN RN % (2135/176) = 0 THEN 1 ELSE 0 END,
abs(checksum(newid()))/100000.0
from cte;
select *
from @Emps;
riya_dave: at least one set based solution is even easier than this code for making the sample data.
Also, please note that this now tells us:
1. Table structure involved. (We don't have to guess the data types of any columns.)
2. Sample data that shows the problem. (2135 records, 176 marked as active, all divided into 4 groups, all with a salary)
I know you've been pointed to the first link in my signature numerous times. In between Lynn's definition of the problem, and this sample data, this shows you the best way to post your problems so that you can get help in solving your problem.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
May 9, 2012 at 9:44 pm
riya_dave (5/9/2012)
ok.i remove inserting in identity,but still nt improving performance
The identity column was only to aid you in adding a unique index on your table variable with duplicate values.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
May 9, 2012 at 9:46 pm
riya_dave (5/9/2012)
i remove cursor with while ,not improving still.what other i need to use in place of cursor
See my earlier post where I explained why a while loop without a c.u.r.s.o.r is worse than a c.u.r.s.o.r.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
May 9, 2012 at 9:48 pm
Tim Walker. (5/9/2012)
riya_dave, I've not been here for a couple of months, but you have at least three of the best experts giving you free help here. You haven't given them any information as to what you want to do, as far as I can seeSo I have a tip on that basis. Replace the whole SP with SELECT GETDATE(). This is quick and might return the results you want based on the information supplied.
Best of luck.
Tim
Hi Tim.
SELECT 1 might be a wee bit faster.
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
May 9, 2012 at 9:55 pm
riya_dave, still waiting to see what kind of solution you came up with before I show you the four solutions I put together in 30 minutes.
Wayne showed you how to setup some test data, and it won't take me much to fixup my code to use his test suite in my code.
May 10, 2012 at 1:25 am
riya_dave (5/9/2012)
i remove cursor with while ,not improving still.what other i need to use in place of cursor
TSQL 101, SQL for Dummies, or any other TSQL book for beginners. Spend a few weeks running through the examples. It will take you far less time to learn the basics from a book than by posting random questions here.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 15 posts - 16 through 30 (of 51 total)
You must be logged in to reply to this topic. Login to reply