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

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.

Related

SSIS OLE DB Destination Table View Fastload or normal does not give error on duplicate key

I am filling a table using a package and after preparing the data to be saved I present it to an OLEDB destination for SQL server and setting the data acces mode to Table or View - Fastload or just Table or view (no fast load).
There is no error message but it does not write to the table.
I switch over to the normal Table or View so that each record is inserted with a separate INSERT command instead of a BULK insert.
When nothing happens I stop the execution of the package and do a select * from the destination table. I saw that he inserted 20 records. After investigation the data which is send to the OLE DB destination, I saw that record 21 results in a duplicate key.
Instead of getting an error message the package does not continue its execution flow.
What am I doing wrong.
Go through all the elements of the package, including the package itself, and set:
FailPackageOnFailure = True

Bulk Insert Task Error in ssis

While running ssis Bulk insert i got the following error.
An error occurred with the following error message: "Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.The bulk load failed. The column is too long in the data file for row 1, column 1. Verify that the field terminator and row terminator are specified correctly.".
To solve the problem i have change the connection timeout to 0 and to 5 . That did not work .
I also reviewed the data on row 1 column 1.
"In order to avoid these kinds of issues, I always import all fields as varchar fields into a staging table, then convert them into the data type required. You will have much more control."
You can get more details from the following link:
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/5caa8c0a-2328-4c9e-9894-eaba12e8196a/data-conversion-error-during-bulk-insert?forum=sqlgetstarted

Select to Sybase table from SQL Server (by LinkedServer) getting error "db.schema.table was reported to have a "DBCOLUMNFLAGS_ISFIXEDLENGTH" of 16

Select to Sybase table from SQL Server (by LinkedServer) getting error "The OLE DB provider "MSDASQL" for linked server "XXX" supplied inconsistent metadata for a column. The column "XXXX" (compile-time ordinal 1) of object "db.schema.table" was reported to have a "DBCOLUMNFLAGS_ISFIXEDLENGTH" of 16
Query: Select * from [Server].[db].[schema].[table]
Finally after searching some time in the web found the answer, when makes a query on a table having a nullable CHAR column gives the error...
So the solution was create a View in Sybase where I make an ISNULL Validation for the specific column, and from the SQL Server query the View instead the table.
Query: Select * from [Server].[db].[schema].[view]
Source: http://www.dbainfo.net/wp-content/uploads/CR/sdk_17.htm

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.

SSIS: Convert between Unicode and Non-Unicode Strings

Setup
I have a data flow task with:
An OLE DB Source selecting all data from a database table (SQL Native)
An OLE DB Destination of an Access 2003 File template (MS Jet 4.0)
So Essentially I am selecting a table and inserting it into a .mdb file.
Problem
I have the error:
column [X] cannot convert between unicode and non-unicode string data types
However If I add a 'Data Conversion Step' to convert all columns of type DT_STR to DT_WSTR, that error message goes away however I get the error message:
[DTS.Pipeline] Error: "component "OLE DB Source" (6289)" failed validation and returned validation status "VS_NEEDSNEWMETADATA".
Iv'e been Googling this problem for a while and still can't fix it.
Any ideas?
Instead of
Select * from tblName
use
Select col1, col2, col3, etc from tblName
The reason I found is, when you use different database, they might have one or two column that cause issue as it's not mapped. So when you use table query, be specific regarding column name.

Resources