RobHVB6Sql
Programmer
Hi, this is my first attempt at rollback.
(Using VB6 and TransactSQL)
I have a stored procedure 'spLoopProcWithRB'
which creates two rows for each client listed in a working table
(normally a couple of hundred).
This works for a small number of clients, ie Ok for 100 but does not create all rows for say 150+.
My questions:
1) Is there a way to get VB to wait until SQL is finished?
2) 'spLoopProcWithRB' is not reporting errors.
Is there a better way to write /code this?
3) Is this a better method than my code?
{
Reference = 'spLoopProcWithRB'
Example of what is sent to spLoopProcWithRB:
Thanks in advance.
Rob Hasard
Data Manager -Genetic Services
(VB6 /SQL 7.0)
(Using VB6 and TransactSQL)
I have a stored procedure 'spLoopProcWithRB'
which creates two rows for each client listed in a working table
(normally a couple of hundred).
This works for a small number of clients, ie Ok for 100 but does not create all rows for say 150+.
My questions:
1) Is there a way to get VB to wait until SQL is finished?
2) 'spLoopProcWithRB' is not reporting errors.
Is there a better way to write /code this?
3) Is this a better method than my code?
{
Reference = 'spLoopProcWithRB'
Code:
/* Reusable procedure allowing looping through */
/* a working table to run another stored procedure. */
/* Also has rollback. */
if exists (select * from sysobjects where id = object_id('spLoopProcWithRB') and sysstat & 0xf = 4)
drop procedure spLoopProcWithRB
CREATE PROCEDURE spLoopProcWithRB
(
@UserID VARCHAR (20),
@ExecuteString1 VARCHAR (300),
@ExecuteString2 VARCHAR (300),
@Recipient VARCHAR (20)
)
AS
-- declare variables for use in the proc
DECLARE @PersonID VARCHAR(20) -- PersonID is the identity of the person currently being processed
DECLARE @ContactID VARCHAR(20) -- ContactID is the identity of the new row inserted here
DECLARE @ExecSQL VARCHAR(6000) -- constructed SQL to run
-- declare a cursor to step through the prepared working table
-- only select rows where processing indictor is null just in case we want to make this
-- proc 'restartable from error'
DECLARE curClient INSENSITIVE CURSOR FOR
SELECT PersonID
FROM dbo.tblTempClientList
WHERE ProcessedWhen IS NULL
AND UserID = @UserID
-- open cursor, fetch first row
OPEN curClient
FETCH NEXT FROM curClient INTO @PersonID
--for each row returned in the cursor...
WHILE (@@FETCH_STATUS = 0)
BEGIN
-- start a transaction
BEGIN TRANSACTION
-- prepare & execute some dynamically constructed SQL
SELECT @ExecSQL = @ExecuteString1 + @PersonID + @ExecuteString2
EXECUTE (@ExecSQL)
-- if there was an error...
IF @@ERROR <> 0
BEGIN
PRINT 'Postal contact record error. @ExecSQL: ' + @ExecSQL + ' PersonID:' + @PersonID --added by RH 25/10/04
ROLLBACK TRANSACTION
RETURN 1
END
ELSE
BEGIN
/* save the generated ContactID to return */
SELECT @ContactID = CONVERT(VARCHAR, @@IDENTITY)
END
--create a record for the Recipient -a mandatory field
SELECT @ExecSQL = 'INSERT INTO tblPostalContactDetails (ContactID, Type, ReferenceCode) ' +
'VALUES (' + @ContactID + ', "Postal Recipient", ' + @Recipient + ')'
EXECUTE (@ExecSQL)
-- if there was an error...
IF @@ERROR <> 0 BEGIN
PRINT 'Recipient record error. @ExecSQL: ' + @ExecSQL + ' PersonID: ' + @PersonID --added by RH 25/10/04
ROLLBACK TRANSACTION
RETURN 1
END
-- finish the transaction
COMMIT TRANSACTION
-- get next row
FETCH NEXT FROM curClient INTO @PersonID
END
-- finished, clean up cursor
CLOSE curClient
DEALLOCATE curClient
GO
Example of what is sent to spLoopProcWithRB:
Code:
spLoopProcWithRB 'he31195',
'spPostalContactUpdate 0, ',
', 123, "27-Oct-2004", "33", "Automatically inserted by Enigma on 27-Oct-2004."',
'1'
Thanks in advance.
Rob Hasard
Data Manager -Genetic Services
(VB6 /SQL 7.0)