SSIS - Date Coloumn to Variable Null and non-Null Values - sql

Within an SSIS package I have a dataflow that extracts two coloumns from an access database a TaskID and a date
I store this in a ADO recordset and pass this to a For Each Loop Container
I am them attempting to assign each value pair to two variables "taskID" and "taskDate"
I then want to use thse two variables within a SQL Insert task that will then update the SQL database in several places with this information
The package works fine when im just pull out the taskID and insert that into sql but when i introduce the date it fails because the date can be NULL and most of the tasks are NULL but SSIS just keeps telling me that it cant put null into the variable, I tried having an Obejct variable which allowed nulls but then the package fails on adding the variable to the sql task as the variable in there is a DATE?
Thanks

There's a possible solution outlined here that uses a second variable and EvaluateAsExpression that may solve your problem.

Related

Use SQL Field in SSIS Variable

Is it possible to reference a SQL field in your SSIS variable?
For instance, I would like use the field from the "table" below
Select '999999' AS Physician_Profile_ID
as a dynamic variable (named "CMSPhysProID" in our example) here
I plan on concatenating multiple IDs into a In statement.
Possible by using execute sql taskIn left side pan of Execute SQL task, general tab 1.Select result set as single row2. Connection type ole db 3. Set connection and form SQL statement, As you mentioned Select '999999' AS Physician_Profile_ID 4.Go to result set in your left side pan 5. Add your variable where you want to store '999999' 6. Click ok
If you are looking to store the value within the variable to be used later, you can simply use an Execute SQL Task with a single row result set. More details in the following article:
SSIS Basics: Using the Execute SQL Task to Generate Result Sets
If you are looking to add a computed column while importing data, you must use a Derived Column Transformation within the data flow task to add a column based on another one, you can refer to the following article for more details about this component:
SSIS Derived Columns with Multiple Expressions vs Multiple Transformations
What are you trying to accomplish by concatenating the IDs into an "IN" statement? If the idea is to use the values of the IDs to limit the results, as a dynamic WHERE clause, you may have better luck just using a lookup against either a table you maintain with the desired IDs or even a static list generated in the package with a script task. (If you can use the lookup table method it will be much easier to maintain as you only have to update a table, not your source code.)
Alternatively, you may even be able to accomplish the goal with a join. Create a temp table from the profile IDs you want to keep and join to it, or, again, use it as a lookup component. Dynamically creating a where clause using IN will come in a lot slower and will be cumbersome to maintain.

How to pass ado object to an in clause of a sql command in SSIS

Details (this is specific to SSIS) : There are two database servers : A & B
I have to fetch list of few values from database A (like 1,2,3,4,5 in one column) and pass it in the "IN" clause to a command that will run on database B (like Select something from B.Table where column in (1,2,3,4,5)).
The "Execute SQl task" result set is mapped to an object variable X, which cannot be passed directly as a parameter to the second command.
What are the possible solutions to this?
One possible solution is to use a Script Task to iterate through the Object Variable and build a SQL String to be used in the second Execute SQL task.

SSIS ForEach Variable Mapping Error

I want to be able to send emails using SSIS. I followed the instructions at "How to send the records from a table in an e-mail body using SSIS package?". However, I am getting an error:
Error: ForEach Variable Mapping number 1 to variable "User::XY" cannot be applied
while running the package. My source table has 5 columns (bigint, datetime, nvarchar, nvarchar, nvarchar types).
Another error is:
Error: The type of the value being assigned to variable "User::XY" differs from the current variable type. Variables may not change type during execution. Variable types are strict, except for variables of type Object.
What could the problem be?
UPDATE: As I was trying to find out the problem, I have done this: while taking the data from Execute SQL Task, I cast the int data to varchar and then use the variable with String data type and it works. But how should I set the variable so it has INT data type, not varchar?
I just ran into this problem & resolved it, although I don't know exactly how.
Running SSIS for SQL Server 2008 R2:
a) query pulls rows into an object
b) for each trying to loop through and pull values for the first two columns into variables
(this had already been running fine--I had come back to edit the query and the for each loop and add an additional variable for branching logic)
c) error mapping variable '1', which happened to be an int and happened to have the same name as the column I was pulling from.
I tried deleting the variable-to-column reference in the foreach loop and re-adding it, and I discovered that the variable was not listed anymore in the list of variables allowed for mapping.
I deleted the variables, created a new variable of the same type (int32) and name, added it, and things ran fine.

Augmenting Rows With Value From T-SQL Statement

I'm using SSIS 2012 to import an Excel file into a database. One of the fields I need to populate into the database on import cannot be stored in the Excel file. The value that goes into the field can only be known at the time the record is being created in the database. The particular software I am using stores the last used value for this field in a separate database. When creating the records on import I need to increment this field and insert the new value in the new record. I have a T-SQL script that generates this value but I don't know enough about SSIS to know how to get that value for each row during Import.
Here's the script that I'm using to generate the value I need:
--Declare some variables
DECLARE #I_sCompanyID smallint,
#O_mNoteIndex numeric(19,5),
#O_iErrorState int
--Get the CompanyID
select #I_sCompanyID = CMPANYID
from DYNAMICS..SY01500
where INTERID = DB_Name()
--Get and increment the next note index
exec DYNAMICS.[dbo].[smGetNextNoteIndex] #I_sCompanyID, 1, #O_mNoteIndex output, #O_iErrorState output
--Print the Next Note Index
SELECT #O_mNoteIndex
The option that comes to mind is to use a Script Component to add a column named O_mNoteIndex into your data flow. You will basically need to use your above TSQL code and either work with OleDB, SqlClient or Odbc to query the Dynamics server and generate your id.
You will need to add the column into your output buffer and assign that the tsql value. I'm not finding any of my previous answers that explore how to do this but the msdn site ought to get you started
I suggest you run the above script and return the result into a SSIS variable. Then add that variable in a computed column transform. That is assuming you need to just run that script once before you load your dataset. If you need to run it per record, you need to go #billinkc's method.
Or if you want to avoid script you could load your source data into an OLEDB recordset and then use that in a For Each Loop and call that script for each iteration of the loop

SSIS - fill unmapped columns in table in OLE DB Destination

As you can see in the image below, I have a table in SQL Server that I am filling via a flat file source. There are two columns in the destination table that I want to update based on the logic listed below:
SessionID - all rows from the first CSV import will have a value of 1; the second import will have a value of 2, and so on.
TimeCreated - datetime value of when the CSV imports happened.
I don't need help with how to write the TSQL code to get this done. Instead, I would like someone to suggest a method to implement this as a Data Flow task within SSIS.
Thank you in advance for your thoughts.
Edit 11/29/2012
Since all answers so far suggested taking care of this on the SQL Server side, I wanted to show you what I had initially tried doing (see image below), but it did not work. The trigger did not fire in SQL Server after SSIS inserted the data into the destination table.
If any of you can explain why the trigger did not fire, that would be great.
If you are able to modify the destination table, you could make the default values for SessionID and TimeCreated do all the work for you. SessionID would be an auto-incremental integer while the default value for TimeCreated would be getdate() or gettime() depending on the data type.
Now, if you truly need it the values to be created as part of your workflow, you can use variables for each.
SessionID would be a package variable which is set by an Execute SQL Task. Just reference the variable in your result set and have your SQL determine the next number to use. There are potential concurrency issues with this, though.
TimeCreated is easily done by creating a Derived Column in your data flow based on the system variable StartTime.
You can use a Derived Column to fill the TimeCreated column, if you want the time of the data flow to happen, you just use the date and time function to get the current datetime. If you want a common timestamp for the whole package (all files) you can use the system variable #[System::StartTime] (or whatitwascalled).
For the CSV looping (i guess), you use a foreach loop container, and map an iterative value to a user variable that you map in the derived column for SessionID as mentioned above.
First, I'd better do it on SQL Server side :)
But if you don't want or cannot to do it on server side you can use this approach:
It is obvious that you need to store SessionID somewhere you can create a txt file for that or better some settings table in SQL Server or there can be other approaches.
To add columns SessionID and TimeCreated to OLE Destination you can use Derived columns