I have two different XML files, from which I have to update two SQL tables via a stored procedure. one is in the format <InvoiceNumber>17-I-36528</InvoiceNumber>, and works fine.
The second file is in the format
<field name="ABCD" type="String" identifier="d167bea7-2ec6-498e-9a8a-3d0e6f37b6b3" value="EFGF" /> this file does not update the table.
What am I doing wrong here? Thanks in advance.
not working XML file
<?xml version="1.0" encoding="utf-8"?>
<document name="COM1-0000000011" type="Commercial_Doc" identifier="02fa318a-bd80-4f73-97bb-141884fbcff5" description="" client="Level 4" process="LEVEL 4 - Commercial" deleted="False">
<field name="Email_From" type="String" identifier="d167bea7-2ec6-498e-9a8a-3d0e6f37b6b3" value="aa#abcd.com" />
<field name="Email_To" type="String" identifier="9f9ad742-502b-44e4-b223-ae048b395bd3" value="bb#abcd.com" />
<field name="Email_Subject" type="String" identifier="cb7c7194-bc4c-4e44-b9b6-7643f1faca13" value="Second set" />
<field name="Email_EmailDate" type="DateTime" identifier="7476b62b-ec35-432b-8d50-572ed504133e" value="03/18/2015 04:05:09" />
<field name="Email_LocalDate" type="DateTime" identifier="ce571c82-440a-4ffd-8ee9-ebf53c1e9071" value="03/18/2015 09:35:09" />
<field name="Email_ReceivedDate" type="String" identifier="f007da05-2ffa-4290-9f28-6cd15de3c5b1" value="03/18/2015 09:35:10" />
<field name="Email_Charset" type="String" identifier="237a7237-b57a-404c-9fdb-39eca5a0d197" value="us-ascii" />
<field name="Email_PreferredCharset" type="String" identifier="f577b3b5-46a2-45ea-8db5-f1b8dc478d2b" value="" />
<field name="OutputImageFile" type="String" identifier="bec3db3f-21c0-4491-99e2-ea32e0f2b788" value="E:\Level 4\ComDoc\Output\02fa318a-bd80-4f73-97bb-141884fbcff5.PDF" />
<table name="itemdata" />
</document>
Stored procedure
CREATE PROCEDURE [dbo].[prc_Commercial_Invoice_XML]
(
#XMLdata XML
)
AS
BEGIN
INSERT INTO [DocumentDetails]
([Name]
,[Type]
,[Identifier]
,[Value]
,[ItemType])
SELECT
[Name] = TempTable.t.value('Email_From[1]','nvarchar(120)')
,[Type] = TempTable.t.value('Email_To[1]','nvarchar(120)')
,[Identifier] = TempTable.t.value('Email_Subject[1]','nvarchar(120)')
,[Value] = TempTable.t.value('Email_ReceivedDate[1]','nvarchar(120)')
,[ItemType] = TempTable.t.value('Email_Charset[1]','nvarchar(120)')
FROM
#XMLdata.nodes('/')AS TempTable(t)
END
Create Table
CREATE TABLE [dbo].[DocumentDetails](
[Id] [int] IDENTITY(1,1) NOT NULL,
[ItemType] [varchar](120) NULL,
[Name] [varchar](120) NULL,
[Type] [varchar](120) NULL,
[Identifier] [varchar](120) NULL,
[Value] [varchar](120) NULL,
CONSTRAINT [PK_DocumentDetails] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
try something like this. SQL Fiddle
DECLARE #XMLdata XML = '<?xml version="1.0" encoding="utf-8"?>
<document name="COM1-0000000011" type="Commercial_Doc" identifier="02fa318a-bd80-4f73-97bb-141884fbcff5" description="" client="Level 4" process="LEVEL 4 - Commercial" deleted="False">
<field name="Email_From" type="String" identifier="d167bea7-2ec6-498e-9a8a-3d0e6f37b6b3" value="aa#abcd.com" />
<field name="Email_To" type="String" identifier="9f9ad742-502b-44e4-b223-ae048b395bd3" value="bb#abcd.com" />
<field name="Email_Subject" type="String" identifier="cb7c7194-bc4c-4e44-b9b6-7643f1faca13" value="Second set" />
<field name="Email_EmailDate" type="DateTime" identifier="7476b62b-ec35-432b-8d50-572ed504133e" value="03/18/2015 04:05:09" />
<field name="Email_LocalDate" type="DateTime" identifier="ce571c82-440a-4ffd-8ee9-ebf53c1e9071" value="03/18/2015 09:35:09" />
<field name="Email_ReceivedDate" type="String" identifier="f007da05-2ffa-4290-9f28-6cd15de3c5b1" value="03/18/2015 09:35:10" />
<field name="Email_Charset" type="String" identifier="237a7237-b57a-404c-9fdb-39eca5a0d197" value="us-ascii" />
<field name="Email_PreferredCharset" type="String" identifier="f577b3b5-46a2-45ea-8db5-f1b8dc478d2b" value="" />
<field name="OutputImageFile" type="String" identifier="bec3db3f-21c0-4491-99e2-ea32e0f2b788" value="E:\Level 4\ComDoc\Output\02fa318a-bd80-4f73-97bb-141884fbcff5.PDF" />
<table name="itemdata" />
</document>'
SELECT
[Name] = TempTable.t.value('#name[1]','nvarchar(120)')
,[Type] = TempTable.t.value('#type[1]','nvarchar(120)')
,[Identifier] = TempTable.t.value('#identifier[1]','nvarchar(120)')
,[Value] = TempTable.t.value('#value[1]','nvarchar(120)')
FROM
#XMLdata.nodes('/document/field')AS TempTable(t)
The query does not cater to the item "ItemType" column. you can add it to the XML as required.
Related
indexing in returning only few of the columns specified in query in data-import xml.
<entity
name="All_Manuals"
query="SELECT Query........"
dataSource="JdbcDataSource">
<field column="Column1" name="id" />
<field column="Column2" name="deptId" />
<field column="Column3" name="groupId" />
<field column="Column4" name="subGrpId" />
<field column="Column5" name="manualId" />
</entity>
We are indexing above all columns, but when we are fetching it is returning only first two columns.
you need to add all columns in your schema.xml like this :
<field name="id" type="string" indexed="true" stored="true" />
<field name="deptId" type="string" indexed="true" stored="true" />
<field name="groupId" type="string" indexed="true" stored="true" />
..............................
and suppose if you dnt want indexing on any column but still want that cloumn in your results
<field name="xxxxx" type="string" indexed="false" stored="true" />
I have the next XML file:
<?xml version="1.0" encoding="utf-8"?>
<DynamicEntity Name="ProductsMilk">
<Field Name="Name" Type="nvarchar(256)" AllowNulls="false" />
<Field Name="Description" Type="ntext" AllowNulls="false" />
<Field Name="Price" Type="float" AllowNulls="false" />
</DynamicEntity>
I need to read some value from any option of XML-node, for e.g. I need to read the value of Type option from Field node using T-SQL.
How can I do this using internal OpenXML provider in MSSQL?
The following will extract all the data from your XML:
DECLARE #doc VARCHAR(1000) = '<?xml version="1.0" encoding="utf-8"?>
<DynamicEntity Name="ProductsMilk">
<Field Name="Name" Type="nvarchar(256)" AllowNulls="false" />
<Field Name="Description" Type="ntext" AllowNulls="false" />
<Field Name="Price" Type="float" AllowNulls="false" />
</DynamicEntity>';
DECLARE #iDoc INT;
EXECUTE sp_xml_preparedocument #idoc OUTPUT, #doc;
SELECT *
FROM OPENXML(#iDoc, 'DynamicEntity/Field')
WITH
( DynamicEntityName VARCHAR(100) '../#Name',
FieldName VARCHAR(100) '#Name',
[Type] VARCHAR(100) '#Type',
AllowNulls VARCHAR(100) '#AllowNulls'
);
Basically, you just need to specify a column mapping for your xml attributes.
Here is a solution:
DECLARE #x XML = '<?xml version="1.0" encoding="utf-8"?>
<DynamicEntity Name="ProductsMilk">
<Field Name="Name" Type="nvarchar(256)" AllowNulls="false" />
<Field Name="Description" Type="ntext" AllowNulls="false" />
<Field Name="Price" Type="float" AllowNulls="false" />
</DynamicEntity>'
SELECT t.c.value(N'../#Name', N'nvarchar(100)'),
t.c.value(N'#Name', N'nvarchar(100)'),
t.c.value(N'#Type', N'nvarchar(100)'),
t.c.value(N'#AllowNulls', N'nvarchar(100)')
FROM #x.nodes(N'/DynamicEntity/Field') t(c)
I am trying to use SQL templating in smartgwt to load data from my database into a listgrid but I am not able to get the desired result. This is the raw SQL query which I am trying to adopt in SmartGWT to get the result
SELECT
dbo.province.provincename as province,
dbo.province.capital,
dbo.province.code,
dbo.province.telcode,
dbo.province.taxcode,
dbo.county.countyname as county,
dbo.district.districtname as district,
dbo.zone.alternateName as zone,
dbo.neighbourhood.alternateName as neigbhour,
dbo.city.cityname as city,
dbo.city.taxcode,
dbo.city.fdocode,
count(dbo.customer.customeraltname) as countCustomer
FROM
dbo.county
INNER JOIN
dbo.province
ON
(
dbo.county.provinceID = dbo.province.id)
INNER JOIN
dbo.district
ON
(
dbo.county.id = dbo.district.countyID)
INNER JOIN
dbo.city
ON
(
dbo.district.id = dbo.city.districtID)
INNER JOIN
dbo.zone
ON
(
dbo.city.id = dbo.zone.cityId)
INNER JOIN
dbo.neighbourhood
ON
(
dbo.zone.id = dbo.neighbourhood.zoneId)
INNER JOIN
dbo.customer
ON
(
dbo.neighbourhood.id = dbo.customer.neighbourhoodId)
Group By dbo.province.provincename,
dbo.province.capital,
dbo.province.code,
dbo.province.telcode,
dbo.province.taxcode,
dbo.county.countyname,
dbo.district.districtname,
dbo.zone.alternateName,
dbo.neighbourhood.alternateName,
dbo.city.cityname,
dbo.city.taxcode,
dbo.city.fdocode
Below is my ds.xml
<DataSource ID="CusNeiGroupDS" serverType="sql">
<fields>
<field name="id" type="integer" />
<field name="provincename" title="province" type="text"/>
<field name="capital" title="capital" type="text"/>
<field name="code" title="code" type="text"/>
<field name="telcode" title="telcode" type="text"/>
<field name="taxcode" title="taxcode" type="text" />
<field name="provinceId" type="integer" tableName="county"/>
<field name="countyname" title="county" type="text" tableName="county"/>
<field name="district" title="district" type="text" />
<field name="city" title="city" type="text" />
<field name="zone" title="zone" type="text" />
<field name="neighbour" title="neighbour" type="text" />
<field name="taxcodecity" title="taxcodecity" type="text"
/>
<field name="fdocode" title="fdocode" type="text" />
<!-- <field name="countCustomer" title="countCustomer" type="int" /> -->
</fields>
<operationBindings>
<operationBinding operationId="summary"
operationType="fetch">
<selectClause>
districtname as
district,
alternateName as zone,
alternateName as neigbhour,
cityname
as city,
fdocode
</selectClause>
<tableClause>province, county, district, zone, neighbourhood, city
</tableClause>
<whereClause>
province.Id = county.provinceId
AND district.countyId = county.Id
AND city.districtId = district.Id
AND neighbourhood.zoneId = zone.Id
</whereClause>
</operationBinding>
</operationBindings>
the error I get is
Execute of select: SELECT COUNT(*) FROM CusNeiGroupDS WHERE ('1'='1')
on db: SQLServer threw exception: java.sql.SQLException: Invalid
object name 'CusNeiGroupDS'. - assuming stale connection and retrying
query.
But when I put the table name in the datasource like this,I get output but only from that table which I mention and not from the other tables which are joined with FK.
<DataSource ID="CusNeiGroupDS" serverType="sql" tableName="province">
I was able to achieve this with using includeFrom and foreignKey tags in the datasources. Then create another datasource where i use it to include all the coumns I need from the different tables.Like so
<DataSource ID="neighbourDS_1" serverType="sql" tableName="neighbourhood" inheritsFrom="neighbourDS">
<fields>
<field name="provincename" includeFrom="provinceDS.provincename" />
<field name="capital" includeFrom="provinceDS.capital" />
<field name="code" includeFrom="provinceDS.code" />
<field name="telcode" includeFrom="provinceDS.telcode" />
<field name="countyname" includeFrom="countyDS.countyname" />
<field name="district" includeFrom="districtDS.districtname" />
<field name="city" includeFrom="cityDS.cityname" />
<field name="taxcodecity" includeFrom="cityDS.taxcodecity" />
<field name="fdocode" includeFrom="cityDS.fdocode" />
<field name="zone" includeFrom="zoneDS.zone" />
</fields>
</DataSource>
This is my xml
DECLARE #XMLValues XML
SET #XMLValues = '<?xml version="1.0" encoding="UTF-8"?>
<DOCUMENTS name="NYSPIT">
<DOCUMENT ID="140208512T200911101">
<REPEATS>
<REPEAT NAME="EXCEPTIONS">
<ROW>
<FIELD VALUE="09_NYC-3A_2" NAME="PageType"/>
<FIELD VALUE="" NAME="KeyWord"/>
<FIELD VALUE="020852009111001.002" NAME="ImageName"/>
<FIELD VALUE="2" NAME="PageNo"/>
<FIELD VALUE="" NAME="Qualifier"/>
</ROW>
</REPEAT>
</REPEATS>
</DOCUMENT>
<DOCUMENT ID="140208512T200911102">
<REPEATS>
<REPEAT NAME="EXCEPTIONS">
<ROW>
<FIELD VALUE="09_NYC-3A_2" NAME="PageType"/>
<FIELD VALUE="" NAME="KeyWord"/>
<FIELD VALUE="020852009111001.002" NAME="ImageName"/>
<FIELD VALUE="2" NAME="PageNo"/>
<FIELD VALUE="" NAME="Qualifier"/>
</ROW>
</REPEAT>
</REPEATS>
</DOCUMENT>
</DOCUMENTS>
and i need to retrieve the XML node for ID - 140208512T200911101 alone. i cant able to get the information using various methods, still didnt get the correct one.
my desired result should be like this :
<DOCUMENT ID="140208512T200911101">
<REPEATS>
<REPEAT NAME="EXCEPTIONS">
<ROW>
<FIELD VALUE="09_NYC-3A_2" NAME="PageType"/>
<FIELD VALUE="" NAME="KeyWord"/>
<FIELD VALUE="020852009111001.002" NAME="ImageName"/>
<FIELD VALUE="2" NAME="PageNo"/>
<FIELD VALUE="" NAME="Qualifier"/>
</ROW>
</REPEAT>
</REPEATS>
</DOCUMENT>
Please help on this...
Thanks for your support and it is working fine, for getting the #ID value dynamically from a variable we need to user like this :
DECLARE #DCN Varchar(50)
SET #DCN = '140208512T200911101'
select #XMLValues.query('/DOCUMENTS/DOCUMENT[#ID = sql:variable("#DCN")]')
select #XMLValues.query('/DOCUMENTS/DOCUMENT[#ID = "140208512T200911101"]')
Result
<DOCUMENT ID="140208512T200911101">
<REPEATS>
<REPEAT NAME="EXCEPTIONS">
<ROW>
<FIELD VALUE="09_NYC-3A_2" NAME="PageType" />
<FIELD VALUE="" NAME="KeyWord" />
<FIELD VALUE="020852009111001.002" NAME="ImageName" />
<FIELD VALUE="2" NAME="PageNo" />
<FIELD VALUE="" NAME="Qualifier" />
</ROW>
</REPEAT>
</REPEATS>
</DOCUMENT>
I want to create index between two tables, stock and auction. Basically I am working on a product site. So I have to create index on both tables. and they are not related at all.
In data-config.xml, that I created to create index, I wrote the following code
<dataConfig>
<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/database" user="root" password=""/>
<document name="content">
<entity name="stock" query="select ST_StockID,ST_StockCode,ST_Name,ST_ItemDetail from stock where estatus = 'Active' limit 100">
<field column="ST_StockID" name="stock_ST_StockID" />
<field column="ST_StockCode" name="stock_ST_StockCode" />
<field column="ST_Name" name="stock_ST_Name" />
<field column="ST_ItemDetail" name="stock_ST_ItemDetail" />
<entity name="auction" query="select iauctionid,rad_number,vsku,auction_code from auction limit 100">
<field column="iauctionid" name="auction_iauctionid" />
<field column="rad_number" name="auction_rad_number" />
<field column="vsku" name="auction_vsku" />
<field column="auction_code" name="auction_auction_code" />
</entity>
</entity>
</document>
</dataConfig>
and the schema.xml contains the fields are given below.
<field name="stock_ST_StockID" type="string" indexed="true" stored="true" required="true"/>
<field name="stock_ST_StockCode" type="string" indexed="true" stored="true" required="true"/>
<field name="stock_ST_Name" type="string" indexed="true" stored="true" required="true"/>
<field name="stock_ST_ItemDetail" type="text" indexed="true" stored="true" required="true"/>
<field name="auction_iauctionid" type="string" indexed="true" stored="true" required="true"/>
<field name="auction_rad_number" type="string" indexed="true" stored="true" required="true"/>
<field name="auction_vsku" type="string" indexed="true" stored="true" required="true"/>
<field name="auction_auction_code" type="text" indexed="true" stored="true" required="true"/>
But this way the indexes are being created in wrong way as I put the other table data into the first table in data-config.xml. If I create two entity element like given below then the indexes are not being created.
<dataConfig>
<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/lc" user="root" password=""/>
<document name="content">
<entity name="stock" query="select ST_StockID,ST_StockCode,ST_Name,ST_ItemDetail from stock where estatus = 'Active' limit 100">
<field column="ST_StockID" name="stock_ST_StockID" />
<field column="ST_StockCode" name="stock_ST_StockCode" />
<field column="ST_Name" name="stock_ST_Name" />
<field column="ST_ItemDetail" name="stock_ST_ItemDetail" />
</entity>
<entity name="auction" query="select iauctionid,rad_number,vsku,auction_code from auction limit 100">
<field column="iauctionid" name="auction_iauctionid" />
<field column="rad_number" name="auction_rad_number" />
<field column="vsku" name="auction_vsku" />
<field column="auction_code" name="auction_auction_code" />
</entity>
</document>
</dataConfig>
I did not get your answer, can you pls elaborate a little more. I also have the same requirement. I have two tables stock and auction. Basically I am working on a product site. So I have to create index on both tables. and they are not related at all.
Please help
Do you get any errors when indexing the data ??
The following data config is fine as you have two unrelated items.
<dataConfig>
<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/lc" user="root" password=""/>
<document name="content">
<entity name="stock" query="select ST_StockID,ST_StockCode,ST_Name,ST_ItemDetail from stock where estatus = 'Active' limit 100">
<field column="ST_StockID" name="stock_ST_StockID" />
<field column="ST_StockCode" name="stock_ST_StockCode" />
<field column="ST_Name" name="stock_ST_Name" />
<field column="ST_ItemDetail" name="stock_ST_ItemDetail" />
</entity>
<entity name="auction" query="select iauctionid,rad_number,vsku,auction_code from auction limit 100">
<field column="iauctionid" name="auction_iauctionid" />
<field column="rad_number" name="auction_rad_number" />
<field column="vsku" name="auction_vsku" />
<field column="auction_code" name="auction_auction_code" />
</entity>
</document>
</dataConfig>
However, there are few things missing ?
Whats the id field for the entity ? As each document should have a unique id, the configuration seems missing above.
Also the id should be unqiue for the entites, else the stock and auction should overwrite each other.
So you may want the id append as stock_ & auction_
You can also add a static field as Stock and auction to your schema and populate them, which would help you the filter out the results when searching and hence improve the performance.
For Assigning the Ids -
You can use the following to create the id value - This should append the Stock_ with the ST_StockID field value.
<field column="id" template="Stock_#${stock.ST_StockID}" />
OR
Use alias in sql e.g. SELECT 'Stock_' || ST_StockID AS ID ..... as use -
<field column="id" name="id" />