In SSIS, I am feeding a XML file and using XSLT getting one resultant XML(with my required field) file.
Could I use the result of XML task to directly feed the SQL table.
While exploring in this regard..i got to know I can save the result of XML task to a variable and can use this variable to put the data in SQL table.
Not sure how to accomplish this or is it possible ?
If you have the resultant value contained within a varaible. Assuming it is just one value. You could use a Parameterised Execute SQL Task to accomplish this.
Related
I want to copy and merge data from tables with identical structure (in a number of different source databases) to a single table of similar structure in a destination database. From time to time I need to add or remove a source database.
This is currently achieved using a Data Flow Task containing an OLEDB source with a SQL query within which there is a UNION for each of the databases I am extracting from. There is quite a lot of SQL within each UNION so, if I need to add fields, I need to add the same additional SQL to each UNION. Similarly, when I add or remove a source database I need to add or remove a UNION.
I was hoping that, rather than use such a UNION with a lot of duplicated code, I could, instead, use a Foreach Loop Container that executes SQL contained in a variable using parameters to substitute the name of the database and other database dependent items within the SQL on each iteration but I hit problems with that as I assume the Data Flow Task within the loop could not interpret the incoming fields because of the use what is effectively dynamic SQL.
Any suggestions as to how I might best achieve this without duplicating a lot of SQL?
It sounds like you have your loop figured out for moving from database to database. As long as the table schemas are identical (other than names as noted) from database to database, this should work for you.
Inside the For Each Loop container, create either a Script Task or an Execute SQL Task, whichever you're more comfortable working with.
Use that task to dynamically generate the SQL of your OLE DB Source query, changing the Customer Code prefix for each iteration. Assign the SQL text to a variable, either directly in a Script Task, or by assigning the Result Set of your Execute SQL Task (the result set being the query text) to a variable.
Inside your Data Flow Task, in the OLE DB Source, under Data Access Mode select "SQL Command from variable". Select the variable that you populated with your query in the last task.
You'll also need to handle changing the connection string between iterations, but, again, it sounds like you have a handle on that part already.
My requirement is that I need to spool multiple tables in SQL Server to as many CSVs using SSIS.
The filename convention should be somethin like below:
TABLENAME_MAX(Period)_GETDATE().csv
I can get the getdate() and Max(Period), that is fine.
How do I store the table name to a variable so that I can use it as an expression for the CSV connection manager.
You can store the list of tables in another table. Then use loop container in SSIS to loop.
Ex: http://pldineshtech.blogspot.in/2012/08/loop-through-each-row-in-table-using.html
I have an SSIS package that contains one data flow task that has several data sources as well as several destinations. The package takes data fro one table and inserts it into another.
I want to transfer from Source table to destination table only records that belong to a particular CollectionID. I added a parameter "CollectionID" of type string to the project and added the parameter to the configuration file.
I select data from the source table via SQL command. How can I get the sql command to use the parameter I added to the configuration file? I understand I need to add a WHERE clause, but how do I point the where clause to a parameter in the config file?
You need to create a variable and map it to the configuration value.
Assuming you are using the OLE connection type, you then map the variable value to the SQL statement with the ? placeholder.
SELECT * from Table where columnvalue = ?
Finally, map the variable in the ExecuteSQL task:
If the parameter doesn't have a name you can just use 0, but make sure the data type is correct. If it is a text data type, you will need to give it the proper length, not -1.
I've got a column in a SQL Server table that is type XML. I want to change multiple values in the XML in a stored proc. I've already found the XML.Modify(...) command and have it working for a single value. Can I change multiple values in the XML in one update command or do I need to do it as multuple update commands?
You will need to use multiple (xmlcol).modify calls - you cannot update multiple bits at once, as far as I know.
Is there a direct route that is pretty straight forward? (i.e. can SQL Server read XML)
Or, is it best to parse the XML and just transfer it in the usual way via ADO.Net either as individual rows or perhaps a batch update?
I realize there may be solutions that involve large complex stored procs--while I'm not entirely opposed to this, I tend to prefer to have most of my business logic in the C# code. I have seen a solution using SQLXMLBulkLoad, but it seemed to require fairly complex SQL code.
For reference, I'll be working with about 100 rows at a time with about 50 small pieces of data for each (strings and ints). This will eventually become a daily batch job.
Any code snippets you can provide would be very much appreciated.
SQL Server 2005 and up have a datatype called "XML" which you can store XML in - untyped or typed with a XSD schema.
You can basically fill columns of type XML from an XML literal string, so you can easily just use a normal INSERT statement and fill the XML contents into that field.
Marc
You can use the function OPENXML and stored procedure sp_xml_preparedocument to easily convert your XML into rowsets.
If you are using SQL Server 2008 (or 2005), it has an xml native datatype. You can associate an XSD schema with xml variables, and Insert directly into columns of type xml.
Yes, SQL Server 2005 and above can parse XML out of the box.
You use the nodes, value and query methods to break it down how you want, whether values or attributes
Some shameless plugging:
Importing XML into SQL Server
Search XML Column in SQL
Xml data and Xml document could have different meaning.
When xml type is good for data, it doesn't save formatting (white spaces removed), so in some cases (e.g. cofiguration files) the best option is nvarchar.