Abstract
This article explains how to extract SQL Statements found in Execute SQL Tasks of an SSIS package and save them into a delimited text file using C# programming language.
Requirements
Article
This article continues from another article which can be found here.
Launch Visual Studio 2008 and create an Integration Services Project. After the default (new) package has launched, drag a script task to the control flow pane.
Right-click to edit the script task. In your Script Task Editor ensure that you have selected Microsoft Visual C# as your programming language.
At the bottom of your Script Task Editor, click “Edit Script”.
Add the following namespaces:
using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using Microsoft.SqlServer.Server;
using Microsoft.SqlServer.Dts.Tasks;
using Microsoft.SqlServer.Dts.Tasks.ExecuteSQLTask;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using RuntimeWrapper = Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.IO;
Proceed to load the package (that contains the Execute SQL Tasks you would like to extract and save).
Application app = new Application();
Load package
Package p = app.LoadPackage(“C:\\\\TEMP\\\\pkg_Execute_Sql_Tasks.dtsx”, null);
Declare variables that will be used to store extracted queries:
//string declare delimiter
string del = “|”; // You can also change it to tab (^) comma (,) etc
string src_query = “”;
string sql_task_name = “”;
Int32 i = 1;
Create a text file as follows:
FileStream fileStream = new FileStream(@”c:\\\\TEMP\\\\selectSIFISO_file.txt”, FileMode.OpenOrCreate);
StreamWriter streamWriter = new StreamWriter(fileStream);
streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);
streamWriter.WriteLine(“src_query” + del + “sql_task_name”);
The rest of the code is as follows:
foreach (Executable executable in importPackage.Executables)
{
DtsContainer container = (DtsContainer)executable;
if (executable.GetType().Name == “TaskHost”)
{
i = i + 1;
TaskHost loop = (TaskHost)executable;
ExecuteSQLTask sqlTask = (ExecuteSQLTask)loop.InnerObject;
src_query = sqlTask.SqlStatementSource;
sql_task_name = container.Name;
string source = src_query;
string[] stringSeparators = new string[] { “GO” };
string[] result;
result = source.Split(stringSeparators, StringSplitOptions.None);
foreach (string s in the result)
{
// Insert new records into the workbook
streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);
streamWriter.WriteLine(s + del + sql_task_name, result);
}
}
}
streamWriter.Flush();
streamWriter.Close();
Dts.TaskResult = (int)ScriptResults.Success;
We then save the package in a file system.
SIFISO_app.SaveToXml(“C:\\\\TEMP\\\\pkg_Execute_Sql_Tasks.dtsx”, dyna_pkg, null);
Conclusion
It’s that simple!
You can now execute your script task and the package will be created in the location you specified.
greetings, awesome page, and a very good understand! just one for my bookmarking.
Thanks, Dodie
I dunno if it is a problem in my end or on your end but I cant see any pictures on your site… All I get is like little red crosses. That means that the image is gone right?
please send me the page link
What service do you use for RSS feed? Is it feedburner? I ask because I cant figure out how to install a RSS service on my Blogger blog. I wouldn’t get any of that techy stuff if my life would depent on it :s So if you use a RSS feed service please let me know. Thanks! Jen
I truly appreciate this post. I’ve been looking everywhere for this! Thank goodness I found it on Bing. You’ve made my day! Thanks again!
my pleasure
I don’t have the habbit of just randomly posting a comment on your blog but I found that for some reason I have trouble loading this page. Not the homepage but as far as I can tell only http://www.blogs.selectsifiso.com/?p=284. I just thought you should know. Regards, Grace
thanks, grace. can you try again
Hey There. I found your blog using msn. This is a really well written article. I’ll make sure to bookmark it and come back to read more of your useful information. Thanks for the post. I will certainly return.
I appreciate it, thanks Pattie
Hey, no matter what I do, this page (http://www.blogs.selectsifiso.com/?p=284) seems to load very slow for me. It is also just this one as far as I can tell so maybe it’s the pictures or something? Anyway just FYI 🙂 Regards, Joanne
I appreciate it thanks
Interesting post!
thanks
Good write-up, I am normal visitor of one’s site, maintain up the excellent operate, and It is going to be a regular visitor for a lengthy time.
looking forward to your valuable input
nice information!
I appreciate, many thanks
Usually I don’t comment that often but I wanted to let you knwo that there is something wrong with your comment field thingy. It keeps giving me a black page when I click on ‘Comment’ :s
I think other website proprietors should take this website as an model, very clean and wonderful user genial style and design, let alone the content. You are an expert in this topic!
It’s comments like yours that inspires this blog. I appreciate it, thanks.
I used to be recommended this blog by means of my cousin. I’m no longer positive whether this put up is written via him as nobody else recognize such special approximately my difficulty. You’re incredible! Thank you!
many thanks
Hi! I just want to tell you that I found this blog really useful to me. I will visit this blog regularly to read more. Thanks. srt1307955413 http://ur1.ca/4d0se
I appreciate it, thanks
A Great Ezine Website will help you with your marketing efforts if you understand article marketing properly.
Hello there, I found your web site by way of Google whilst searching for a related subject, your website got here up, it seems to be great. I have bookmarked it in my google bookmarks.
I appreciate it, many thanks
Nice read, I just passed this onto a friend who was doing a little research on that. And he just bought me lunch because I found it for him smile Thus let me rephrase that: Thank you for lunch!
lol, my pleasure
hello there and thank you for your information – I’ve certainly picked up something new from right here. I did however expertise several technical issues using this website, since I experienced to reload the web site a lot of times previous to I could get it to load correctly. I had been wondering if your web hosting is OK? Not that I’m complaining, but sluggish loading instances times will sometimes affect your placement in google and can damage your high-quality score if ads and marketing with Adwords. Anyway I am adding this RSS to my e-mail and can look out for much more of your respective exciting content. Make sure you update this again soon..
For some reason or another, I can’t see all of this text, stuff keeps hiding? Are you utilising something crazy?
not really, just wordpress
I just added your website on my blogroll. I may come back later on to check out updates. Excellent information!
I just added your website on my blogroll. Really enjoyed reading through. Excellent information!
Thanks for sharing superb informations. Your web site is so cool. I am impressed by the details that you have on this web site. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for extra articles. You, my friend, ROCK! I found simply the information I already searched everywhere and just could not come across. What an ideal web-site.
Just want to say your article is as amazing. The clarity in your post is just nice and i could assume you’re an expert on this subject. Well with your permission let me to grab your RSS feed to keep updated with forthcoming post. Thanks a million and please carry on the gratifying work.
Excellent post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work.
Fantastic website. Plenty of helpful info here. I am sending it to some friends ans also sharing in delicious. And naturally, thanks in your sweat!
Really interesting blog, keep up the good work!
I was just seeking this information for some time. After six hours of continuous Googleing, at last I got it in your web site. I wonder what’s the Google’s issue that does not rank this type of informative sites closer to the top. Usually the top sites are full of garbage.
Another quality post. I posted a link for this blog at my web site. So, I am sure several persons forget the points you are discussing.
Great goods from you, man. I’ve be mindful your stuff prior to and you’re just extremely great. I really like what you’ve received right here, really like what you’re stating and the way in which in which you are saying it. You’re making it entertaining and you continue to care for to stay it sensible. I can’t wait to learn much more from you. This is actually a terrific web site.
Hello There. I found your weblog the usage of msn. This is an extremely smartly written article. I?ll make sure to bookmark it and come back to learn more of your useful information. Thanks for the post. I?ll certainly comeback.
pleasure
Hello there, I found your web site by the use of Google at the same time as searching for a related topic, your website came up, it appears to be like good. I have bookmarked it in my google bookmarks.
Wow, awesome blog structure! How long have you ever been running a blog for? you make running a blog look easy. The entire glance of your web site is great, as smartly as the content material!
thank you
Hi there very nice site!! Guy .. Excellent .. Wonderful .. I’ll bookmark your website and take the feeds additionally?I am satisfied to search out a lot of helpful information here in the publish, we need work out more strategies in this regard, thanks for sharing. . . . . .
My brother recommended I would possibly like this web site. He was once entirely right. This put up actually made my day. You cann’t believe simply how much time I had spent for this info! Thank you!
Someone essentially help to make severely articles I would state. This is the first time I frequented your web page and thus far? I amazed with the research you made to make this actual post incredible. Wonderful job!