Showing posts with label destination. Show all posts
Showing posts with label destination. Show all posts

Thursday, March 22, 2012

Easiest way to get a value in af file into a variable ?

I get a file with some key information delivered to an ftp destination each day along with some files containing rawdata.

The file is a csv file containing some short description of what is being delivered.

Numrows;pulltime;sourceinfo

25302524;25-01-2006;dssrv34

So the file has columndescription and 1 row with some information.

My question is, what is the easiest way to get those 3 informations into 3 variables ?

How about a Flat File Adapter into a script component in which you can load the values into the variables.

-Jamie

|||

Or you could do this: http://blogs.conchango.com/jamiethomson/archive/2005/06/15/1693.aspx

(I forgot I'd done this before)

-Jamie

|||Personally, I would create a script task that takes in a filespec variable and writes to three variables. Like so.

Dim strFileSpec as String = cstr(Dts.Variables("MyFileSpec").Value)

Dim sr As System.IO.StreamReader

Dim strVals() As String

' Check if file exists

If System.IO.File.Exists(strFileSpec) Then

' Open the stream reader

sr = New System.IO.StreamReader(strFileSpec)


' Check if not at end of stream

' Split the first line at the semicolons and put in string array

If Not sr.EndOfStream Then

strVals = sr.ReadLine.Split(";")

End If


' Close the stream reader

sr.Close

' Check if there are three variables,

' If so right to output variables

If strVals.GetLength = 3 Then

Dts.Variables("MyVar1").Value = strVals(0)

Dts.Variables("MyVar2").Value = strVals(1)

Dts.Variables("MyVar3").Value = strVals(2)

End If


End If

Larry Pope

Monday, March 19, 2012

Dynamically selecting table in DTS

Hello,

I have to design a DTS package (not SSIS ) in which i want to select the destination table dynamically. Can any one help me out.

Thanks

MV

Are you trying to do this in a bulk insert task or data pump? If you're using the bulk insert task, you can set a connection and the properties of the bulk insert task to be dynamic by using the dynamic properties task. If you're using the data pump, it's slightly more complex. You'll have to use an ActiveX Script task to also set the properties of the destination table to concatenate whatever database you'd like to load to the table name. It's been a while since I looked at the code but it isn't too bad (about 4 lines or so). OR, you can just convert to SSIS :).

Brian

|||

Thanks

i got the way

Dynamically pick the source and destination tables

I want to write a SSIS which picks up the source and destination tables at runtime is it possible. As we have a SSIS which is used to pull data from oracle but the source and destination table name changes.

If the metadata changes (that is the column names change and/or data types) then you cannot do this without manually accounting for the differences.

If the structures are the same, then you can build SQL statements to select against the appropriate table name. You can build the SQL in a variable expression.|||

Would you please give an example for this.

|||A little bit of searching will help you out...

I found: http://blogs.conchango.com/jamiethomson/archive/2005/12/09/2480.aspx|||

All the source tables have different data type and columns, so I think it is not possible to have 1 common SSIS for all of them.

We are storing our packages under the FileSystem on the server and executing them via jobs. So my question is if we make the changes in the package in BIDS(our Solution file) will it be reflected in the job or we'll have to import the package in File System?

|||

Paarul wrote:

All the source tables have different data type and columns, so I think it is not possible to have 1 common SSIS for all of them.

We are storing our packages under the FileSystem on the server and executing them via jobs. So my question is if we make the changes in the package in BIDS(our Solution file) will it be reflected in the job or we'll have to import the package in File System?

If you edit the package that's being referenced in the job, then the changes will be picked up on the next iteration.

Sunday, March 11, 2012

Dynamically create text file as destination from sql script in SSIS

I have a select Script as follows:

SELECT c.ABC AS 'ABC'

, a.Qty AS 'Quantity_Recived'

, b.PC AS 'PC'

, b.PC AS 'PC'

, 'I' AS 'Flag'

FROM TNRInventory.dbo.tInventoryAlloc AS a

LEFT OUTER JOIN vwInventoryAllocMapping AS vwMap ON a.TNRAllocTypeID = vwMap.TNRInventoryAllocID

LEFT OUTER JOIN ABC.dbo.ZREFRESHTAB AS b ON a.DispenserID = b.Asset

LEFT OUTER JOIN ABC.dbo.TableJoinKey AS c ON a.TitleID = c.TITLE_ID

WHERE (vwMap.DataSourceID = 3) and vwMap.[DataSourceAllocName] = 'I'

group by c.SKU_NO , vwMap.[DataSourceAllocName],a.Qty , b.Profit_Center

order by c.SKU_NO,vwMap.[DataSourceAllocName]

GO

i have to send the result of aforesaid script in batch of 300 records per file (tab delimited text file)
now the file name must be dynamically created as each file will contain 300 records.

I have found some document related to same issue on this url

http://forums.microsoft.com/TechNet/ShowPost.aspx?PostID=1238184&SiteID=17

but still there is a catch.

Can any one guide/suggest me better way to do the aforesaid.

Thanks

Thinking while typing, this could be done with a few steps.

1 - Data Flow - Load a staging table with the results of the SQL. Add to it a row number counter so that every row is numbered uniquely.

2 - Control Flow - Run an Execute SQL Task to select max(rownumber) from that staging table

3 - Control Flow - Use a script task to take the output of the Execute SQL task and populate another variable with the number of iterations needed to populate files with up to 300 rows. (max(rownumber) / 300 - if no remainder, use that value, if remainder add one to the integer, etc...)

4 - Control Flow - Use a for loop to iterate the output variable from step 3 above.

5 - Control Flow - Build a variable set to an expression to use a base filename and the variable from step 3 above.

6 - Control Flow For Loop - Loop through the variable from step 3 above and add a data flow. Inside this data flow, use an OLE DB (or whatever) to connect to the staging table from step 1, filtering by (variable_step3 * 300) you can select just the records for this iteration. Hook that up to a flat file destination, which uses expressions to set the file name equal to the variable from step 5 above.

Something like that.|||

You might also check Jamie's blog post on this topic.

http://blogs.conchango.com/jamiethomson/archive/2005/12/04/SSIS-Nugget_3A00_-Splitting-a-file-into-multiple-files.aspx

Essentially the same technique that Phil recommended, but he's got some sample code already

|||

jwelch wrote:

You might also check Jamie's blog post on this topic.

http://blogs.conchango.com/jamiethomson/archive/2005/12/04/SSIS-Nugget_3A00_-Splitting-a-file-into-multiple-files.aspx

Essentially the same technique that Phil recommended, but he's got some sample code already

D'oh! Should've just looked there first!|||

What i did is i create a view for aforesaid script and then used that script in the following VB.net script

' Microsoft SQL Server Integration Services Script Task

' Write scripts using Microsoft Visual Basic

' The ScriptMain class is the entry point of the Script Task.

Imports System

Imports System.Data

Imports System.Math

Imports Microsoft.SqlServer.Dts.Runtime

Imports System.Data.SqlClient

Imports System.Text

Imports System.IO

Public Class ScriptMain

Public Sub Main()

Dim ConnectionString As String = "Server=ServerName;Database=DatabaseName;uid=Login;pwd=Password;"

Dim querystring As String = "SELECT * FROM vwInventory_I"

Dim con As New SqlConnection(ConnectionString)

Dim adapter As New SqlDataAdapter()

Dim ds As New DataSet

Dim dt As New DataTable

adapter.SelectCommand = New SqlCommand(querystring, con)

adapter.Fill(ds)

dt = ds.Tables(0)

CreateFile(dt)

End Sub

Private Sub CreateFile( ByVal table As DataTable)

Dim intRowCount As Integer = table.Rows.Count

Dim Column1Value As String

Dim Column2Value As String

Dim Column3Value As String

Dim Column4Value As String

Dim Column5Value As String

Dim sb As New StringBuilder

Dim seperator As String = vbTab

Dim nFileNameCount As Integer

nFileNameCount = 0

'For i As Integer = 0 To table.Columns.Count - 1

Dim i As Integer = 0

Dim j As Integer = 0

For j = 0 To intRowCount - 1

If nFileNameCount = 100 Then

PrintFile(sb, j)

'clears the stringbuilder

sb.Remove(0, sb.ToString.Length - 1)

nFileNameCount = 0

End If

Column1Value = table.Rows(j)(i).ToString

Column2Value = table.Rows(j)(i + 1).ToString

Column3Value = table.Rows(j)(i + 2).ToString

Column4Value = table.Rows(j)(i + 3).ToString

Column5Value = table.Rows(j)(i + 4).ToString

sb.Append(Column1Value & seperator & Column2Value & seperator & Column3Value & seperator & _

Column4Value & seperator & Column5Value & seperator & vbCrLf)

nFileNameCount = nFileNameCount + 1

Next

PrintFile(sb, j)

'Next

End Sub

Private Sub PrintFile( ByVal sb As StringBuilder, ByVal recordcount As Integer)

' Create an instance of StreamWriter to write text to a file.

Dim strDate As String

strDate = String.Format("{0:yyyy}" , DateTime.Now)

strDate = strDate & String.Format( "{0:MM}", DateTime.Now)

strDate = strDate & String.Format( "{0d}", DateTime.Now)

Using sw As StreamWriter = New StreamWriter("C:\SSIS\I_" & strDate & "_" & recordcount & ".txt")

sw.WriteLine(sb.ToString)

sw.Close()

End Using

'this will create a text file in bin directory

End Sub

End Class

This is how you generate tab delimited files to test check Use Script Tas from SSIS

|||Uh, okay. Good. So why even use SSIS then? Write your own program as you have done, compile it, and execute the resulting binary file. Leave the bloat of SSIS out of it.|||

I am going to put this piece in middle of my design.

It was the one of the key to finish my jik-so-puzzle.

Other one is to delete the files from Unix Aix Server with FTP connection.

And here too I have to go all the way round, as there FTP connection works fine on Microsoft server but not on UNIX server.

It does not allow you to delete the files on UNIX server.

That is the reason I am using the code in SSIS.

Dynamically create text file as destination

I am trying to create a text file from an SQL query on a SQL table. I would like the SSIS package to prompt for the file name and path. The text file is tab delimited and the text qualifier is a double quote.

Thanks,

Fred

SSIS by itself won't be able to prompt you. You'll have to write a custom executable perhaps to get this to work.|||

I think you could use the Windows.Forms.SaveFileDialog object in a Script Task.

|||

Thanks for the suggestions but I am new to programming. I have "played" a little with VBA in Excel. Can anyone get me started with some more VB code?

I would think this has been done before but I can not find any code in any SSIS forum for it.

Thanks,

Fred

|||

Couldn't you create a package variable called FilePath (for example) of type string and then read into it through a script task?

Something like the following would give you an input prompt and then store the value into the variable created:

Public Sub Main()

Dts.Variables("FilePath").Value = InputBox("Enter your file path", "Prompt").Trim()

Dts.TaskResult = Dts.Results.Success

End Sub

Then, you could just set the ConnectionString property in Expressions to the FilePath variable.

|||

SaveFileDialog would give the same result than InputBox except you are able to browse...

Dim fSaveFileDialog As New Windows.Forms.SaveFileDialog
fSaveFileDialog.ShowDialog()
Dts.Variables("fileName").Value = fSaveFileDialog.FileName

|||

Thanks for the suggestions but I am still lost.

I have a Data Flow Task which has a SQL Server Source. In the Data Flow Task I connect the SQL Server Source to what I believe is next - a Destination Script Component. I did not see anywhere you code put code in a Data Flow Destination - Flat File Destination.

In the Script Component it allows you to add code in Script Design box which is Visual Studio's designer window. How do I assign the filename to the new file I create?

Thanks for any help.

Fred

|||

Use expressions to assign the filename to the Flat File Destination (property Connection = User::variable)

Add a Script task before your data flow task. This script task will prompt for the file location and set the User::variable.

|||

Can I just question this approach? Is this for end users? If so then I don’t think using SSIS in this way is appropriate. SSIS is a server, so it is licensed as part of the full SQL Server license, and is not something you can install on client desktops as part of the normal Client access License. Each machine needs a full SQL Server license. To have a task offering a UI to the user means that package, and therefore SSIS itself, must be installed on the user's machine, making each user machine a full SSIS server install.

Friday, February 24, 2012

Dynamic table name in destination

How to create a new table dynamically in OLE DB destination.

This is what i am doing

I am reading multiple flat files in loop and saving file name to a variable. Then i have a source script component which read and transforms data .Now how can I push the data to SQL table. I want to create a new table with name saved in a variable. I tried using OLE DB destination and assigning table name from variable. Does'nt work.

Thanks in advance for any insight on how to make this work.

-Amar

I think you'll have to break this up into multiple work flows. The first executes an Execute SQL task in the control flow to create the table. Then attached to that Execute SQL Task is the data flow that operates on the file and then inserts into the destination, which is derived from the incoming table name.

That may work for you. The problem is, the OLEDB Destination cannot create a table at run time.|||

Ok i broke into multiple work flows. Now i read file names and then execute a script task to create table(it worked). Then read and transform data..but how to attach destination to it or how to insert into SQL table.

Can you please throw me some example.

-Amar

|||

Amar Khaira wrote:

Ok i broke into multiple work flows. Now i read file names and then execute a script task to create table(it worked). Then read and transform data..but how to attach destination to it or how to insert into SQL table.

Can you please throw me some example.

-Amar

In the OLE DB Destination, set it to "Table name or view name variable - fast load". Then just pick the variable that you loaded in the foreach loop. The metadata must be the same, though, for each table.|||

I am reading files in ForEachLoop container and creating tables(works fine) but how to tell the OLE DB destination about table because the table does not exist yet. It will be created during runtime. OLE DB Destination needs to map the columns...in order to insert..that's why it does not work...any workaround for that.

-Amar

|||

Phil,

I tried that I am getting "Object does not exist in database" which i understand that table will be created a runtime but not there yet. So OLE DB does not know what to map.

-Amar

|||

Amar Khaira wrote:

I am reading files in ForEachLoop container and creating tables(works fine) but how to tell the OLE DB destination about table because the table does not exist yet. It will be created during runtime. OLE DB Destination needs to map the columns...in order to insert..that's why it does not work...any workaround for that.

-Amar

You need to create one table first. Then populate the variable that you are using in the foreach loop with a default value of that table you just created. Then in the OLE DB destination, in selecting that variable, it will read it and find the table. You can then perform your mappings. When you execute the package, the default value of the variable will be over-written.|||

Amar Khaira wrote:

Phil,

I tried that I am getting "Object does not exist in database" which i understand that table will be created a runtime but not there yet. So OLE DB does not know what to map.

-Amar

See my comment above.|||

Phil,

That what I did...can you please elaborate more...what i am missing

|||

Amar Khaira wrote:

Phil,

That what I did...can you please elaborate more...what i am missing

You need to create a table first. Then, in the variable you are using, TYPE in the name of that table in the DEFAULT VALUE parameter of that variable.

Then in the OLE DB Destination, do as I said above. Select that variable, and it should work for you.|||And this next comment is important, so I'm going to make it its own post:

The metadata (number of columns, data types, etc...) must be the same across all of your tables that you are dynamically feeding into the OLE DB Destination.|||

Phil,

It works that way...if i already create the table before running the package. But if i remove the tables from the db and run the package again it fails.

But SSIS should create tables during runtime.

|||

Phil,

I got it...I just had to change "Validate External Metadata" property of OLE DB destination to False.

It works all fine now...thanks for your help;)

|||

Amar Khaira wrote:

Phil,

It works that way...if i already create the table before running the package. But if i remove the tables from the db and run the package again it fails.

But SSIS should create tables during runtime.

Try setting the "ValidateExternalMetaData" property on the OLE DB Destination to false. If that doesn't work, then set the "DelayValidation" property of the data flow to true.|||

Amar Khaira wrote:

Phil,

I got it...I just had to change "Validate External Metadata" property of OLE DB destination to False.

It works all fine now...thanks for your help;)

Excellent. Please mark one of these posts as the answer to your question.

Thanks,
Phil

Friday, February 17, 2012

Dynamic SQL Server Destination?

Hi,

Is there anyway to dynamically change the Sql Server Destination? Let's imagine i want the data to go to a table based on a specific parameter like year for example...

Imagine tables like table1_2005, table1_2006 and i want to dinamically change the SQL Server Destination so it would insert 2005 or 2006 and so on....

I can't seem to find that option... It was really a good idea for microsoft to implement Expressions in such components... Dynamics it's all about it :)

Best Regards,

Luis,
From my experience the words dynamic and data flow task don't seem to go hand in hand....and SSIS leaves a lot to be desired in this area. However there are some workarounds that I have used. I've mainly done dynamic stuff with datasources, using the datareader and expressions for the SqlCommand property. Granted the table can change, but the metadata, field names, and datatypes need to be identical(as far as I'm aware at least). I don't believe any kind of expressions are possible with destinations within a dataflow though. However you could try out the "table name or view name variable" selection in the data access mode piece of the destination, and then use package variables to set the table name. You would have to use the OLE DB Destination to do this instead of SQL Server Destination, since it's not implemented there(at least in Sept CTP, I haven't moved over yet to release). Jamie has a good article on using variables in a OLE DB source that might give you some ideas:
http://blogs.conchango.com/jamiethomson/archive/2005/12/09/2480.aspx

Thanks,
Adrian
|||

Yes i had already seen that option in the OLE DB but if i use that i would up getting hundred of diferent variables and that's not too good.. an Expression would be much more accessible and i cant understand why it doesn't exists...

Lets wait and see if it comes in by microsoft!

|||Perhaps you should use "Table or view name variable" as the data access mode of your destination, then set the variable to EvaulateAsExpression=True. Then you can put an expression in the Expression property and the destination will receive the evaluation result as the table name.
|||That's correct. Sql Server Destination does not support variable access mode, so it is not possible to change the table name dynamically at execution time. You should use OLEDB destination, and use the variable access mode. For the variable, make the EvaluateAsExpression = True, and use an expression to determine the variable value.