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


Cursors


Cursors

Author
Message
eligiable
eligiable
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 41
--Can AnyOne Help me in this ...
I am using the following Procedure to open a Cursor to update some data from a table to another Temporary Table, but I am not getting the desired results, because the Cursor update the table with the following data in all the fields?

Code = 509
GPOName = ABC
TotalMO = 25259

CREATE PROCEDURE DailyUPD
AS

Declare @Code as int
Declare @GPOName as varchar(50)
Declare @TotalMO as varchar(50)

Begin

DECLARE DailyUPD_Cursor CURSOR FOR

select distinct(masterdata.gpo_id) as Code, gpo.name as "GPO Name", count(masterdata.mno) as "Total MO" from masterdata, gpo
where masterdata.gpo_id = gpo.gpo_id
group by masterdata.gpo_id, gpo.name
order by masterdata.gpo_id

OPEN DailyUPD_Cursor

FETCH NEXT FROM DailyUPD_Cursor into @Code, @GPOName, @TotalMO

WHILE @@FETCH_STATUS = 0

BEGIN
Update tempDailyUPD
Set Code = @Code, GPOName = @GPOName, TotalMO = @TotalMO
FETCH NEXT FROM DailyUPD_Cursor into @Code, @GPOName, @TotalMO
-- FETCH NEXT FROM DailyUPD_Cursor
END

CLOSE DailyUPD_Cursor
DEALLOCATE DailyUPD_Cursor

End
GO
Dave Ballantyne
Dave Ballantyne
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2936 Visits: 8370
There are no sarg's on you update statement,

You probably meant to say

Update Table
Where current of CursorName

Take a look in books online for more details.

I would also argue that you dont need a cursor for this operation at all.

Try to get all your logic into one update statement , your server will be much happier for the effort :-)



Clear Sky SQL
My Blog
Florian Reischl
Florian Reischl
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3559 Visits: 3934
There is no reason for the cursor. Try a joined UPDATE to get less code and much better performance:

UPDATE t SET
t.Code = md.Code,
t.GPOName = md.GPONam,
t.TotalMO = md.TotalMO
FROM tempDailyUPD
JOIN
(
select
distinct(masterdata.gpo_id) as Code,
gpo.name as GPOName,
count(masterdata.mno) as TotalMO
from masterdata, gpo
where masterdata.gpo_id = gpo.gpo_id
group by masterdata.gpo_id, gpo.name
) md
ON t.[Your Destination JOIN Column] = md..[Your Source JOIN Column]



Notice the ON part of the JOIN. Here you have to define the relation between both tables which seems to be missing in your script.

Flo


The more I learn, the more I know what I do not know
Blog: Things about Software Architecture, .NET development and T-SQL

How to Post Data/Code to get the best Help How to Post Performance Problems
eligiable
eligiable
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 41
Thanx |Ten Centuries| This is much helpful
But I am facing another thing which is a bit complex, I have to run the following procedures in the same I have mentioned above by passing the same variables in the clause as; (@YearMonth, @sDate, @eDate), following are the queries I am trying to pass in these SP:

Begin
Execute CountStatusD @YearMonth
Execute CountStatusU @YearMonth
Execute CountStatusNull @YearMonth
Execute DisposedBefore @sDate, @eDate, @YearMonth
Execute DisposedToday @sDate, @eDate, @YearMonth
End

SELECT DISTINCT masterdata.gpo_id AS Code, gpo.name AS [GPO Name], COUNT(masterdata.mno) AS [Total MO]
FROM masterdata INNER JOIN
gpo ON masterdata.gpo_id = gpo.gpo_id
WHERE (masterdata.year_month = @YearMonth)
GROUP BY masterdata.gpo_id, gpo.name
ORDER BY masterdata.gpo_id

SELECT DISTINCT gpo_id AS Code, COUNT(mno) AS Delivered
FROM masterdata
WHERE (year_month = @YearMonth) AND (status = 'D') -- Here I will pass Three Status as "U and NULL"
GROUP BY gpo_id
ORDER BY gpo_id

SELECT DISTINCT (masterdata.gpo_id) AS Code, COUNT(mno) AS DisposedBefore -- The Same will run as DisposedToday
FROM masterdata
WHERE status IS NOT NULL AND year_month = @YearMonth AND date_reported >= @sDate AND date_reported <= @eDate
GROUP BY masterdata.gpo_id
ORDER BY masterdata.gpo_id

Would there be a scenario to run all these queries as one, because I have to update the same tempDailyUPD Table by running all these SP/Queries, like;

Update tempDailyUPD
Set DisposedBefore = @DisposedBefore where Code=@Code

Please help 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