create flat file in ssis package - sql-server

I'm working on creating a csv export from a SQL Server database and I've been familiar with a process for doing so that admittedly, I've never completely understood. The process involves creating a "template" file, which defines the columns and structure for the file export. Once the "template" file exists, you can use a Data Flow task to fill it and a File System Task to copy it to the final storage destination with whatever file name you'd like (frequently a date/time stamp).
Is there a reason that you can't simply create a file directly, without the intermediate "template" file? I've looked around for a bit and it seems like all the proposed solutions involve connecting to an existing file. I see that there is a "Create File" Usage type for a "File" connection manager, but you can't use it in any File System Task. The only File System Type connection managers you can use relative to a file are "Copy", "Delete", "Move", "Rename", and "Set Attributes".
Is there a way to create a file at package run time and fill it?

The whole point of SSIS is to create a data flow with metadata so that the data can be manipulated - if you just want to go database direct to CSV you are probably better off using bcp (bulk copy program) from the command line. If you want to include it as part of a SSIS package just add an Execute Process Task and add the command line to that. You can dynamically change the included columns or the output file by adding an expression to the task. You could also call bcp though TSQL using an Excute SQL Task.
One other option is to concatenate all your columns in your query inter-spaced with a comma literal and output to a text file with just one very wide column.
For documentation on bcp look here

Related

How to create separate transformed file instead of overwrite ssis output flat file?

I have a dataflow that is used to do transformation of multiple flat files from given folder using for each loop container. I have a flat file again as output file. The problem is that every time I execute the the job only the last file that got transformed will be stored in destination file.
Is there a way in SSIS I can create individual transformed output file instead on overwriting on same one over and over again?
For. eg. I have 5 flat files ,test_1.txt,test_2.txt,test_3.txt ,test4_.txt
and test_5.txt in a folder.
After the job ran I can only see the data from last file test_5.txt being
transformed in my destination file.
Here's steps on a working example I tested.
Variables
I have 3 variables defined:
FileName - To be used in the foreach loop
DestinationDir - where are the files going
SourceDir - where are the files I want to process
Foreach Loop Setup
I have a foreach loop configured as:
Expression for "Directory" set to #[User::SourceDir]
Retrieve file name set to "Name and extension"
Then under the "Variable Mappings":
That means as the foreach loop is iterating over the files in the directory it will be setting the "Name and extension" of the file its on to the variable #[User:FileName]
Data Flow Task
The I add a Data Flow Task inside the foreach loop:
Then inside the DFT I have a simple Flat File Source to Flat File Destination. We'll just pass the contents of each file to new files:
During initial development I'll manually pick one file to walk through setting each of the source and destinations. Then come back and change the connection managers and set an expression on the ConnectionString.
Connection Manager Expressions
SourceFile Connection Manager:
ConnectionString gets an expression as: #[User::SourceDir] + #[User::FileName]
DestinationFile Connection Manager:
ConnectionString gets an expression as: #[User::DestinationDir] + #[User::FileName]
Testing
I have 2 test files in my source directory and no files in my destination:
After I execute my package I get success and also get new files in my destination:
There are ways to do what you are asking in SSIS with variables and expressions but there is an easier way to accomplish it using command line.
Since you are just consolidating a text files into 1 you can use a command prompt to better handle your issue:
copy *.txt output.txt

SSIS Creating a multiline DOS file

I have to create a multiline DOS batch file in SSIS that looks like this:
cd e:\client\data
copy generic_output.txt clientname.yyyymmdd
where yyyymmdd is today's date
I have created quite a few single line DOS copy statements which do the copy (using a Derived column in the Data Flow) but can't come up with an approach that will allow me to create a template file that contains the (hardcoded) first line and then have a Data Flow task which writes out the first line and then have the Derived column be the second line.
Can I write out both commands in the Derived Column and put in a CR-LF in between the cd command and the copy command? Or is there a better approach?
I figured out a way that works. The cd command is fixed so I put that into a file. Then I used a Data Flow to build the COPY command in another file. Then I set up a copy.bat file to copy the 2 files, and then finally an Execute Process task to execute the final batch file. It works.
Thanks,
Dick

UNIX Shell script: file reading issue

I have to read a file in my shell script. I was using PL/SQL's UTL_FILE to open the file.
But I have to do a new change which will append timestamp to the file.
e.g import.data file becomes import_20152005101200.data
Now timestamp is the time at which file arrive at the server.
Since the file name changed I can't use the old way of file accessing.
I came up with below solution:
UTL_FILE.FOPEN ('path','import_${file_date}.data','r');
To achieve this I have to get filename and trim it using SUBSTR to get timestamp and pass to file_date variable.
However I am not able to find how to access filename in a particular path. I can use basename. But My file name keeps changing because of timestamp.
Any help/ alternate ideas are welcome.
PL/SQL isn't a good tool to solve this problem; UTL_FILE doesn't have any tools to list all the files in a folder.
A better solution is to define a stored procedure which uses UTL_FILE and pass the file name to process as an argument to the procedure. That way, you use the shell (which has many powerful commands and tools to examine folders and files) or a script language like Python to determine which file to process.

Why doesn't my SSIS File System Task recognize my destination path?

I've set up a File System path inside a ForEachFile enumerator in SSIS 2012. I'm iterating over a directory, loading each file, archiving that file, then processing the next file, etc. I've set the Destination folder via an expression that uses a Project Param value, and I get the source file from the variable set in the ForEachFile enumerator. The File System task says it can't find my Destination folder:
Here's the File System Task:
And proof that the destination folder exists:
Why am I getting this error? I'd swear I've used the exact same technique in SSIS 2008 and 2005. This is 2012, but it should work the same way.
I met the same problem and to resolve it i create a variable and i put something like this in the path.Be carefull you must put double "\ \".
So the path must be like this:"\ \ \ \ad1hfdalhp001\ \d$\ \data\ \Archive\ \"
The DestinationConnection field must contain a reference to a flat file connection. The error is saying that you have no Flat file connection manager with that name.
DestinationConnection should not be a free text field. You should be able to open a drop down in the DestinationConnection field and select or create a connection manager.
You will need to configure your output file path as the ConnectionString property on the file connection manager referenced in the DestinationConnection field.
Using a UNC is still an option. Similar to as you've done with the Source, on your Destination, set IsDestinationPathVariable = True and then push \server\path into a Variable User::ArchivePath or similar.
Otherwise, it is as user3922917 indicates: if IsDestinationPathVariable is false, then you need to use a File Connection manager.
In your comments, you indicate that you're building the UNC path based on an Expression in the File System Task. I find I have a better experience when I build my expressions in SSIS Variables and then simply assign that Variable into the Task's Expression. While this step may seem to provide another layer of maintenance, put a break point on the Task and tell me what the expression evaluates to. And you can't. It's only available to the object to use and you are unable to inspect it so you're left high and dry if your formula is off. Which never happens when you're having to deal with escaping a UNC path

Is it possible to open a flatfile in PLSQL when only a partial filename is known?

Is it possible to open a flatfile when only part of the file name is known?
I have files in a directory that have a timestamp appended to the filename, is it possible to open it by specifiying the known part of the filename (excluding timestamp)?
Is it possible with a PLSQL only approach?
There is a dbms_ package which allows you to get a directory listing for the directory (or you can implement your own in a java stored procedure - google!) This will allow you to find the file you are looking for - if necessary choose which is the relevant file and then process.
See http://notdennis.wordpress.com/2013/07/03/listing-directory-files-plsql/

Resources