Groups | Blog | Home
all groups > sql server programming > april 2007 >

sql server programming : Cursor trouble 2005



Erland Sommarskog
4/21/2007 8:28:24 PM
michael vardinghus (xx) writes:
[quoted text, click to view]

Without knowing the definition of the underlying the table - or is that
a view, it's difficult to say exactly what is going on. Then again,
it should be a simple thing to rewrite the UPDATE statement to use
a regular WHERE clause, and not WHERE CURRENT OF. In fact, while I need
to confess that I resort to cursors sometimes, I have found that I
never need to use WHERE CURRENT OF.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
michael vardinghus
4/21/2007 10:10:22 PM
Greetings All

I didnt work with 2005 much yet and I'm having trouble with a cursor script
used on 2000.

When this is exececuted it says "The cursor is READ ONLY" and i tried to
look through books online or internet but couldnt find an answer.

Anyone ?

USE [MIVARTEST_EDW]

GO

/****** Object: StoredProcedure [dbo].[C_51_HDS_OPRET_PERIODER_FINANSÅR]
Script Date: 04/21/2007 21:06:04 ******/

--ALTER PROCEDURE [dbo].[C_51_HDS_OPRET_PERIODER_FINANSÅR] AS

SET NOCOUNT ON

DECLARE @Arbejdsdag INT

DECLARE @PrevArbejdsdag INT

DECLARE @Dato Datetime

DECLARE @TempPeriode Datetime

DECLARE @Ugedag_nummer INT

DECLARE @Førstedag INT

DECLARE @Helligdag INT

DECLARE @Fridag INT

DECLARE @Periode Datetime

/*

@TempPeriode gemmer det aktuelle Finansår_ID

*/

/*

Temporær tabel til at gemme sidste Arbejdsdag Finansår_ID

*/

--CREATE TABLE #t (Periode INTEGER PRIMARY KEY)

/*

Cursor, der gennemløber for Periodisering ift Sidste Arbejdsdag FINANSÅR

*/

DECLARE cursDim CURSOR FOR

SELECT

Dato,

Arbejdsdag,

Ugedag_nummer,

Helligdag

FROM Dimensions.TID

ORDER BY Dato

FOR UPDATE OF Dimensions.TID.Sidste_Arbejdsdag

OPEN cursDim

FETCH NEXT FROM cursDim

INTO @Dato,

@Arbejdsdag,

@Ugedag_nummer,

@Helligdag





WHILE @@FETCH_STATUS = 0

BEGIN

/*

Set @TempPeriode fra Finansår i temp tabel

*/

SET @TempPeriode=@Dato

SET @PrevArbejdsdag = @Arbejdsdag

/*

Opdatér Sidste_Arbejdsdag_ID for aktuelle post i cursor

*/

UPDATE Dimensions.TID

SET Sidste_Arbejdsdag = @Periode

--(SELECT Periode FROM #t)

WHERE CURRENT OF cursDim

FETCH NEXT FROM cursDim

INTO @Dato,

@Arbejdsdag,

@Ugedag_nummer,

@Helligdag





/*

Husker sidste arbejdsdag i #t

*/



IF @PrevArbejdsdag = '1'

BEGIN

-- TRUNCATE TABLE #t

-- INSERT INTO #t VALUES(@Finansår_ID-1)

SET @Periode = @Dato-1

END







END

CLOSE cursDim

DEALLOCATE cursDim

--DROP TABLE #t



TheSQLGuru
4/22/2007 12:00:00 AM
1) Does the user executing the sproc have permissions to update the
Dimensions.TID table?

2) Try removing the Order By from the SELECT in the cursor definition. From
what I see of the logic, it is unnecessary and therefore inefficient. It
may also be causing the forward_only (default setting) cursor to be
non-updatable. Just a guess.

--
TheSQLGuru
President
Indicium Resources, Inc.

[quoted text, click to view]

AddThis Social Bookmark Button