dynamically generate file with last day of previous month in ssis - sql

I need to generate a file with last day of month using SSIS. I am getting it using SQL.
DECLARE #mydate DATETIME
SELECT #mydate = '03/21/2013'
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(#mydate)),#mydate),112)
--20130228
I dont know how to put it in SSIS expression in connection manager.
The file name will be like 'ABC_20130228.txt'

I would assign the date to a variable in SSIS, then pass that variable to a script task that builds the file name and puts that in an output variable. Then you can assign the output variable in the ConnectionString property under the Expressions property group in the properties for the Connection Manager for that output file to use the variable's value as your file name.
to Summarize:
Use a script task and pass the date to use in the file name to it as a read only variable as type string to make it easier to manipulate.
Assign a read/write variable to the script task to store your file name.
Create the file name in the script task.
Variables named: User::DataFile, User::MaxDate
ie String DataFileName;
String DateToUse;
DateToUse= (String) Dts.Variables["MaxDate"].value;
DataFileName="ABC_"+Convert.ToString((Convert.ToDateTime(DateToUse)).Year)+Convert.ToString((Convert.ToDateTime(DateToUse)).Month)+Convert.ToString((Convert.ToDateTime(DateToUse)).Day)+".txt";
Dts.Variables["DataFile"].value=DataFileName;
Assign the variable in the Expression sections of the properties for the Connection Manager to the CONNECTIONSTRING property.
That should do it. If you need a particular path, then that would be concatenated onto the property and variable in step 4.

Why use a script task? Why not use the expression described here:
Add datestamp to a .txt file in an SSIS Package?

Related

Empty variable in foreach loop in SSIS

In SSIS I'm using a foreach loop to browse zip in a directory. So I defined a variable called User::CurrentZip in the variable mapping window. So technically, I use this variable to manipulate the zip file (unzip, move..) in a C# script task and futhermore I can see the variable writing Dts.Variables["User::CurrentZip"].Value.ToString().
The problem is when I try to log the zip name in a SQL table using stored procedure the variable User::CurrentZip is empty. How could I use this variable as parameter in a stored procedure in a SQL Task ?

How to use a variable to look up in a foreach loop container -ssis?

I need to process all the files in a folder.
something like this:
foreach loop over n
fileprocess.exe -argument_n filename_n
each argument is file specific and will be retrieved from a table.
Need to design ssis package to batch process the files.
foreach loop seems perfect for it.
I'm thinking of using lookup transform to retrieve the corresponding argument.
My question are
how to feed the variable #[user::filename] to the lookup transform?
how to assign the lookup output into #[user::argument]
Wonder if lookup transform is the right one to use?
Thanks a lot!
Assuming that you have a table containing columns for file name and corresponding argument, one way to implement your requirement is as below:
Add components from below figure to the Control Flow.
In the Foreach Loop, Enumerator is set to Foreach File Enumerator. The files are read from a folder, but you could use any type of enumerator.
Create 2 variables in the scope of the Loop to hold the file name and arguments, say, fname and farg. In the Collections tab of Foreach Loop Editor, assign index 0 to the variable fname.
In the Execute SQL task, write a query to retrieve the arguments for a given filename. Assign variable fname as input parameter, set Result Set to Single Row, and assign the result to variable farg.
In the Execute Process task, pass the variables fname and farg as arguments for your executable.

how to store sql query result in a variable and messegeBox

I have a simple sql query in my Execute sql task in ssis package,
SELECT MAX(binindex)
FROM dbo.myTable
I need to store this maximum index into a variable and then pass it to Script Task and display it,
I already declared a package variable, the package compiles, however it shows -1 every time, I don't know what I'm doing wrong, any help will be appreciated!
public void Main(){
//TODO: Add your code here
Dts.TaskResult = (int)ScriptResults.Success;
MessageBox.Show(Dts.Variables["User::BININDEX"].Value.ToString());
}
The good news, is that you are doing everything correctly as far as I can see. I recreated your package and I get the expected value from my query.
I can also induce your situation - the correct value is returned from my query but my package produces an "incorrect result."
The problem, I hope, is that you have two BININDEX variables defined at different scopes. My original assumption was the Package scoped one contained a value of -1 and you had a variable scoped to the "Execute SQL Task" with the same name. The default behaviour is a variable is created scoped to the object that currently has focus. This changes in the 2012 release of SQL Server by the way.
As your picture shows a design-time value of 123 for the package scoped variable, the possibility also exists that you have a variable defined on the Script Task with the same name of BININDEX. The local variable would override the globally scoped variable
Click on your Script Task and hopefully you'll see a BININDEX defined there like the above. Otherwise, I think the problem is somewhere in your package, you have conflicting BININDEX variables. You can try slogging through the Package Explorer looking for an instance where you have two variables with the same name listed.
I need to leave but if none of that is the case, add a PostExecute breakpoint on the Execute SQL Task and look at your Locals window (not Variables as that only reflects Design-time values). Expand Variables and you should be able to see the value of BININDEX. Is it correct there?

SSIS set variable at runtime

Anyone know how I can change a SSIS variable at runtime?
I have a variable User:SkipStuff
I want to set this based on a condition during a for loop container
Use Expression Task instead. In the "Expressions" pane, put the variable to set on left, then equates it to the intended value.
#[User::VariableToSet] = some expressions...
You can add a Script task. Add your SSIS variable(s) to the ReadWriteVariables property in the Script Task Editor.
You can reference the variable(s) in your script using the following format: Dts.Variables["MyCaseSensitiveVariableName"].Value
Lots of ways really, but the one I use most frequently is to use an execute SQL task and set the result set to single row and then put the results set into the variable.

Changing an SSIS dts variables

In an SSIS package I have a For..Each container that enumerates all (.) the files in a folder.
In that For..Each container I have a component-level variable 'fileComments' of DataType 'String'.
In that For..Each container I have a script task. With a ReadWrite entry for 'filecomments' (amongst others)
In that script task, I have some code :-
Dim Comments As String = Dts.Variables("User::fileComments").ToString
which allows me to read the value of that variable, but if I try to allocate a value back to that variable, I get a Property 'Item' is 'ReadOnly' blue squiggly underline.
How do I change the vlaue of that variable (or get the value out of the script task so I can use it later in the flow) ?
Thanks in advance.
your problem is a common trap check this article out:
http://www.programmersedge.com/?p=1350
you basically need to use the Dts.VariableDispenser object to lock or declare the variable as read / write in the script task.