2009年7月16日 星期四

MSSQL Hack: be able to execute sys.sp_addrolemember within a transaction

sys.sp_addrolemember is a MS SQL Server system stored procedure that cannot be executed within a user-defined transaction.
The work around is to terminate/commit any outer transaction (only in scenarios where the caller does not have any change *prior to calling sp_addrolemember*, that may need rolling back if something fails *after calling sp_addrolemember*, as this might affect the caller to lose the capability of rolling back unwanted changes).

SET IMPLICIT_TRANSACTIONS OFF -- make sure server does not start any implicit transaction
WHILE XACT_STATE() = 1 -- the transaction is committable
COMMIT TRAN -- Try committing any outer transaction, in order for

EXEC sp_addrolemember
@rolename = 'rolename',
@membername = 'db_user_name'

沒有留言:

張貼留言