I am trying to create an SSIS package for Stored Procedure Deployment and Backup for our project.
I have some .sql file, each file contains one stored procedure definition and the name of the file is the stored procedure name itself. I am trying to do the following by using SSIS
I am able to generate the desire .sql files, but I am faceing the following problem 1. The package also generated unwanted blank .sql file for all new Stored Procedure 2. The execution process failed if the stored procedure has some dependency on subsequent stored proc
In this answer, I will provide the main steps with some references to get more information on how to achieve each step. Even if I agree with the comments mentioned above that this is not the job of SSIS.
Add a foreach loop container that loop over .sql files and store the file name inside a variable:
Add an Expression Task to retrieve the file name from the File Full Path (variable)
@{User::FileNameWithoutExtension] = SUBSTRING (@[User::FullFilePath], LEN( @[User::FullFilePath] ) - FINDSTRING( REVERSE( @[User::FullFilePath] ), "\\", 1) + 2, LEN (RIGHT( @[User::FullFilePath], FINDSTRING( REVERSE( @[User::FullFilePath] ), "\\", 1 ) - 1 ) ) - FINDSTRING( REVERSE( @[User::FullFilePath] ), ".", 1 ) )
Add an Execute SQL Task inside the foreach loop container to check if the stored procedure is found in the database:
SELECT COUNT(*) FROM sys.objects WHERE type = 'P' AND name = ?)
Pass the procedure name as parameter to the execute sql task:
Store the count result inside a variable of type integer using Resultsets:
Using precedence constraints with expressions add 2 paths from the execute sql task
@[User::Count] == 0
@[User::Count] > 0
Other references:
Defining Workflow in SSIS using Precedence Constraints
On the second path add an Execute SQL Task to get the procedure definition using the same approach above:
SELECT OBJECT_DEFINITION (OBJECT_ID(N'<databasename>.<schemaname>.' + CAST(? as VARCHAR(100))));
And store the result inside a variable using a result set.
Add a Script Task to write the procedure definition into the destination file
On the first path add a File system task
to move the file into the directory specified
Add another foreach loop to read new files and execute the content.