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.
Author |
Topic |
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-09-12 : 10:54:45
|
Jeff writes "I have a stored procedure that creates a cursor and I'm trying to use the value of that cursor to update a record in another table. I get the error:The cursor does not include the table being modified or the table is not updatable through the cursor. Here's the SPROC can you do this?CREATE PROCEDURE dbo.spMakeBatchCursor @packCount int, @carrier varchar(10), @maxBatchID int = NULLASSELECT DISTINCT a.CartContentID, c.CartIDINTO #PackCursorFROM shipTransactions A, ShippingMethod B, Cart CWHERE a.BatchID = 0AND c.CartID = a.CartIDAND a.ShipMethodCode IN (select ShipMethodCode from ShippingMethod where carrier = @carrier)EXEC spMakeBatchParameters @packCount, NULL, @carrierSELECT @maxBatchID = (SELECT MAX(BatchID) FROM BatchInfo)DECLARE @ThisCursor int -- holding variableDECLARE @ThisCount intSELECT @ThisCount = 1DECLARE pCursor CURSOR -- create and fill cursorGLOBAL -- callable outside this processSCROLL -- Scroll back from @packCountDYNAMIC -- STATICOPTIMISTICFORSELECT CartContentID FROM #PackCursorFOR UPDATE OF cartContentID-- Open Cursor and process to the Nth fetched recordOPEN pCursorFETCH NEXT FROM pCursor INTO @ThisCursorWHILE (@ThisCount<=@packCount) AND (@@FETCH_STATUS=0)BEGINUPDATE ShipTransactionsSET batchID = @maxBatchIDWHERE CURRENT OF pCursorSELECT @ThisCount = @ThisCount +1FETCH NEXT FROM pCursor INTO @ThisCursorENDCLOSE pCursorDEALLOCATE pCursorDROP TABLE #PackCursor" |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2002-09-12 : 12:19:51
|
If the cursor is updateable for that table then you should be able to do it.Whether it is a good idea to do it that way is another question though.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2002-09-12 : 12:29:31
|
You're trying to UPDATE...WHERE CURRENT OF on a table that isn't included in the cursor, no wonder why it's not working. |
|
|
|
|
|