Advertisement

03.10.2008 at 03:55PM PDT, ID: 23230257
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.7

Optimizing a cursor query in MS SQL 2005

Asked by JohnModig in SQL Server 2005, Databases Miscellaneous, MS SQL Server

Tags: , ,

Dear Experts,
I am trying to create a stored procedure that returns multiple tables. The database contains two tables for this, one for accounts and one for costs. I need my stored procedure to create a table with all the costs for each account in a specific project.

I have managed to do this using a cursor (see code below). My sp is working but being a beginner I need to make sure that I am doing this in the best way possible. I have read that I should stay away from cursurs if possible due to resource problems. Is there an alternative for me? Can my code be optimized or re-written without using a cursor?Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
USE [project_management]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [projects].[spGetProjectCosts_cursor]
	-- parameter for project id
	@pId int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
 
	-- Declare the variables to store the values returned by FETCH.
	DECLARE @aId int, @aNo int
 
	-- create a cursor to find cost for distinct accounts of the project
	DECLARE account_cursor CURSOR LOCAL FAST_FORWARD READ_ONLY FOR
	SELECT DISTINCT t1.account_id, t2.account_number 
	FROM projects.costs t1
	INNER JOIN finance.accounts t2 ON t1.account_id = t2.id 
	WHERE project_id = @pId
	ORDER BY t2.account_number ASC
 
	OPEN account_cursor
 
	-- Perform the first fetch and store the values in variables.
	-- Note: The variables are in the same order as the columns in the SELECT statement.
	FETCH NEXT FROM account_cursor
	INTO @aId, @aNo
 
	-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
	WHILE @@FETCH_STATUS = 0
	BEGIN
 
		-- Get the costs for this specific account
		-- I have limited the number of columns to one for EE
		SELECT cost_name
		FROM projects.costs
		WHERE account_id = @aId AND project_id = @pId
 
		-- This is executed as long as the previous fetch succeeds.
		FETCH NEXT FROM account_cursor
		INTO @aId, @aNo
	END
 
CLOSE account_cursor
DEALLOCATE account_cursor
 
END
 
Loading Advertisement...
 
[+][-]03.10.2008 at 04:18PM PDT, ID: 21091468

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.10.2008 at 04:22PM PDT, ID: 21091488

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SQL Server 2005, Databases Miscellaneous, MS SQL Server
Tags: Microsoft, SQL Server, 2005
Sign Up Now!
Solution Provided By: DanielWilson
Participating Experts: 4
Solution Grade: B
 
 
[+][-]03.10.2008 at 04:40PM PDT, ID: 21091640

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.10.2008 at 05:27PM PDT, ID: 21091903

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.10.2008 at 05:39PM PDT, ID: 21091955

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.11.2008 at 12:50AM PDT, ID: 21093704

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.11.2008 at 03:05AM PDT, ID: 21094258

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03.11.2008 at 04:21AM PDT, ID: 21094600

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03.11.2008 at 06:08AM PDT, ID: 21095305

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.11.2008 at 06:12AM PDT, ID: 21095342

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.11.2008 at 07:54AM PDT, ID: 21096266

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.11.2008 at 09:10AM PDT, ID: 21097138

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628