Avatar of ZURINET
ZURINET

asked on 

CURSOR update Table

Hi all
Senario Table A primary key
             Table B references primary key Table A

I have a Store procedure that is expected to do the following
1 - use a select statement to select values for cursor
2 - Fetch from cursor into variables
3 - Performe delete, insert and update base on return values
4 - Calculate how many records are deleted
5 - update the primary key row , by setting date done.

However If I have 1 Primary key row in table A that is referenced by Table B
The query works fine and  Table A. PK is updated as expected

But if there are More than 1 Row  in Table A... all the rows in Table B are processed but only 1 Primary Row table get update at last.
see snippet below for guidance..

I need to update each primary key Row after each processing of the corresponding child tables
and move to the next Primary Key row.. process the child table. update the primary key...
move to the next primary key ,, process the  child table.. update the primary key.


Thanks in Advance
CREATE PROCEDURE [dbo].[sp_Import]
	as
		SET NOCOUNT ON
		
		
	-- Declare Variables 
			DECLARE 
				@TransactionID		bigint,
				@-------------
	
   
	-- Declare Cursor for Cutomer Objects
			DECLARE transaction_C_O CURSOR FOR
			
				SELECT	TransactionID, 			
				FROM		C_O h, M_C i
				WHERE	h.Status = 50 
				AND h.TransactionID = i.fk_TransactionID 
				ORDER BY	TransactionID
				
 
	OPEN transaction_C_O 
	
 
	FETCH NEXT FROM transaction_C_O 
		INTO 	@TransactionID, @..... other Attributes
	
	WHILE (@@FETCH_STATUS = 0)
 
	BEGIN --!!		
	
			BEGIN TRANSACTION 
					if (@F0bis = '2000-01-01')
				BEGIN --!!
					SET @F0bis = NULL
				END --!
		BEGIN  --!!
						
		if (@Command = 'DEC')
				BEGIN --!!
					SET TRANSACTION ISOLATION LEVEL READ COMMITTED
					BEGIN TRANSACTION AA
					
					
						if exists (SELECT Conditions)
							BEGIN --!!
								DELETE
								FROM	Table
								WHERE condition
							   -----------------------							
								INSERT INTO Table (columns) 
								VALUES 
										(values)
 							 -----------------------
								UPDATE	Table
								SET		statement
								FROM	Table
								WHERE	condition												
 
							END 
						else
							BEGIN 
								--Something
							END 
					COMMIT TRANSACTION --!
				END  
 
      else
						BEGIN 
							--Do something
						END 
		END 
 
COMMIT TRANSACTION 
	
 
	FETCH NEXT FROM transaction_C_O 
		INTO 	@TransactionID,--other columns
 
 
	END --!
 
 
CLOSE transaction..
-- Count how many rows in update and set status done in the Primary key row
								SET @Reco = (SELECT  --Statement)
							
								if (@RecordCount = @Reco)
									BEGIN									
										UPDATE	statement										
									END
								else
									BEGIN									
										UPDATE	statement
										
									END
 
DEALLOCATE transaction_..

Open in new window

Microsoft SQL Server

Avatar of undefined
Last Comment
ZURINET
Avatar of cdbeste
cdbeste

bad idea changing the primary key...

ask yourself the question Why do I need to change the primary key and you will
find the real source of the problem..
Avatar of ZURINET
ZURINET

ASKER

Sorry Wrong comment!
I am not changing the primary key rather

update and set status done!!! --- that is what I need to do after processing the child key
and not the primary key

-- Count how many rows in update and set status done in the Primary key row
                                                                SET @Reco = (SELECT  --Statement)
                                                       
                                                                if (@RecordCount = @Reco)
                                                                        BEGIN                                                                  
                                                                                UPDATE  statement                                                                              
                                                                        END
                                                                else
                                                                        BEGIN                                                                  
                                                                                UPDATE  statement
                                                                               
                                                                        END
Avatar of momi_sabag
momi_sabag
Flag of United States of America image

you need to move your update to the primary table before the fetch of the next record from the cursor
you are doing the update outside the loop, that is why only one row gets updated
Avatar of ZURINET
ZURINET

ASKER

Do you mean between

 
COMMIT TRANSACTION
        ???????Here
 
        FETCH NEXT FROM transaction_C_O
                INTO    @TransactionID,--other columns
 
 
        END --!
 
Avatar of momi_sabag
momi_sabag
Flag of United States of America image

yep

as long as it is outside the loop, it will only be performed once...
Avatar of ZURINET
ZURINET

ASKER

Tried just that ..
But non of the pk table is update..

But the child row fields have their respective values update.. ;-(
ASKER CERTIFIED SOLUTION
Avatar of ZURINET
ZURINET

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo