Showing posts with label table1. Show all posts
Showing posts with label table1. Show all posts

Wednesday, March 21, 2012

Dumping Data from a XML file to DB Tables

Hi All ,

I have an XML file saved in my machine at c:\MyXML.xml . I have two tables in my DB as Table1 ( [col1],[col2]) & Table2()[col1],[col2] . I have to dump the data from this xml file to these two tables in one shot .The format of the XML file MyXML.xml is something like this :

<?xml version = "1.0">

-<Report xmlns = " MyReport".......... >

- <table1>

- <Data>

<Record col1 = "A" col2 = "B" / >

<Record col1 = "C" col2 = "D" / >

<Record col1 = "E" col2 = "F" / >

</Data>

</table1>

- <table2>

- <Data>

<Record col1 = "A" col2 = "B" / >

<Record col1 = "C" col2 = "D" / >

<Record col1 = "E" col2 = "F" / >

</Data>

</table2>

</Report>

Plz suggest me some possible ways to get this done . I m using sql 2005.

Thanks in advance for the help.

Here is a complete example, you can use the OPENXML statement after the stored procedure sp_xml_preparedocument has parsed the XML:

Code Snippet

CREATE TABLE table1 (

col1 nvarchar(10),

col2 nvarchar(10)

);

CREATE TABLE table2 (

col1 nvarchar(10),

col2 nvarchar(10)

);

DECLARE @.xmlDocument xml;

SET @.xmlDocument = '<Report xmlns="MyReport">

<table1>

<Data>

<Record col1 = "A" col2 = "B" />

<Record col1 = "C" col2 = "D" />

<Record col1 = "E" col2 = "F" />

</Data>

</table1>

<table2>

<Data>

<Record col1 = "A" col2 = "B" />

<Record col1 = "C" col2 = "D" />

<Record col1 = "E" col2 = "F" />

</Data>

</table2>

</Report>';

DECLARE @.docHandle int;

EXEC sp_xml_preparedocument @.docHandle OUTPUT, @.xmlDocument, N'<Report xmlns:ns1="MyReport"/>';

INSERT INTO table1

SELECT *

FROM OPENXML (@.docHandle, N'/ns1:Report/ns1:table1/ns1:Data/ns1:Record', 0)

WITH table1;

INSERT INTO table2

SELECT *

FROM OPENXML (@.docHandle, N'/ns1:Report/ns1:table2/ns1:Data/ns1:Record', 0)

WITH table2;

EXEC sp_xml_removedocument @.docHandle;

SELECT col1, col2 FROM table1;

SELECT col1, col2 FROM table2

Wednesday, March 7, 2012

dtsx load id list from xls

How to write a dtsx that loads a list of ids from an xls and incorporates
this list into sql ?
For example,
update table1 set column1 = false where id in ( {the list of ids } )On Mar 12, 7:30 pm, "John Grandy" <johnagrandy-at-gmail-dot-com>
wrote:
> How to write a dtsx that loads a list of ids from an xls and incorporates
> this list into sql ?
> For example,
> update table1 set column1 = false where id in ( {the list of ids } )
These directions will get an excel spreadsheet into a table that you
can then do: 'update table1 set column1 = false where id in (select id
from NewlyCreatedImportTable)':
1. Create a new Integration Services project.
2. Right click in the 'Connection Managers' tab and select 'New
Connection...' from the drop-down list.
3. Select the 'EXCEL' type and the 'Add' button.
4. Select the 'Browse...' button to browse for the Excel file and
select the 'Open' button.
5. If the first line of the Excel file contains the column names,
select the radio button to the left of: "First row has column names"
then select the 'Ok' button.
6. Right click in the 'Connection Managers' tab and select 'New OLE
DB Connection...' from the drop-down list.
7. Select the 'New...' button and select a Server name from the drop-
down list and select 'OK' and select the 'OK' button again.
8. Drag and drop a 'Data Flow Task' control from the Toolbox onto the
'Control Flow' tab/pane.
9. Select the 'Data Flow' tab/pane.
10. Drag and drop an 'Excel Source' control from the Toolbox onto the
'Data Flow' tab/pane.
11. Right click the 'Excel Source' control and select 'Edit...'
12. Below 'OLE DB connection manager:' select the connection manager
created in steps 2-5.
13. Select the sheet number from the drop-down list below: "Name of
the Excel sheet:" and then select 'OK.'
14. Drag and drop an 'OLE DB Destination' control from the Toolbox
onto the 'Data Flow' tab/pane.
15. Drag the green arrow from the 'Excel Source' control to the 'OLE
DB Destination' control.
16. Right click the 'OLE DB Destination' control and select the
'Edit...' button
17. Below 'OLE DB connection manager:', select the connection control
created in steps 6-7.
18. Select the 'New...' button and edit the sql query for the table
name and column names of the new table to be created (NOTE: SSIS is
very picky on the conversion types so
change datatypes very carefully, if at all).
19. Select the 'Mappings' option from the left-hand-side and map the
Input Columns and Destination columns as desired.
20. Select the 'OK' button and select 'F5' to execute the new package.
Hope this helps.
Regards,
Enrique Martinez
Sr. SQL Server Developer