Smartssolutions

Wednesday, January 5, 2011

CREATE DATABASE statement not allowed within multi-statement transaction and SSIS

In fact, my aim was creating an entire data base in one single transaction in an atomic manner. In other words, if one SQL statement is failed for one raison or another then all the data base won't exist within my SQL Server instance. But one small problem could be anoying in that case. The problem is that CREATE DATABASE DDL statement is not allowed within multistatement tranaction. So how to do?

Suppose that we are creating this small pakage


This above package will definetly fail if we set the Sequence container TransactionOption property to required. In the other hand, if we move the first create data base task out of the sequence container then the data base will be created even if the sequence container fails   
To tewak that, I decided to add a new Execute Sql Task just after the sequence container, as bellow

The principal mission of that Execute Sql Task is to drop the created data base if it already exists. The SQL statement that I add to that  task is as follow

DECLARE @dbase_id  int;
SET @dbase_id @dbase_id = DB_ID(N'FIRM'); 
IF @dbase_id  IS NOT NULL  --The data base name is FIRM
BEGIN
DROP DATABASE FIRM
END
GO


Of Corse, the constraint has to be changed to failure. As we need to drop the data base only when the  sequence container is failed.
Hope that helps to tweak arround the problem.

1 comment: