Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL Cursor help?

Author  Topic 

techglider
Starting Member

26 Posts

Posted - 2009-04-15 : 15:21:38
OK Updated version...
I have the following table that i want to update.. and get values from..

table1


What i need to do is loop through this table.. one row at a time getting the value from column 'branch', and using that value, put it in the select query that will give me the results i need. Then put that result needed into column 'branch_count' in its respective row that the original value was gotten from. And move on to the next column 'branch's, row which would be row number 2 and do this until there are no more rows..

This is what i currently have but it is not working correctly..


DECLARE branchComparison
CURSOR FOR SELECT branch
FROM table1
DECLARE @e_branch integer
OPEN branchComparison
FETCH FROM branchComparison INTO @e_branch
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE table1 SET branch_count =
(SELECT
count(distinct calculatedValue)
FROM tablea a,
tableb b,
JOIN tablec c
ON b.valueX = c.valueY
WHERE b.valueZ > '01/01/2000'
AND a.branch = @e_branch
AND b.valueC = 1
AND c.valueD IS NOT NULL)
FETCH NEXT FROM branchComparison INTO @e_branch
END
CLOSE branchComparison
DEALLOCATE branchComparison


If I run the select query by itself and get the desired result buy manually replacing the
@e_branch value with the respective column and row value.. The result of

SELECT
count(distinct calculatedValue)
FROM tablea a,
tableb b,
JOIN tablec c
ON b.valueX = c.valueY
WHERE b.valueZ > '01/01/2000'
AND a.branch = @e_branch <--- Which would now be "AND a.branch = 1"
AND b.valueC = 1
AND c.valueD IS NOT NULL


Running that select query gives me this result...

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-15 : 15:31:25
UPDATE MyTable
SET Col3 = COALESCE(Col3, Col1),
Col4 = COALESCE(Col4, Col2)



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

techglider
Starting Member

26 Posts

Posted - 2009-04-15 : 15:58:38
How will that get the new value from the select query inside the loop? Which i called newValue?

Definitely need some response rather than just code.. I want to learn.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-15 : 16:09:03
Then you need to supply some proper sample data and also post your expected output.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

techglider
Starting Member

26 Posts

Posted - 2009-04-15 : 16:26:25
Updated original post ^ Scroll UP ^
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-15 : 16:33:20
Can you also post an image of your expected output?
How will the table look when you are done?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-15 : 16:38:49
[code]UPDATE t1
SET t1.branch_count = e.x
FROM Table1 AS t1
INNER JOIN (
SELECT a.Branch,
COUNT(DISTINCT calculatedValue) AS x
FROM TableA AS a
INNER JOIN TableB AS b ON b.ValueZ >= '01/01/2000'
AND b.ValueC = 1
INNER JOIN TableC AS c ON c.ValueY = b.ValueX
WHERE c.valueD IS NOT NULL
GROUP BY a.Branch
) AS e ON e.Branch = t1.Branch[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

techglider
Starting Member

26 Posts

Posted - 2009-04-15 : 16:47:29
Original post updated
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-15 : 16:51:01
See post made 04/15/2009 : 16:38:49


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

techglider
Starting Member

26 Posts

Posted - 2009-04-15 : 17:23:57
quote:
Originally posted by Peso

UPDATE		t1 
SET t1.branch_count = e.x
FROM Table1 AS t1
INNER JOIN (
SELECT a.Branch,
COUNT(DISTINCT calculatedValue) AS x
FROM TableA AS a
INNER JOIN TableB AS b ON b.ValueZ >= '01/01/2000'
AND b.ValueC = 1
INNER JOIN TableC AS c ON c.ValueY = b.ValueX
WHERE c.valueD IS NOT NULL
GROUP BY a.Branch
) AS e ON e.Branch = t1.Branch



E 12°55'05.63"
N 56°04'39.26"





Im confused on this line "SET t1.branch_count = e.x"

where did the e.x come from?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-15 : 17:29:31
It comes from the derived table e.
The x column is the distinct count of calculatedValue for any given Branch.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

techglider
Starting Member

26 Posts

Posted - 2009-04-16 : 08:46:26
This is exactly how i want it to work..


DECLARE branchComparison
CURSOR FOR SELECT branch
FROM table1
DECLARE @e_branch integer

DECLARE @branch_count INTEGER

OPEN branchComparison
FETCH FROM branchComparison INTO @e_branch
WHILE @@FETCH_STATUS = 0
BEGIN

SELECT @branch_count = count(distinct calculatedValue)
FROM tablea a,
tableb b,
JOIN tablec c
ON b.valueX = c.valueY
WHERE b.valueZ > '01/01/2000'
AND a.branch = @e_branch
AND b.valueC = 1
AND c.valueD IS NOT NULL

UPDATE table1 SET branch_count = @branch_count WHERE branch = @e_branch

FETCH NEXT FROM branchComparison INTO @e_branch
END
CLOSE branchComparison
DEALLOCATE branchComparison


So now i am trying to make it so that query doesn't take 20+ minutes and perhaps do column4 at the same time, where column4 gets its calcuated value from column2.. soo something like this?


DECLARE branchComparison
CURSOR FOR SELECT branch, code
FROM table1
DECLARE @e_branch INTEGER
DECLARE @branch_count INTEGER
DECLARE @e_code VARCHAR(100)
DECLARE @code_count INTEGER

OPEN branchComparison
FETCH FROM branchComparison INTO @e_branch,@e_code

WHILE @@FETCH_STATUS = 0
BEGIN

SELECT @branch_count = count(distinct calculatedValue)
FROM tablea a,
tableb b,
JOIN tablec c
ON b.valueX = c.valueY
WHERE b.valueZ > '01/01/2000'
AND a.branch = @e_branch
AND b.valueC = 1
AND c.valueD IS NOT NULL

UPDATE table1 SET branch_count = @branch_count WHERE branch = @e_branch

SELECT @code_count = count(distinct calculatedValue)
FROM tablea a,
tableb b,
JOIN tablec c
ON b.valueX = c.valueY
WHERE b.valueZ > '01/01/2000'
AND b.Branch_ID = @e_code
AND b.valueC = 1
AND c.valueD IS NOT NULL

UPDATE table1 SET code_count = @code_count WHERE code= @e_code

FETCH NEXT FROM branchComparison INTO @e_branch,@e_code

END
CLOSE branchComparison
DEALLOCATE branchComparison


Or is there a simpler way to do this? Cause this takes forever!
Go to Top of Page

tosscrosby
Aged Yak Warrior

676 Posts

Posted - 2009-04-16 : 09:36:38
Why does this seem suspiciously like the "%fox%" dude who didn't want a set based soultion because cursors were "better" - in his opinion?????

Did you test Peso's solution? If so, did it provide you with your expected results?

Terry

-- Procrastinate now!
Go to Top of Page

techglider
Starting Member

26 Posts

Posted - 2009-04-16 : 10:04:23
tosscrosby.. I am pretty new to Advanced SQL and I am seeing this query is taking while. As i stated in the first post, if you can show me a easier or more clearer way to get to the results i am looking for.. Take me to school.. Im here to learn and not argue.. So if you can duplicate what i need.. please show me a way.. The last post i submitted has the code to get the EXACT results i need but only does it one column at a time and secondly takes FOREVER..

p.s. I dunno who this %fox% fella is but i assure you i have opinions and they aren't code related, much more political based, and when it comes to programming, i have no opinions only questions.. Thanks guys
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-04-16 : 11:28:49
what were results when you used peso's suggestion on 04/15/2009 : 16:38:49?
Go to Top of Page

techglider
Starting Member

26 Posts

Posted - 2009-04-16 : 12:30:38
The query kept going for over an hour so i stopped it but, it finished that column with i believe the correct values
Go to Top of Page
   

- Advertisement -