For Loop Container SSIS AssignExpression - sql-server

I have table with columns Start date and end date and i need to enter
values for 2 years.I created a for loop and I am trying to load values for
every month.
For Example, My first input start Date is 10/12/2016 end date 11/11/2016. It
should be incremented with one month and inserted into table for 2 years.
For Loop Container
InitExpression: #windowStart="10/12/2016"
Evalexpression: #WindowStart<#windowMaxdate
AssignExpression: #windowStart= Dateadd("mm",1,#windowStart)
Execute sql task
SQL command:
Insert into dbo.datetemp
(WindowStart,WindowEnd ) values (?,?)
My problem is i am not getting values what i am expected and its just
returning same windowstart and windowend for every record. And loop is not coming to stop.

Guys i got the solution.
I have added a expression task inside for loop after execute sql task
#[User::WindowStart] = (DT_WSTR,24)(DT_DBTIMESTAMP) dateadd("mm",1,
(#[User::WindowStart]) )
instead adding this expression in Assign expression in for loop editor.

You are passing a string value to a #windowStart.
Create a variable #StartDate of type Datetime with the value 2016-12-10. And use the following expression:
InitExpression: #windowStart= #StartDate
Also make sure that #windowStart and #windowMaxdate are of type DateTime
Also check this similar question for more information:
How do I loop through date values stored as numbers within For Loop container?

Related

SSIS SQL Task not returning string date

I am trying to run the below procedure from SSIS using SQL Task.
ALTER PROCEDURE [dbo].[sp_Previous_Load_Dt_Tm]
AS
BEGIN
SELECT Extract_Dt_Tm = ISNULL(CONVERT(VARCHAR(20), MAX([Previous_Load_Date_Time]),120),'')
FROM [Test_table] WITH (NOLOCK)
END
I am returning as below
When I use the variable type as String in SSIS :
If I update the variable type to date it gives me the value:
Question ) I am not sure why its returning a date when I am converting the date to varchar in the procedure.
Is this happening at run time?
If you change the variable type to datetime at design time, and the string returned from the stored procedure is written to it during execution, this would most likely be due to SSIS performing an implicit conversion, i.e. successfully parsing a datetime value from the string.
I imagine the images you posted are at design time, with the string not having been written yet, and the datetime having defaulted to the current date and time?
I just removed and re added the variable. And it worked!!!! Not sure what the issue was.

Get DateTime value from DB into a variable in SSIS

I'm taking the last updated date from SQL table as below:
SELECT CONVERT(DATETIME, [Value]) AS LastModifiedDate
FROM [WarehouseDB].[dbo].[tblWarehouseSettings]
WHERE Name = 'LastModifiedDate'
[Value] is varchar.
A variable as below:
I'm using an Execute SQL Task to get the date value & assign it to the variable. My Execute SQL Task Editor values set as below:
The task executed successfully but it doesn't get the value from the DB. Assigned value to the variable after the task execution is {12/30/1899 12:00:00 AM}
Can anyone figure out what I'm doing wrong here?
There are 2 things that caused this issue :
There is no need to specify #[User::LastUpdateOn] as an Output parameter in Parameters mapping Tab
Your SQL Statement is showing that you are converting [Value] column to DATETIMEOFFSET instead of DateTime and DT_DBTIMESTAMP is related to DateTime SQL Data Type
References
https://learn.microsoft.com/en-us/sql/integration-services/data-flow/integration-services-data-types
http://bidn.com/blogs/DevinKnight/ssis/1387/ssis-to-sql-server-data-type-translations

Run SSIS Package skipping Invalid dates

I've a DWH Table which has Date Value stored as int.Now I wanna get all records for a day which are active on a day with conditions as start_date<=#date and End_Date>#DATE
for almost close to 2 years.
I've used a for loop and 3 variables where in hardcoded FROM_DATE and TO_DATE as 20130101 and 20150107 and the 3 variable VAR_DATE initially set TO_DATE and decreasing by 1 until it reaches FROM_DATE.
But after reaching 20150101...It's starting to insert values for 20150100,20150099 and so on..
Is there a way where in I can check to see if the VAR_DATE column being used is actually a valid date(by convert and comparing on the go) or any possible way of using a result set..
Thanks,
Vijay
Use a script to convert the integer into a string. Then parse it into a date object like so:
DateTime parsedDate;
bool parseResult = DateTime.TryParseExact(dateValue, "yyyyMMdd", null, DateTimeStyles.None, out parsedDate)
Then parseResult will be true if it's a valid date.

Assigning a SQL NULL to a SSIS string - A SSIS Flaw?

I fetch a result set with an execute SQL task. It has only one column NullTime varchar. It has three rows, first one is NULL. I want to simply iterate and display the value of these rows. If I do it only by C# script, then there is no problem. The NULL is displayed as a blank. The same thing can also be done with a foreach loop.
How to do it with foreach - use that loop to read each row and set the value of each row to SSIS string User::STR_WORD. Then, simply display User::STR_WORD with a C# script task.
In the pure C# method, I can even assign the blank value (actually a NULL) to the SSIS string. But with foreach loop method, I get an error because of the NULL value.
The error is -
Error: The type of the value being assigned to variable "User::STR_WORD" differs from the current variable type. Variables may not change type during execution. Variable types are strict, except for variables of type Object.
How do I fix this error ? Is script the only alternative to what seems to be a flawed for
loop ?
A workaround for this is the Coalesce function which will convert NULLs to the value you specify.
SELECT
COALESCE([YourColumn], 'EnterValueHere') AS [YourColumn]
FROM YourTable
So, this will replace a null with the value you want to use instead. It will
prevent the weird FOR loop from suddenly crashing.
Create a temporary table to see this working -
create table ##tester
(names varchar(25))
insert into ##tester
values(Null)
insert into ##tester
values('Not a null value')
select names from ##tester
select coalesce(names, 'Null has been obliterated!') from ##tester

How to use XML data of SQL table column and perform tasks based on tag values

I need some help. I am suppose to get XML data from column of a table. The XML data has many tags. One of the tag contains values like
<Code date= 30/03/2013>
<apr id =1> -2 </apr>
<rdv id =2> 1 </rdv>
</code>
I need to run a particular task which checks the date. Like if today's date= (Code date -2) or
today's date = (code date + 1)
run a mailer task.
How should I go about it ? Please forgive me for the XML data format. I am naive in XML.
Create 2 variables to store the XML column Date value and other one to get the current date .To write an expression ,select the variable ,right click on it .In the properties windows Set EvaluateAsExpression =true and write the expression
Name DataType Expression
ExecDate DateTime
CurrentDate DateTime GETDATE()
Use Execute SQL Task .Set Resultset to Single Row and write the following code to read the date value from the XML column in your table
SELECT
convert(datetime,XMLData.value('(/Code/#date)[1]', 'varchar(10)'),103) as PkgDate
from yourTable
Demo in SQL Fiddle
In the resultset tab map the output from the above sql query with the variable ExecDate
Result Name Variable Name
PkgDate ExecDate
Put another Task or component Ex SEND Mail TASK after Execute SQL Task and write an expression in the Precedence Constraint.
Expression : #ExecDate==#CurrentDate
Only when the Date value from the XML column matches with the Current Date which is stored in the variable #CurrentDate then only the other components after Execute SQL Task will execute .
today's date= (Code date -2) or today's date = (code date + 1) run a mailer task.
In order to perform the above expression ,you need to change the sql code in Execute SQL Task
To get CodeDate +1
SELECT
Dateadd(day,1,convert(datetime,XMLData.value('(/Code/#date)[1]', 'varchar(10)'),103)) as PkgDate
from yourTable
I don't have the SSIS environment to test it now but this is how you should be doing it

Resources