CREATEProc[Dbo].[Sys_rebate_equity]AsDeclare@fMemberIDvarchar50)--User IDDeclare@RebateDecimal18,2)--Total number of sharesBEGINBeginTryBeginTransaction--Start a transactionDECLARE Cursor1CURSORFor--Defining Cursors Cursor1Select Fmemberid,SUM (fnumber)As‘Fnumber‘From bp_equitywhere Fnumber>0Groupby Fmemberid--Objects that use cursorsOpen Cursor1--Open cursorFetchNextFrom Cursor1Into@fMemberID,@Rebate--Move the cursor down by 1 rows to get the data into the previously defined variable @fmemberid, @RebateWhile@ @FETCH_STATUS=0--Determine if the data was successfully obtainedBeginSelect1--To handle accordinglyFetchNextFrom Cursor1Into@id--Move the cursor down by 1 rowsEndClose Cursor1--Close CursorsDeallocate Cursor1--To delete a cursor referenceSelect888commit transaction --< Span style= "color: #008080;" > COMMIT transaction end Try begin< Span style= "color: #000000;" > Catch if @ @trancount > 0 rollback transaction -- ROLLBACK TRANSACTION 8888 end Catch end
SQL stored procedures, using transactions (Try Catch), cursors