Eight ways of executing SSIS package and passing parameters (T-SQL, VB.net, C#, SQLAgent, DTExec)

Who is online?  0 guests and 0 members
Home  »  Articles  »  Eight ways of executing SSIS package and passing parameters (T-SQL, VB.net, C#, SQLAgent, DTExec)

Eight ways of executing SSIS package and passing parameters (T-SQL, VB.net, C#, SQLAgent, DTExec)

change text size: A A A
Published: 12/15/2009 by  NayanPatel  - Views:  [2849]  

This article will explain differnt ways of executing SSIS package in interactive mode and non-interactive mode from your T-SQL/VB/C# code

1. Executing SSIS package from SQL Agent Job ( Non-interactive mode )
2. Executing SSIS package using commandline - DTExec
3. Executing SSIS package using DTSExecUI.exe  ( Interactive mode )
4. Executing SSIS package from T-SQL Code ( using xp_CmdShell )
5. Executing SSIS package from .Net Application using SSIS API ( VB.net or C# ) 
6. Executing SSIS package from BIDS (Run in 32bit mode or 64Bit Mode)
7. Executing SSIS package from SQL Server Management Studio
8. Executing SSIS package on Remote Machine (Various Techniques)


There are many ways you can execute SSIS package. No matter what option you pick behind the scene SSIS Runtime will use DTExec commandline application to execute your package.

Note: For 64Bit Machine

If you have 64-Bit OS then you will have two version of DTExec installed. 32Bit and 64Bit

On 64bit Machine By default 64Bit version of DTExec will be executed. If you want to use 32bit version of DTExec then you will have to specify the different path for DTExec as below
C:\Program files (x86)\Microsoft Sql Server\90\dts\binn\dtexec.exe

Some times if you using 32-Bit Driver (e.g. MS Jet/Excel Driver, any ODBC Driver) in SSIS Package then you will have to call 32bit DTExec when calling it from commandline.

Executing SSIS package from SQL Agent Job (Non-interactive mode)

If you want to schedule SSIS package via SQL Agent Job then perform the following steps.


Create Job

1. Open SSMS (SQL Server management Studio)
2. Expand SQL Agent Node and go to Jobs Folder (Note: If SQL Agent Service is not running you will be prompted to start it)
3. Right click on the Jobs folder and enter Job name (For this example name it "TestSSISPackage")

Create Step

1. To create new step on the Job Dialogbox click on the "Steps" tab (Left side) and enter step name (For this example name it "Step-1")
2. Change step type to "SQL Server Integration Services Package"
3. Select Proxy Account. Proxy Account is very important to make sure your Job runs without any error. By default SQLJob Runs using credentials of SQL Agent  Service Account (Most of the times its Networks Services or Local System Account). If you are accessing file systrem or using windows authentication then you will need an Proxy account which has all necessary permissions to connect to database and read/write file system if needed. how to configure "Proxy Account" URL: http://msdn.microsoft.com/en-us/library/ms189064.aspx
4. When you change Step-Type to "SQL Server Integration Services Package" you will see many new tabs. On the general tab ... change the following items
5. Change Package Source to Location where your packages are stored (For this example Package is stored on File system) 
6. If you select SQL Server then you will have to enter Server/UserID/Password  and If you select SSIS package Store then only Server Name is needed becuase SSIS Package Store is only allowed for Authnticated Windows Account
7. And specify package path.
8. You can also pass variable values as below

Set Values


Scedule Job
http://msdn.microsoft.com/en-us/library/ms191439.aspx

Executing SSIS package using commandline - DTSExec

You can execute SSIS Package using command line by executing DTExec command.  DTExec is commandline application used to execute SSIS package. DTExec is found under the following path

C:\Program files\Microsoft Sql Server\90\dts\binn\dtexec.exe

DTExec Commandline


Examples:

-- Running package stored on File System
dtexec /F "C:\Package1.dtsx"

-- Running package stored on SQL Server (Mixed mode)
dtexec /SQL "\Package1" /SERVER PROD001 /USER sa /PASSWORD Pass01

-- Execute filesystem package and pass single variable value
dtexec /F "C:\Package1.dtsx" /SET "\Package.Variables[User::fileName].Properties[Value]";"CUST001.txt"

-- Execute filesystem package and pass multiple variable values
dtexec /F "C:\Package1.dtsx" /SET "\Package.Variables[User::fileName].Properties[Value]";"CUST001.txt" /SET "\Package.Variables[User::employeeID].Properties[Value]";9999

-- Running encrypted package (passing password to commandline)
dtexec /F "C:\Package1.dtsx" /DECRYPT mypass123

-- Set entire connection string property
dtexec /F "C:\Package1.dtsx" /CONNECTION MyConnection "\"Data Source=(local);Initial Catalog=Northwind;Provider=SQLNCLI.1;Integrated Security=SSPI;Auto Translate=False;\""

-- Set any property with double quotes (Example -  Varibale value  is  [Babies"R"Us]  )... If you have doublequotes in string then Replace it with \"
dtexec /F "C:\Package1.dtsx" /SET "\Package.Variables[User::companyname].Properties[Value]";"Babies\"R\"Us"

-- Execute file system package with logging , save log file to c:\log.txt
dtexec /F "C:\Package1.dtsx" /LOG "DTS.LogProviderTextFile;c:\log.txt"


Please visit the following URL to check all the options for DTExec
http://msdn.microsoft.com/en-us/library/ms162810.aspx


Executing SSIS package using DTSExecUI.exe  (interactive mode)

You can execute SSIS Package in interactive mode by right clicking on SSIS package and  launching Execute Package Utility. This untility allows you to pass parameters and set connection properties using GUI.

DTExecUI is usually located under the following path

C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\DtExecUI.exe

Execute Package from Windows Explorer

Execute Package Utility

Set Variable Values

Set Parameters

Executing SSIS package from T-SQL Code using xp_CmdShell

You can use xp_CmdShell stored procedure to call DTExec commandline application to execute SSIS package and pass commandline parameters as below.  For security reason by default xp_CmdShell is disabled when you install SQL Server but you can enable it using Surface Area Configuration tool or simply execute the following command in Query window. For more info visit http://msdn.microsoft.com/en-us/library/ms190693.aspx

Enable xp_Cmdshell

-- To allow advanced options to be changed.
EXEC Sp_configure 
  

  'show advanced options' ,1
GO
-- To update the currently configured value for advanced options. 
 
 
RECONFIGURE
GO
-- To enable the feature. Pass 1 to enable and 0 to disable xp_CmdShell. 
  

EXEC Sp_configure 
 
  'xp_cmdshell' ,1
GO
-- To update the currently configured value for this feature. 
  

RECONFIGURE
GO


Example: Executing SSIS Package using T-SQL Code, Pass multiple variables values.

DECLARE  @cmd VARCHAR   (1000)
DECLARE  @ssispath VARCHAR 
  
(1000)
DECLARE  @fileName VARCHAR 
  
(1000)
DECLARE  @maxrows VARCHAR 
(10)

SET @ssispath = 'C:\Package1.dtsx'
SET @fileName = 
'\\SRV1\CUST001.txt'
SET @maxrows = 
'5000'
 
SELECT @cmd = 'dtexec /F "' + @ssispath + '"'
SELECT @cmd = @cmd + ' /SET \Package.Variables[User::fileName].Properties[Value];"' + @fileName + '"'
SELECT @cmd = @cmd + ' /SET \Package.Variables[User::maxrows].Properties[Value];"' + @maxrows + '"'
EXEC MASTER..xp_cmdshell 
 
  @cmd

 

// Sample Command Line will be as below ....

dtexec /F "C:\Package1.dtsx" /SET \Package.Variables[User::fileName].Properties[Value];"\\SRV1\CUST001.txt"  /SET \Package.Variables[User::maxrows].Properties[Value];"5000"


Executing SSIS package from .Net Application using SSIS API (VB.net or C#  (Non-interactive mode))


Example: VB.net - Execute package and pass parameters

Dim pkgLocation As String = "c:\package1.dtsx" 

Dim pkg As Microsoft.SqlServer.Dts.Runtime.Package 
Dim app As Microsoft.SqlServer.Dts.Runtime.Application 
Dim pkgResults As Microsoft.SqlServer.Dts.Runtime.DTSExecResult
Dim vars As Microsoft.SqlServer.Dts.Runtime.Variables 

app = New Microsoft.SqlServer.Dts.Runtime.Application() 
pkg = app.LoadPackage(pkgLocation, Nothing) 


-- // OR //--
--Load Package from SQL Server --pkg = app.LoadFromSqlServer(\package1, "(local)", "sa", "mypwd01", Nothing)
vars = pkg.Variables vars("fileName").Value = "\\SRV1\CUST001.txt" vars("maxrows").Value = 5000 pkgResults = pkg.Execute(Nothing, vars, Nothing, Nothing, Nothing) If pkgResults = DTSExecResult.Success Then Console.WriteLine("Package ran successfully") Else Console.WriteLine("Package failed") End If


 Example: C# - Execute package and pass parameters

 

string pkgLocation = "c:\\package1.dtsx"; 

Microsoft.SqlServer.Dts.Runtime.Package pkg = default(Microsoft.SqlServer.Dts.Runtime.Package); 
Microsoft.SqlServer.Dts.Runtime.Application app = default(Microsoft.SqlServer.Dts.Runtime.Application); 
Microsoft.SqlServer.Dts.Runtime.DTSExecResult pkgResults = default(Microsoft.SqlServer.Dts.Runtime.DTSExecResult); 
Microsoft.SqlServer.Dts.Runtime.Variables vars = default(Microsoft.SqlServer.Dts.Runtime.Variables); 

app = new Microsoft.SqlServer.Dts.Runtime.Application(); 
pkg = app.LoadPackage(pkgLocation, null); 

-- // OR //--
--Load Package from SQL Server --pkg = app.LoadFromSqlServer(\\package1, "(local)", "sa", "mypwd01", Nothing); vars = pkg.Variables; vars("fileName").Value = "\\\\SRV1\\CUST001.txt"; vars("maxrows").Value = 5000; pkgResults = pkg.Execute(null, vars, null, null, null); if (pkgResults == DTSExecResult.Success) { Console.WriteLine("Package ran successfully"); } else { Console.WriteLine("Package failed"); }

  

Executing SSIS package from BIDS (32Bit or 64Bit Mode)

This is the most common method of executing SSIS package during development phase. You can execute package in BIDS couple of ways

Executing Entire Package

1. Select Package in Solution Explorer and Right click and "Execute"
2. Hit F5 to execute package

Executing only single task
1. Right click on any Task and click "Execute"

Running package in 32Bit Mode (Only applicable for 64Bit OS)
When you testing your packages on 64Bit machine inside BIDS then by default package is running using 64Bit version of DTExec commandline application. But if you wish to run package using 32 Bit version of DTExec then change the "Run64BitRuntime" to False as below. This setting is found under SSIS Project Property Dialogbox.

64Bit Setting in BIDS

Executing SSIS package from SQL Server Management Studio

In the management studio click on connect and select Integration Services.  Enter server name and connect. Expand "Stored Packages Folder" and you can right click and run any selected package.

SSMS Execute SSIS Package

Executing SSIS package on Remote Machine

IWhen you execute SSIS package without SQL Agent Job then package is loaded into local machine memory and all local resources are used to execute package... including Memory, Credentials to connect to datasources, Drivers etc. If you want to launch your SSIS package on remote machine then you can impement one of the following approaches.

1. Call Package via SQL Agent Job Programatically.
2. Call Package via Web Services Programatically.

Please read the following MSDN Article for more information

http://msdn.microsoft.com/en-us/library/ms403355.aspx




I hope you like this article. Plese feel free to leave your feedback at the bottom comment section.

Cheers!!!!

 
0
/5
Avg: 0/5: (0 votes)

Comments (13)

Peager
Peager said:
Thank you so much for publishing this article. This is EXACTLY the type of document we newbies need. Paul
2/10/2010
 · 
 
by
chimpoy
chimpoy said:
Hi, is it possible to run one ssis package concurrently using different variable value?
3/8/2010
 · 
 
by
nayanpatel
nayanpatel said:
Yes you can run many instances of SSIS Packages with different values. Basically SSIS Engine loads Package XML into memory and perform execution so its not like locking package until its done. Try creating few Jobs calling same package and you can test your scenario passing different values to each Package step.
4/7/2010
 · 
 
by
varunsp
varunsp said:

Hi,

 

Package.variables do not work in ssis 2008. The above code works for 2005 ssis. Can you tell the way we can pass variables in 2008?

 

Thanks,

varun

6/9/2010
 · 
 
by
nayanpatel
nayanpatel said:

Varun,

Not sure what you mean by Package.Variables do not work??? Can you explain more what you trying to do here? May be post code snippet so I can answer your question.

Thanks,
Nayan

6/9/2010
 · 
 
by
varunsp
varunsp said:

Hi Nayan,

I am trying to run ssis 2008 package from my aspx page made in c# code.

 

 SqlDataProvider ObjConn = new SqlDataProvider();
                string FolderPath = Server.MapPath("DesktopModules") + @"\FeedPackage\PackageData";
                string pkgLocation;// = FolderPath + FileName;
                Package pkg;
                string FilePath = FolderPath + @"\" + FileName;
                Microsoft.SqlServer.Dts.Runtime.Application app;
                pkgLocation = FilePath;
                app = new Microsoft.SqlServer.Dts.Runtime.Application();
                pkg = app.LoadPackage(pkgLocation, null);
                //Assiggning initial variable to execute package
                pkg.Variables["ConnectionString"].Value = ObjConn.ConnectionString;
                pkg.Variables["LocalFeedRootPath"].Value = FolderPath + @"\filepath\";
                Label2.Text = pkg.Variables["ConnectionString"].Value.ToString();
                pkgResults = pkg.Execute();

 

 

I am running above code in ssis 2008, but it says pkg.variables dnt exists. But in 2005 it was there pkg.variables. There may be different way of running ssis 2008 in c#, might be some syntax change.

 

Can you guide me in this.?

 

Or simply can you send me code of sample ssis 2008 made package which can be run through aspx page?

 

Thanks,

Varun

6/10/2010
 · 
 
by
Frederico_fonseca

Varun

try this way (untested)

vars = pkg.Variables; 
vars["ConnectionString"].Value = ObjConn.ConnectionString; 
vars["LocalFeedRootPath"].Value = FolderPath + @"\filepath\";               
pkgResults = pkg.Execute(null, vars, null, null, null)

10/10/2010
 · 
 
by
claude.leblanc

Hi,

in Visual Studio I used the variable System:UserName in my package to log the name of the user who runs the package into a log table.  That works well when I execute the package locally on the server.

But when I call the package from a .net application the package fails to execute !  I removed the variable and it works fine.

Is there a way to pass the username from the .net application ?

I want different users to upload excel files from the .net application.  It would nice to log who imported data with the time stamp

Thanks,

Claude

 

 

 

6/14/2011
 · 
 
by
lttula
lttula said:

One more way to do this is create a .bat file and put this command

dtexec /File C:\package1.dtsx

1.  To create .bat file open an note pad and put the above command and save as .bat

2.  once you have created the .bat double click on .bat file it will execute you package.

3.  You can call .bat file from task scheduler to schedule it.

6/21/2011
 · 
 
by
NayanPatel
NayanPatel said:

Sounds odd that System::UserName causing problem.

One more would be Pass your own variable callit varUploadUserName by reading currently logged usename

Then before you call execute package method set varUploadUser to wtever account is uploading

e.g.

pkg = ssisApp.LoadPackage(.... )
pkg.Variables("User::varUploadUser").value = <Current User ID>
pkg.Execute

 

7/13/2011
 · 
 
by
sternman
sternman said:
I have packages that needs to read from Excel 2003 files. This requires me to use the 32bit JET driver. I see that I can use dtexec to run my packages, but, I am building them into a coded framework. I have code that can run the package from C#, however, I get this error when I run the code. "SSIS Error Code DTS_E_OLEDB_EXCEL_NOT_SUPPORTED: The Excel Connection Manager is not supported in the 64-bit version of SSIS, as no OLE DB provider is available. Microsoft.SqlServer.Dts.Runtime.DtsError" Now the issue is obvious, in BIDS, I can specify to run in 32bit mode. And with dtexec it is assumed. How do I get my c# code to run the packages in 32bit mode? Thank you
9/27/2011
 · 
 
by
KeithHyer
KeithHyer said:
sternman, Not to step on NayanPatel's blog, but maybe this will help. When SQL Server installs on a 32-bit machine, it installs both the 32-bit and the 64-bit runtimes. Make sure that you're referencing the PATH to the 32-bit runtime's executable: :\Program Files(x86)\Microsoft SQL Server\100\DTS\Binn The 64-bit version will probably be in :\Program Files\Microsoft SQL Server\100\DTS\Binn ..assuming that you didn't change the default folders, of course.
9/27/2011
 · 
 
by
KeithHyer
KeithHyer said:
oops, that 2nd line should read, "..When SQL Server installs on a 64-bit machine, it installs.."
9/27/2011
 · 
 
by
  • Name:*
  • Email:*
  • Website:
Type the characters you see in the image: *

Most Recent Articles