Good Friday Readers,
So I got another good challenge at work the other day and thought I would share what I learned in hopes it would help you out. I did lots of research on figuring out how to do this.
What did I need to do?
We have multiply databases on a server that have same views on them. The databases are based on location and the view is filtered on location. Each view is the same so when we update a view we have to update all the views. It's not effiecent to run the alter view command on each database, but rather have loop that automatically does it. We tried by creating a dynamic loop in sql with a cursor but that was not working. So I thought of creating an SSIS package to do! What a great and simple way to do it.
We have multiply databases on a server that have same views on them. The databases are based on location and the view is filtered on location. Each view is the same so when we update a view we have to update all the views. It's not effiecent to run the alter view command on each database, but rather have loop that automatically does it. We tried by creating a dynamic loop in sql with a cursor but that was not working. So I thought of creating an SSIS package to do! What a great and simple way to do it.
What do you need?
Here are the tables, data and view sql commands to get started. I have two databases mlb_Brewers and mlb_Twins.
Brewers Table Script:
USE mlb_Brewers
CREATE TABLE Players( P_ID INT NOT NULL PRIMARY KEY IDENTITY , LastName VARCHAR(255), FirstName VARCHAR(255) ) INSERT INTO Players VALUES ('Braun','Ryan')
INSERT Players VALUES ('Hart','Corey')
CREATE VIEW v_Players AS SELECT LastName + ' , ' + FirstName AS PlayerName FROM PlayersTwins Table Script:USE mlb_TwinsCREATE TABLE Players( P_ID INT NOT NULL PRIMARY KEY IDENTITY , LastName VARCHAR(255), FirstName VARCHAR(255) ) INSERT INTO Players VALUES ('Mauer','Joe')INSERT Players VALUES ('Revere','Ben')CREATE VIEW v_Players AS SELECT LastName + ' , ' + FirstName AS PlayerName FROM Players Sweet now we have our tables and views set up! Let's query the views quickly to make sure it looks how we wanted it.USE mlb_BrewersSELECT * FROM v_PlayersUSE mlb_TwinsSELECT * FROM v_PlayerSo our view works. Now onto the fun with SSIS. This is a preview of the entire package.Step 1. Set Up Variables
- Code - String Type
- CurrentDatabase - String Type
- DatabaseList - Object Type
Step 2. Setting up ConnectionsWe need to set up the following two database connections.
- Master Database on the Server
- Variable driven database on the Server
The variable driven database is where all the magic happens. We create a normal database connection, but change the expression afterwards (located in the properties of the data source) to our variable CurrentDatabase. Look at the screen shows below for reference.Step 3. Script TaskWe need to get the alter statement. I create the package to grab this command from a text file. The reason I do this is so you can quickly change the alter statement without having to go into the package. I use a script task with a read only variable of Code. We will assign the command in the text file to the this variable.The Code to assigning the value of the text file to the variable is as followed.Public Sub Main() ' ' Add your code here ' 'initialize StreamReader class for text file Dim streamReader As New IO.StreamReader("C:\SQLCode.txt") ' Read the StreamReader To End and assign to local variable Dim StreamText As String = streamReader.ReadToEnd() 'making sure there is code in the text file before going any on. If StreamText.Length < 1 Then ' failing the package Dts.TaskResult = ScriptResults.Failure Else ' assign SSIS variable with value of StreamText local variable. Me.Dts.Variables("Code").Value = StreamText ' going onto the next step Dts.TaskResult = ScriptResults.Success End If End SubStep 4. Grab the database listNow we need to grab the list of databases we want to through in the SSIS Package. We can just query the master database to get the list of databases as seen below.In SSIS the next task we create is the Execute SQL task editor. The result set will be a full result set. We then have the result set applied to the variable of DateabaseList. As seen the screen shots below.Step 5. Loop Through databasesNow that we have a a list of databases in the variable. We can create a for each loop and loop through that within in SSIS. We need to create the enumerator for a type of Foreach ADO Enumerator and the mode is rows in all the tables as seen on this screen shot.We need to map the variable to the loop. To do this we just add the variable of DatabaseList to the variable mappings screen.
Step 6. Grab Current Database
Now that we can loop through all the selected databases. We have to know which database we are currently on so our dynamic connection from earlier will work correctly. To apply the current database to the variable. We use some sql magic.Select Statement:SELECT name FROM sys.databases WHERE name = ?Parameter Mapping: DatabaseList variableResult Set: CurrentDatabaseStep 7. Run SQL CommandWe are finally at our last step in this process. We want to run the custom code in that text file to the current database we are on in the loop. Datasource: (local).currentdatbase - This is the database source that use an expression as the database name linked to the variable. Parameter Mapping: User:Code - This is the variable we created that contains the command from the text file.
Step 8. Run SSIS PackageNow the let's see if this works. Let's run the SSIS Package.Looks like it worked! Congrats!I hope this little blog helped you out. With SSIS the possibilities are endless...Till next time,Robert















