rgb192
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
To execute transaction don't use/remove : ROLLBACK TRAN @TransactionName
@TransactionName is the name of your transaction, you can use another name of course.
@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
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
More information
http://doc.ddart.net/mssql/sql70/ba-bz_3.htm
http://doc.ddart.net/mssql/sql70/ba-bz_3.htm
Thanks!
ASKER
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