SSIS variables in the query - sql-server

I have ssis package which uses the SQLserver 2012 and
have the variable : clientLastSync which holds datetime
and have the execute task
query as below
select * from clnt where opendt > convert(varchar, CAST (#[User::clientLastSync] as date),101)
If I execute the package, below error appears:
Error: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has
occurred. Error code: 0x80040E07. An OLE DB record is available.
Source: "Microsoft SQL Server Native Client 11.0" Hresult:
0x80040E07 Description: "Conversion failed when converting date
and/or time from character string.".
Can you suggest what is wrong here. Thanks

You pass variables in SSIS as a ?, and then define your variables in your parameter mapping pane.
I'm assuming that you aren't silly enough to be storing your dates as a varchar, thus your Direct input SQL in your Execute SQL Task becomes:
SELECT * FROM clnt WHERE opendt > ?;
Then, you need to go to your Parameter mapping pane and click Add. Select your variable's name (probably User::clientLastSync ) in the Variable name Column. Then Input as the direction. DBTIMESTAMP as your Data Type and ParameterName as 0 (if you had further parameters they would be 1, 2, 3, etc in the order they appear in your query). Leave Paramter Size as -1.
This should work as you intend now.

Related

Insert a datetime value with SSIS to an datetime column in a table on a IBM AS/400 library/system

I have the following problem.
I want make a SSIS flow that inserts values from a table in a MS SQL server database to a table in a AS/400 library.
This works for all the values but the datetime2 value. I tried to insert a datetime2 value and a varchar value in the datetime2 format. Both ways did not work.
It gives the following error:
[TABLENAME [11566]] Error: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80040E21.
An OLE DB record is available. Source: "IBMDA400 Command" Hresult: 0x80004005 Description: "CWBZZ5014 Value of parameter COLUMNNAME could not be converted to the host data type.".
An OLE DB record is available. Source: "IBMDA400 Command" Hresult: 0x80004005 Description: "CWBZZ5014 Value of parameter COLUMNNAME could not be converted to the host data type.".
[TABLENAME [11566]] Error: There was an error with input column "COLUMNAME" (12245) on input "OLE DB Destination Input" (11579). The column status returned was: "The value could not be converted because of a potential loss of data.".
Does someone knows how to get this done?
I assume conversion in sql scripting could be better way of mapping destination column in IBM , try this.
CONVERT(TIMESTAMP, #DateTimeVariable ) or
use derived column expression and there by use data conversion.
I would like to try db_timestamp
Data Conversion

How to add null value if a column doesn't exist

I have a package in my ETL that loops through 4 different databases, each with a copy of the same table. I found out that one of the four tables has one extra column named MTFvalue_Permit thus giving me the below error. My fact and staging tables, however, do include this column.
I was hoping ssis would just insert a null value if the column didn't exist. How can I add a null value into my Data Warehouse tables for the other three tables in which this column doesn't exist?
Error:
[Source DB IBS [1]] Error: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80040E14.
An OLE DB record is available. Source: "Microsoft SQL Server Native Client 10.0" Hresult: 0x80040E14 Description: "Statement(s) could not be prepared.".
An OLE DB record is available. Source: "Microsoft SQL Server Native Client 10.0" Hresult: 0x80040E14 Description: "Invalid column name 'MTFvalue_Permit'.".
Edit: I'm thinking about using a case statement like this for the query but I'm getting a different error.
SELECT
[blahblahblah] ,
[blahblahblah] ,
[blahblahblah] ,
[blahblahblah] ,
[blahblahblah] ,
CASE WHEN COL_LENGTH('wmManifests', 'MTFvalue_Permit') IS NOT NULL
THEN [MTFvalue_Permit]
ELSE NULL
END AS 'MTFvalue_Permit'
FROM dbo.wmManifests
Invalid column name 'MTFvalue_Permit'.
I don't think that you can do it using expressions.
You can do this workaround.
Add a Script task that list columns from the table
Build the Select query dynamically:
If the column is not found pass NULL instead of the column name and give an alias (same as column name)
save this query string into a SSIS variable and use it as a source
You can also create a stored procedure that generate the query string. Execute it from a sql task and store the query string into ssis variable
Perhaps I am not understanding your question, but it seams like you are approaching this problem backwards. You cannot insert data into columns that do not exist. Is there a reason this column exists in only one out of four versions of this table? And does this column have a default value that you can leverage?
I would continue inserting to the set of columns that are common among all tables and allow the tables with columns that do not exist in every one of the tables in the set to use the default value for that column.

SSIS: passing derived column variable to execute sql task

I followed the post from bilinkc as mentioned here
how to load extracted file name into sql server table in SSIS
I use this extracted file name in a variable called FileName in a data task within a For Each container. But when I want to use the same variable in a consecutive Execute SQL task it is throwing up the error
[Execute SQL Task] Error: Executing the query "
INSERT INTO HOURLY_DATA
..." failed with the following error: "Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Parameter settings : Data Type is VARCHAR Parameter name is 0 and Parameter length is 1000.
Is there anyway to get around this error. WHen i checked on other posts they say it is related to a TEXT length error but I am not able to get my head around this solution for my scenario.
The full query is as given below:
This is the full query :
INSERT INTO HOURLY_DATA
(UNIQUE_NUM, NAME, FILE_NAME, USER_NAME)
SELECT DISTINCT UNIQUE_NUM, 'MANUS',?, 'MENON'
FROM MY_TABLE
WHERE UNIQUE_NUM IS NOT NULL
For the parameter variable, I have a package level variable defined which depends upon this expression:
REPLACE(REPLACE(REPLACE(REPLACE(#[User::varFilePath],#[User::varSourceFolder],""),"\",""),"My File",""),".xlsx","")
But when I evaluate this expression nothing happens. Could this variable be the issue?
Thank you in advance for your time and help.

How do I convert an Oracle TIMESTAMP data type to SQL Server DATETIME2 data type while connected via Link Server.?

I have tried some examples but so far not working.
I have a Link Server (SQL Server 2014) to an Oracle 12C Database.
The table contain a datatype TIMESTAMP with data like this:
22-MAR-15 04.18.24.144789000 PM
When attempting to query this table in SQL Server 2014 via link server I get the following error using this code:
SELECT CAST(OracleTimeStampColumn AS DATETIME2(7)) FROM linkServerTable
Error:
Msg 7354, Level 16, State 1, Line 8
The OLE DB provider "OraOLEDB.Oracle" for linked server "MyLinkServer" supplied invalid metadata for column "MyDateColumn". The data type is not supported.
While the error is self explanatory, I am not certain how to resolve this.
I need to convert the timestamp to datetime2. Is this possible?
You can work around this problem by using OPENQUERY. For me, connecting to Oracle 12 from SQL 2008 over a linked server, this query fails:
SELECT TOP 10 TimestampField
FROM ORACLE..Schema.TableName
...with this error:
The OLE DB provider "OraOLEDB.Oracle" for linked server "ORACLE" supplied invalid metadata for column "TimestampField". The data type is not supported.
This occurs even if I do not include the offending column (which is of type TIMESTAMP(6). Explicitly casting it to DATETIME does not help either.
However, this works:
SELECT * FROM OPENQUERY(ORACLE, 'SELECT "TimestampField" FROM SchemaName.TableName WHERE ROWNUM <= 10')
...and the data returned flows nicely into a DATETIME2() field.
One way to solve the problem is to create a view in oracle server and convert the OracleTimeStampColumn compatible with sql server's datetime2datatype. You can change the time format to 24 hours format in oracle server's view and mark the field as varchar. Then you can convert the varchar2 column to datetime2 when selecting the column in SQL Server.
In Oracle Server
Create or Replace View VW_YourTableName As
select to_char(OracleTimeStampColumn , 'DD/MM/YYYY HH24:MI:SS.FF') OracleTimeStampColumn from YourTableName
In SQL Server
SELECT CAST(OracleTimeStampColumn AS DATETIME2(7)) FROM **linkServerVIEW**

Storing System::StartTime in SSIS to a datetime column [duplicate]

I am working in SQL Server 2008 and BIDS (SSIS). I am trying to generate a "load ID" for when a package is executed and store that ID in a load history table (which then populates subsequent tables).
My basic SSIS control flow is the following:
Execute SQL Task, Data Flow Task
The load table is created via the following:
CREATE TABLE dbo.LoadHistory
(
LoadHistoryId int identity(1,1) NOT NULL PRIMARY KEY,
LoadDate datetime NOT NULL
);
The editor for the Execute SQL Task is as follows:
General:
ResultSet = None
ConnectionType = OLE DB
SQLStatement:
INSERT INTO dbo.LoadHistory (LoadDate) VALUES(#[System::StartTime]);
SELECT ? = SCOPE_IDENTITY()
Parameter Mapping:
Variable Name = User::LoadID
Direction = Output
Data Type = LONG
Parameter Name = 0
Parameter Size = -1
SSIS is throwing the following error:
[Execute SQL Task] Error: Executing the query "INSERT INTO dbo.LoadHistory
..." failed with the following error: "Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
This error message doesn't really help me find the problem. My best guess is that it's due to the parameter mapping, but I don't see my mistake. Can anybody point out my problem and provide the fix?
I figured out my problem. System::StartTime needs to have DATE as its data type, not DBTIMESTAMP.
I was passing three parameters.
In the Parameter Name property I had:
0
1
3
Corrected it to:
0
1
2
It works now, no multiple-step operation generated errors message.

Resources