Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

do a sql query that i can undo

add a command to a sql query so i can undo query
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

so
BEGIN TRAN @TransactionName
  INSERT INTO ValueTable VALUES(1)
  INSERT INTO ValueTable VALUES(2)
ROLLBACK TRAN @TransactionName

does nothing



and
BEGIN TRAN @TransactionName
  INSERT INTO ValueTable VALUES(1)
  INSERT INTO ValueTable VALUES(2)
ROLLBACK TRAN @TransactionName
COMMIT TRAN @TransactionName

would execute transaction






do i have to set up a name for TransactionName

To execute transaction don't use/remove : ROLLBACK TRAN @TransactionName

@TransactionName is the name of your transaction, you can use another name of course.
transactions in T-SQL let see the following example:

CREATE PROCEDURE spAddBookmark
 @UserID AS INT,
 @Comment AS VARCHAR (100) = NULL,
 @GroupID AS INT = NULL,
 @ID AS INT OUTPUT
AS

DECLARE @Bookmarks INT
DECLARE @err INT

SET NOCOUNT ON
SET ANSI_WARNINGS OFF

BEGIN TRANSACTION
begin
INSERT INTO Books (UserID, Comment) VALUES (@UserID, @Comment)
-- If any error Roll back

SET @err = @@ERROR
IF @err <> 0
 BEGIN
  ROLLBACK TRANSACTION
  RETURN @err
 END
ELSE
 BEGIN
  SELECT @ID = @@IDENTITY
 END
else
COMMIT TRANSACTION

end

 END
-- If noerror Insert

SET NOCOUNT OFF
SET ANSI_WARNINGS ON
GO
Thanks!