Today I have a new situation, in which out client asks the following:
We have XML files in a folder. We need to load these files into a SQL Server table column (which has XML data type); we are NOT loading the XML outputs into various SQL Server tables, rather we are loading the XML file itself into a column, which has XML data type, in SQL Server database.
This ought to be done in SSIS only, as per the client requirement. So I use an Execute SQL Task in this manner. (Apart from the XML file, we need the ImportDate
, FileName
, etc.)
The Connection Type is OLE DB; the SQL Statement I have is as below:
INSERT INTO dbo.tablename (IncomingXMLfile, ImportDate)
-- I am using just 2 columns as an example for this table--
SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE()
FROM OPENROWSET(BULK 'C:\Mic\...........\API_Schemas\ABC.xml', SINGLE_BLOB) AS x;
This works fine when I hardcode with just one file. Now, in the C:\Mic\...\API_Schemas
folder, there are many XML files, I need to load each one of them, and apart from that get their ImportDate
(which is GETDATE()
), and the name of the file itself (I have not mentioned this column for now in the INSERT statement). I have to use a ForEachLoop Container, and place the Execute SQL Task inside this Container.
Hence there are 2 aspects to this issue:
parameterizing the file name inside the Execute SQL Task.
using ForEachLoop Container (and placing this Execute SQL Task inside) to run each XML file in the folder.
I use a user variable called Filename
(string datatype), which has the value ABC.xml
(the first file in the series of XML files in the folder); I parameterize the above TSQL query in this manner (see below):
INSERT INTO dbo.tablename (IncomingXMLfile, ImportDate)
SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE()
FROM OPENROWSET(BULK 'C:\Mic\...........\API_Schemas\?', SINGLE_BLOB) AS x;
I map this parameter (?
) to the user variable Filename in this Execute SQL Task (ParameterMapping
tab). The query parsed correctly in the Execute SQL Task! However when I execute this SQL Task, I get this error:
"Parameter name is unrecognized.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
This situation is fairly different when I load the data from the XML files (using XML Source Editor, with the Data access mode (XML Data from variable) , etc.) into SQL Server tables. In our case, we are loading the XML file itself into a SQL Server table COLUMN which has XML data type, and get information about the various XML files loaded from the folder. As a result I am not sure on how to use a variable to run this package.
Can anyone first of all help me on how to parameterize the filename correctly?
And later on how to configure the ForEachLoop Container to read each file from the folder?
I am confused on how to use user variables in this scenario.