Generate Database Schema information as XML - sql-server

I am trying to generate XML based on an SQL Server Schema. I have followed a few references including: (How to convert database table structure to XML file in sql server?)
After some trial and error, I have gotten as far as:
SELECT TABLE_NAME as '#tablename',
(
SELECT
DATA_TYPE as 'Column/#datatype',
case data_type
when 'nvarchar'
then CHARACTER_MAXIMUM_LENGTH
when 'varchar'
then CHARACTER_MAXIMUM_LENGTH
else null
end as 'Column/#Length',
case IS_NULLABLE
when 'NO' --caseinsensitive by default
then 'False'
when 'YES'
then 'True'
else null
end
AS 'Column/#is_nullable',
Column_Name as 'Column'
FROM INFORMATION_SCHEMA.COLUMNS
where INFORMATION_SCHEMA.COLUMNS.TABLE_NAME =
INFORMATION_SCHEMA.TABLES.TABLE_NAME
order by INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION
-- For XML Path('Column'), type
For XML Path, type
)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='dbo'
ORDER BY TABLE_NAME ASC
For XML PATH ('Table'),Root('Tables')
What I get is:
<Tables>
<Table tablename="Table1">
<row>
<Column datatype="int" is_nullable="False">AA</Column>
</row>
<row>
<Column datatype="nvarchar" Length="50" is_nullable="True">Name</Column>
</row>
<row>
<Column datatype="bit" is_nullable="False">Active</Column>
</row>
<row>
<Column datatype="timestamp" is_nullable="False">CreatedDate</Column>
</row>
</Table>
<Table tablename="Table2">
<row>
<Column datatype="int" is_nullable="False">AA</Column>
</row>
<row>
<Column datatype="datetime2" is_nullable="True">CreatedDate</Column>
</row>
</Table>
</Tables>
The expected output would have the following structure.
<Tables>
<Table tablename="Table1">
<Columns>
<Column datatype="int" is_nullable="False">AA</Column>
<Column datatype="nvarchar" Length="50" is_nullable="True">Name</Column>
<Column datatype="bit" is_nullable="False">Active</Column>
<Column datatype="timestamp" is_nullable="False">CreatedDate</Column>
</Columns>
</Table>
<Table tablename="Table2">
<Columns>
<Column datatype="int" is_nullable="False">AA</Column>
<Column datatype="datetime" is_nullable="True">CreatedDate</Column>
<Columns>
</Table>
</Tables>
Can someone shed some light on
How to remove the individual <row></row> tags and replace with a single set of outer <columns></columns>
how to change datatype= datetime2 to datatype= datetime
Thank you for your help.

Your were close. Just take note of (...) as columns and the Path('') in the subquery
Updated... I missed the datetime requirement
Select TABLE_NAME as '#tablename'
,(
Select case when DATA_TYPE like 'datetime%' then 'datetime' else DATA_TYPE end as 'Column/#datatype',
case data_type
when 'nvarchar'
then CHARACTER_MAXIMUM_LENGTH
when 'varchar'
then CHARACTER_MAXIMUM_LENGTH
else null
end as 'Column/#Length',
case IS_NULLABLE
when 'NO' --caseinsensitive by default
then 'False'
when 'YES'
then 'True'
else null
end AS 'Column/#is_nullable',
Column_Name as 'Column'
FROM INFORMATION_SCHEMA.COLUMNS
where INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = INFORMATION_SCHEMA.TABLES.TABLE_NAME
order by INFORMATION_SCHEMA.COLUMNS.ORDINAL_POSITION
For XML Path(''), type
) AS Columns
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='dbo'
ORDER BY TABLE_NAME ASC
For XML Path('Table'),Root('Tables')
Sample Results
<Tables>
<Table tablename="OD">
<Columns>
<Column datatype="int" is_nullable="False">OD-Nr</Column>
<Column datatype="int" is_nullable="True">OD-Pt</Column>
<Column datatype="int" is_nullable="True">OD-PS</Column>
<Column datatype="varchar" Length="50" is_nullable="False">OD-Class</Column>
<Column datatype="varchar" Length="250" is_nullable="False">OD-Title</Column>
<Column datatype="int" is_nullable="False">OD-LM-Usr</Column>
<Column datatype="datetime" is_nullable="False">OD-LM-UTC</Column>
<Column datatype="int" is_nullable="False">OD-Deleted</Column>
</Columns>
</Table>
<Table tablename="OD-Map">
<Columns>
<Column datatype="int" is_nullable="False">Map-Nr</Column>
<Column datatype="varchar" Length="50" is_nullable="True">Map-Grp</Column>
<Column datatype="varchar" Length="500" is_nullable="True">Map-Val1</Column>
<Column datatype="varchar" Length="500" is_nullable="True">Map-Val2</Column>
<Column datatype="varchar" Length="500" is_nullable="True">Map-Val3</Column>
<Column datatype="varchar" Length="500" is_nullable="True">Map-Val4</Column>
<Column datatype="int" is_nullable="True">Map-LM-Usr</Column>
<Column datatype="datetime" is_nullable="True">Map-LM-UTC</Column>
<Column datatype="bit" is_nullable="True">Map-Deleted</Column>
</Columns>
</Table>
</Tables>

Related

T-SQL how to have multiple criteria in xml.exist function

This code works, but I need to have multiple criteria in the .exist method. "where State = 'FL' And Department = 'HR'" within the same Objects node. I've tried a lot of ways, but no luck. Anyone know how?
Thank you in adance.
If OBJECT_ID(N'tempdb..#xml') Is Not Null
Drop Table #xml
Create Table #Xml
(
RecordId INT IDENTITY(10,1) NOT NULL PRIMARY KEY,
XmlData XML NOT NULL
)
GO
Declare #xml xml =
'<?xml version="1.0" ?>
<DataObject>
<Objects>
<Object Name="FirstName" Value="John" />
<Object Name="LastName" Value="Smith" />
<Object Name="City" Value="Miami" />
<Object Name="State" Value="FL" />
<Object Name="Department" Value="HR" />
</Objects>
</DataObject>'
Insert #xml select #xml
Set #xml =
'<?xml version="1.0" ?>
<DataObject>
<Objects>
<Object Name="FirstName" Value="Jane" />
<Object Name="LastName" Value="Doe" />
<Object Name="City" Value="Hollywood" />
<Object Name="State" Value="FL" />
<Object Name="Department" Value="Accounting" />
</Objects>
</DataObject>'
Insert #xml select #xml
Declare #Dept varchar(30) = 'HR', #State varchar(30) = 'FL'
Select RecordID
From #Xml
Where XMLData.exist('//Objects[Object[#Name="State"][#Value=sql:variable("#State")]]') = 1
Please try the following. The XPath expression is checking exactly for what you need.
SQL
-- DDL and sample data population, start
DECLARE #tbl TABLE
(
RecordId INT IDENTITY(10,1) PRIMARY KEY,
XmlData XML NOT NULL
);
INSERT INTO #tbl (XmlData)
VALUES (N'<?xml version="1.0" ?>
<DataObject>
<Objects>
<Object Name="FirstName" Value="John" />
<Object Name="LastName" Value="Smith" />
<Object Name="City" Value="Miami" />
<Object Name="State" Value="FL" />
<Object Name="Department" Value="HR" />
</Objects>
</DataObject>')
, (N'<?xml version="1.0" ?>
<DataObject>
<Objects>
<Object Name="FirstName" Value="Jane" />
<Object Name="LastName" Value="Doe" />
<Object Name="City" Value="Hollywood" />
<Object Name="State" Value="FL" />
<Object Name="Department" Value="Accounting" />
</Objects>
</DataObject>');
-- DDL and sample data population, end
DECLARE #Dept VARCHAR(30) = 'HR', #State CHAR(2) = 'FL';
SELECT RecordID
FROM #tbl
WHERE XMLData.exist('/DataObject/Objects[Object[#Name="State" and #Value=sql:variable("#State")]
and Object[#Name="Department" and #Value=sql:variable("#Dept")]]') = 1;
Output
+----------+
| RecordID |
+----------+
| 10 |
+----------+

Update Xml Field with replace value of (XML DML)

I face the situation to update an xml field data with mutliple rows within it.
The XML looks like this ( a cart data containing multiple items )
<cart>
<items>
<ItemId>111613</ItemId>
<ItemCode>P.KNT330.01.85.1.1.1</ItemCode>
<ItemName>KANAT330 85 TEAK PETEK ODA PVT</ItemName>
<THUMBNAIL>/Content/images/noimage.jpg</THUMBNAIL>
<Quantity>1</Quantity>
<MU>AD</MU>
<Price_Net>143.50</Price_Net>
<Currency>TL</Currency>
<ExchangeRate>1</ExchangeRate>
<Price_Net_TL>143.50</Price_Net_TL>
<Item_Disc1_Percent>15</Item_Disc1_Percent>
<Item_Disc2_Percent>9</Item_Disc2_Percent>
<Item_Disc3_Percent>0</Item_Disc3_Percent>
<Item_Disc4_Percent>0</Item_Disc4_Percent>
<Item_Disc5_Percent>0</Item_Disc5_Percent>
<Item_Disc6_Percent>0</Item_Disc6_Percent>
<Price_Net_AfterDisc_TL>110.997250</Price_Net_AfterDisc_TL>
<VAT_Percent>18.0</VAT_Percent>
<VAT_TL>19.97950500</VAT_TL>
<Price_Gross_TL>130.97675500</Price_Gross_TL>
<LineDesc />
</items>
<items>
<ItemId>116950</ItemId>
<ItemCode>KS.220.S.0850.51.00</ItemCode>
<ItemName>220 LIK D.TEAK 85 LIK KNT YÖNSÜZ KASA</ItemName>
<THUMBNAIL>/Content/images/noimage.jpg</THUMBNAIL>
<Quantity>1.000</Quantity>
<MU>TK</MU>
<Price_Net>115.20</Price_Net>
<Currency>TL</Currency>
<ExchangeRate>1</ExchangeRate>
<Price_Net_TL>115.20</Price_Net_TL>
<Item_Disc1_Percent>15</Item_Disc1_Percent>
<Item_Disc2_Percent>9</Item_Disc2_Percent>
<Item_Disc3_Percent>0</Item_Disc3_Percent>
<Item_Disc4_Percent>0</Item_Disc4_Percent>
<Item_Disc5_Percent>0</Item_Disc5_Percent>
<Item_Disc6_Percent>0</Item_Disc6_Percent>
<Price_Net_AfterDisc_TL>89.107200</Price_Net_AfterDisc_TL>
<VAT_Percent>18.0</VAT_Percent>
<VAT_TL>16.03929600</VAT_TL>
<Price_Gross_TL>105.14649600</Price_Gross_TL>
<LineDesc />
</items>
<items>
<ItemId>112357</ItemId>
<ItemCode>PRV.071.S.51</ItemCode>
<ItemName>PERVAZ 70 DÜZ D.TEAK TAKIM</ItemName>
<THUMBNAIL>/Content/images/noimage.jpg</THUMBNAIL>
<Quantity>1.000</Quantity>
<MU>TK</MU>
<Price_Net>45.80</Price_Net>
<Currency>TL</Currency>
<ExchangeRate>1</ExchangeRate>
<Price_Net_TL>45.80</Price_Net_TL>
<Item_Disc1_Percent>15</Item_Disc1_Percent>
<Item_Disc2_Percent>9</Item_Disc2_Percent>
<Item_Disc3_Percent>0</Item_Disc3_Percent>
<Item_Disc4_Percent>0</Item_Disc4_Percent>
<Item_Disc5_Percent>0</Item_Disc5_Percent>
<Item_Disc6_Percent>0</Item_Disc6_Percent>
<Price_Net_AfterDisc_TL>35.426300</Price_Net_AfterDisc_TL>
<VAT_Percent>18.0</VAT_Percent>
<VAT_TL>6.37673400</VAT_TL>
<Price_Gross_TL>41.80303400</Price_Gross_TL>
<LineDesc />
</items>
<items>
<ItemId>108561</ItemId>
<ItemCode>34016-13030</ItemCode>
<ItemName>IÇ ODA KAPI KILIDI NIKEL 141-45</ItemName>
<THUMBNAIL>/Content/images/noimage.jpg</THUMBNAIL>
<Quantity>1.000</Quantity>
<MU>AD</MU>
<Price_Net>10.35</Price_Net>
<Currency>TL</Currency>
<ExchangeRate>1</ExchangeRate>
<Price_Net_TL>10.35</Price_Net_TL>
<Item_Disc1_Percent>15</Item_Disc1_Percent>
<Item_Disc2_Percent>9</Item_Disc2_Percent>
<Item_Disc3_Percent>0</Item_Disc3_Percent>
<Item_Disc4_Percent>0</Item_Disc4_Percent>
<Item_Disc5_Percent>0</Item_Disc5_Percent>
<Item_Disc6_Percent>0</Item_Disc6_Percent>
<Price_Net_AfterDisc_TL>8.005725</Price_Net_AfterDisc_TL>
<VAT_Percent>18.0</VAT_Percent>
<VAT_TL>1.44103050</VAT_TL>
<Price_Gross_TL>9.44675550</Price_Gross_TL>
<LineDesc />
</items>
<items>
<ItemId>108568</ItemId>
<ItemCode>34026-11160</ItemCode>
<ItemName>ADOKAPI KÖSE BIRL TKZ TAKIMI</ItemName>
<THUMBNAIL>/Content/images/noimage.jpg</THUMBNAIL>
<Quantity>1.000</Quantity>
<MU>AD</MU>
<Price_Net>1.80</Price_Net>
<Currency>TL</Currency>
<ExchangeRate>1</ExchangeRate>
<Price_Net_TL>1.80</Price_Net_TL>
<Item_Disc1_Percent>15</Item_Disc1_Percent>
<Item_Disc2_Percent>9</Item_Disc2_Percent>
<Item_Disc3_Percent>0</Item_Disc3_Percent>
<Item_Disc4_Percent>0</Item_Disc4_Percent>
<Item_Disc5_Percent>0</Item_Disc5_Percent>
<Item_Disc6_Percent>0</Item_Disc6_Percent>
<Price_Net_AfterDisc_TL>1.392300</Price_Net_AfterDisc_TL>
<VAT_Percent>18.0</VAT_Percent>
<VAT_TL>0.25061400</VAT_TL>
<Price_Gross_TL>1.64291400</Price_Gross_TL>
<LineDesc />
</items>
<items>
<ItemId>108543</ItemId>
<ItemCode>34006-70370</ItemCode>
<ItemName>ADOKAPI MENTESE 10401.0000.0.2</ItemName>
<THUMBNAIL>/Content/images/noimage.jpg</THUMBNAIL>
<Quantity>3.000</Quantity>
<MU>AD</MU>
<Price_Net>1.30</Price_Net>
<Currency>TL</Currency>
<ExchangeRate>1</ExchangeRate>
<Price_Net_TL>1.30</Price_Net_TL>
<Item_Disc1_Percent>15</Item_Disc1_Percent>
<Item_Disc2_Percent>9</Item_Disc2_Percent>
<Item_Disc3_Percent>0</Item_Disc3_Percent>
<Item_Disc4_Percent>0</Item_Disc4_Percent>
<Item_Disc5_Percent>0</Item_Disc5_Percent>
<Item_Disc6_Percent>0</Item_Disc6_Percent>
<Price_Net_AfterDisc_TL>1.005550</Price_Net_AfterDisc_TL>
<VAT_Percent>18.0</VAT_Percent>
<VAT_TL>0.18099900</VAT_TL>
<Price_Gross_TL>1.18654900</Price_Gross_TL>
<LineDesc />
</items>
<items>
<ItemId>108575</ItemId>
<ItemCode>34026-90300</ItemCode>
<ItemName>ADOKAPI ROZETI NIKEL 20298.0000.0.2</ItemName>
<THUMBNAIL>/Content/images/noimage.jpg</THUMBNAIL>
<Quantity>2.000</Quantity>
<MU>AD</MU>
<Price_Net>0.45</Price_Net>
<Currency>TL</Currency>
<ExchangeRate>1</ExchangeRate>
<Price_Net_TL>0.45</Price_Net_TL>
<Item_Disc1_Percent>15</Item_Disc1_Percent>
<Item_Disc2_Percent>9</Item_Disc2_Percent>
<Item_Disc3_Percent>0</Item_Disc3_Percent>
<Item_Disc4_Percent>0</Item_Disc4_Percent>
<Item_Disc5_Percent>0</Item_Disc5_Percent>
<Item_Disc6_Percent>0</Item_Disc6_Percent>
<Price_Net_AfterDisc_TL>0.348075</Price_Net_AfterDisc_TL>
<VAT_Percent>18.0</VAT_Percent>
<VAT_TL>0.06265350</VAT_TL>
<Price_Gross_TL>0.41072850</Price_Gross_TL>
<LineDesc />
</items>
<items>
<ItemId>115240</ItemId>
<ItemCode>34024-35016</ItemCode>
<ItemName>TPE ADOKAPI CONTA AÇIK KAHVE</ItemName>
<THUMBNAIL>/Content/images/noimage.jpg</THUMBNAIL>
<Quantity>5.000</Quantity>
<MU>MT</MU>
<Price_Net>0.36</Price_Net>
<Currency>TL</Currency>
<ExchangeRate>1</ExchangeRate>
<Price_Net_TL>0.36</Price_Net_TL>
<Item_Disc1_Percent>15</Item_Disc1_Percent>
<Item_Disc2_Percent>9</Item_Disc2_Percent>
<Item_Disc3_Percent>0</Item_Disc3_Percent>
<Item_Disc4_Percent>0</Item_Disc4_Percent>
<Item_Disc5_Percent>0</Item_Disc5_Percent>
<Item_Disc6_Percent>0</Item_Disc6_Percent>
<Price_Net_AfterDisc_TL>0.278460</Price_Net_AfterDisc_TL>
<VAT_Percent>18.0</VAT_Percent>
<VAT_TL>0.05012280</VAT_TL>
<Price_Gross_TL>0.32858280</Price_Gross_TL>
<LineDesc />
</items>
<GenDiscPercent_1 xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:nil="true" />
<GenDiscPercent_2 xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:nil="true" />
<GenDiscPercent_3 xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:nil="true" />
<ResChar1 />
<ResChar2>A-0201-1748</ResChar2>
<ResChar3 />
<ResNum1 xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:nil="true" />
<ResNum2 xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:nil="true" />
<ResNum3 xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:nil="true" />
<ResNum4 xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:nil="true" />
<ResDate1>2016-02-18T00:00:00</ResDate1>
<ResDate2>2016-02-18T00:00:00</ResDate2>
<ResDate3 xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:nil="true" />
<ResDate4 xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:nil="true" />
</cart>
How to modify (let's say) the price of a specific item (with ItemId 116950) in this xml field data?
I assume, that your table contains several rows with the same structure. Do you want to change the price in one row, or - might be dangerous - in all rows?
For this I assume, that you want to change one row. Therefore I'll add a WHERE-clause to the end. If you have to change all, you can change this with CART_DATA.exist(...)=1. This was the fastes XML-based approach to get only rows which really contain the given ItemId.
But: Why is this XML?
The design cries for a classical 1:n-relation via physically defined tables. XML is good for final storage (archives), for interfaces, web-service (data transmission in general), but not for heavily moving data, or data you want to look into with filters or statistical question.
A solution
Nevertheless, this is the code:
Change #YourTable and YourXMLColumn to the actual names.
--Two variables:
DECLARE #itemID INT=116950;
DECLARE #newPrice DECIMAL(10,4)=0.99;
--This will display the price as-is
SELECT t.YourXMLColumn.value('(/cart/items[ItemId=sql:variable("#itemID")]/Price_Net/text())[1]','decimal(10,4)')
FROM #YourTable AS t
WHERE t.ID=1;
--This will modify the price
UPDATE #YourTable
SET YourXMLColumn.modify(N'replace value of (/cart/items[ItemId=sql:variable("#itemID")]/Price_Net/text())[1] with sql:variable("#newPrice")')
WHERE ID=1;
--This will display the new price if the operation was successfull
SELECT t.YourXMLColumn.value('(/cart/items[ItemId=sql:variable("#itemID")]/Price_Net/text())[1]','decimal(10,4)')
FROM #YourTable AS t
WHERE t.ID=1;
Let's say the xml field is called CART_DATA in the table called GM_CART.
We will also use sql-variables as in most cases you will need to run your query with parameters.
DECLARE #cartid int = 1000, #itemid int = 116950, #newprice float = 99.90
UPDATE GM_CART
SET CART_DATA.modify('replace value of (/cart/items/Price_Net/text()[.>>(/cart/items/ItemId[.=sql:variable("#itemid")])[1] ])[1]
with sql:variable("#newprice")')
WHERE CARTID=#cartid
to update other nodes if neccesary, you have to call the sql update statement seperately for each one of them

SQL Server bulk insert XML format file

I am trying to load a fixed width text file using Bulk Insert and a XML format file. I have used the same process and XML file on another fixed width, except with less columns.
Error
Msg 4857, Level 16, State 1, Line 16
Line 4 in format file "\\PATHC\addr.xml": Attribute "type" could not be specified for this type.
SQL Server Table
create table [dbo].[raw_addr](
address_number varchar(max),
addr_linel varchar(max),
addr_line2 varchar(max),
street_no varchar(max),
street_name varchar(max),
street_type varchar(max),
locality varchar(max),
[state] varchar(max),
country varchar(max),
postcode varchar(max)
);
SQL Server Bulk Insert
BULK INSERT [dbo].[raw_addr] FROM '\\PATH\addr.txt'
WITH (
FORMATFILE = '\\PATH\addr.xml',
ROWTERMINATOR='\r\n');
XML Code:
<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema_instance">
<RECORD>
<FIELD ID="1" xsi:type="CharFixed" LENGTH="9" />
<FIELD ID="2" xsi:type="CharFixed" LENGTH="50" />
<FIELD ID="3" xsi:type="CharFixed" LENGTH="50" />
<FIELD ID="4" xsi:type="CharFixed" LENGTH="10" />
<FIELD ID="5" xsi:type="CharFixed" LENGTH="50" />
<FIELD ID="6" xsi:type="CharFixed" LENGTH="10" />
<FIELD ID="7" xsi:type="CharFixed" LENGTH="30" />
<FIELD ID="8" xsi:type="CharFixed" LENGTH="3" />
<FIELD ID="9" xsi:type="CharFixed" LENGTH="30" />
<FIELD ID="10" xsi:type="CharTerm" TERMINATOR="\r\n" LENGTH="4" />
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="address_number" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="2" NAME="addr_linel" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="3" NAME="addr_line2" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="4" NAME="street_no" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="5" NAME="street_name" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="6" NAME="street_type" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="7" NAME="locality" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="8" NAME="state" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="9" NAME="country" xsi:type="SQLNVARCHAR" />
<COLUMN SOURCE="10" NAME="postcode" xsi:type="SQLNVARCHAR" />
</ROW>
</BCPFORMAT>
Example TXT extract
001044057C/- XXXXXX XXXXXXX 0000
001295978XXXX 0000
0013974311 0000
00124485712 0000
0012390352 0000
0014720345 0000
0014792876 0000
000986525 ARABANOO GRENFELL NSW 2810
000986589 PO BOX XX KEMPSEY NSW 2440
000740594 'RUSSLEY' ABERDEEN NSW 2336
000311516 BUNYARA ABERDEEN NSW 2336
000298796 CAMBRIA ABERDEEN NSW 2336
000540611 HALCOMBE ABERDEEN NSW 2336
000513112 'LARK HILL' ABERDEEN NSW 2336
000612955 FAN HILL ABERMAIN NSW 2326
001109439 BRAYWOOD ADAMINABY NSW 2630
000460864 TARA ADAMINABY NSW 2630
000315297 ADAMSTOWN NSW 2289
000470057 COORUMBENE ADELONG NSW 2729
000491941 NACKI ADELONG NSW 2729
I think your problem is in the SQL statement, try removing the ROWTERMINATOR. It is not necessary since you specify it in the format file.

Many-to-many relationship with columns in the association in propel

I want to have in generated by Propel objects function e.g. getUsersPrivileges(). I have two tables Users and Privileges described as;
<table name="users" phpName="User">
<column name="user_id" type="integer" required="true" autoIncrement="true" primaryKey="true" />
<column name="login" type="longvarchar" required="true" />
<column name="password" type="longvarchar" required="true" />
<column name="email" type="longvarchar" required="true" />
<column name="language" type="longvarchar" required="true" defaultValue="en" />
<column name="salt1" type="longvarchar" required="true" />
<column name="salt2" type="longvarchar" required="true" />
<column name="date_created" type="timestamp" required="true" />
<column name="archived" type="integer" required="true" defaultValue="0" />
</table>
<table name="privileges" phpName="Privilege">
<column name="privilege_id" type="integer" required="true" autoIncrement="true" primaryKey="true" />
<column name="name" type="longvarchar" required="true" />
<column name="description" type="longvarchar" required="true" />
</table>
<table name="users_privileges" phpName="UserPrivilege" isCrossRef="true">
<column name="user_privilege_id" type="integer" required="true" autoIncrement="true" unique="true" primaryKey="true" />
<column name="user_id" type="integer" />
<column name="privilege_id" type="integer" />
<column name="archived" type="integer" required="true" defaultValue="0" />
<foreign-key foreignTable="users">
<reference local="user_id" foreign="user_id"/>
</foreign-key>
<foreign-key foreignTable="privileges">
<reference local="privilege_id" foreign="privilege_id"/>
</foreign-key>
</table>
I want to have a user_privilege_id as primary key in SQL schema, but Propel requires to generated this method that columns user_id and privilege_id as primary key and foreign key to users.user_id and privilege_id to privileges.privilege_id. It is good to have any additional column in this table.
How to tell Propel to join these tables through these columns and no primary key?
It doesn't work. PrimaryKeys are the only way Propel knows which relations from left to right should created. If you add additional PKs it will create different relation methods. If you have a completely different PK Propel is unable to check which outgoing relations from the cross-table needs to be cross linked from user to privileges and vice-versa. If you really need a ID, you can add this id column as normal with a unique constrain and auto-increment.
Or alternative, you don't use isCrossRef and write your relation methods from user to privileges and vice-versa on your own.

NHibernate and sql timestamp columns as version

I've been racking my head trying to get Nhibernate to work with a byte
array as version mapping to an sql timestamp. I'd implemented an
IUserVersionType but Nhibernate was creating varbinary in the database
rather than timestamp. Inspired by a blog post by Ayende recently on
concurrency, I changed my mapping to specify the sql-type to timestamp
which worked perfectly. However I now face a rather curious problem
wherein Nhibernate does an insert, gets the new version and then
immediately tries to do an update and attempts to set the version
column, which being an sql timestamp fails.
This is my mapping:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Core.Domain, Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" namespace="Core.Domain.Entities"
default-lazy="false">
<class name="Contact" table="Contacts" xmlns="urn:nhibernate-
mapping-2.2" optimistic-lock="version" dynamic-insert="true" dynamic-
update="true">
<id name="Id" type="Int32" column="Id">
<generator class="identity" />
</id>
<version name="Version" type="BinaryBlob" generated="always"
unsaved-value="null">
<column name="Version" sql-type="timestamp" not-null="false" />
</version>
<property name="Title" type="String">
<column name="Title" length="5" />
</property>
<property name="FirstName" type="String">
<column name="FirstName" not-null="true" length="50" />
</property>
<property name="MiddleName" type="String">
<column name="MiddleName" length="50" />
</property>
<property name="LastName" type="String">
<column name="LastName" not-null="true" length="50" />
</property>
<property name="Suffix" type="String">
<column name="Suffix" length="5" />
</property>
<property name="Email" type="String">
<column name="Email" length="50" />
</property>
<bag name="PhoneNumbers" inverse="true" cascade="all-delete-
orphan">
<key foreign-key="FK_Contacts_PhoneNumbers_ContactId" on-
delete="cascade" column="ContactId" />
<one-to-many class="Core.Domain.Entities.PhoneNumber,
Core.Domain, Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" />
</bag>
<property name="DateCreated" type="DateTime">
<column name="DateCreated" />
</property>
<property name="DateModified" type="DateTime">
<column name="DateModified" />
</property>
<property name="LastModifiedBy" type="String">
<column name="LastModifiedBy" />
</property>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Core.Domain, Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" namespace="Core.Domain.Entities"
default-lazy="false">
<class name="Customer" table="Customers" xmlns="urn:nhibernate-
mapping-2.2" optimistic-lock="version" dynamic-insert="true" dynamic-
update="true">
<id name="Id" type="Int32" column="Id">
<generator class="identity" />
</id>
<version name="Version" type="BinaryBlob" generated="always"
unsaved-value="null">
<column name="Version" sql-type="timestamp" not-null="false" />
</version>
<property name="AccountNumber" access="nosetter.pascalcase-
underscore" type="String">
<column name="AccountNumber" unique="true" length="25" />
</property>
<!-- other mappings... -->
<property name="DateCreated" type="DateTime">
<column name="DateCreated" />
</property>
<property name="DateModified" type="DateTime">
<column name="DateModified" />
</property>
<property name="LastModifiedBy" type="String">
<column name="LastModifiedBy" />
</property>
<joined-subclass name="Core.Domain.Entities.Individual,
Core.Domain, Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" table="Individuals">
<key column="CustomerId" />
<many-to-one fetch="join" lazy="false" not-null="true"
cascade="all" unique="true" not-found="exception" name="Contact"
column="ContactID" />
<bag name="Addresses" table="Addresses_Individuals">
<key column="AddressId" foreign-
key="FK_Addresses_Individuals_Addresses_AddressId" />
<many-to-many column="IndividualId"
class="Core.Domain.Entities.Address, Core.Domain,
Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" foreign-
key="FK_Addresses_Individuals_Individuals_IndividualId" />
</bag>
</joined-subclass>
<joined-subclass name="Core.Domain.Entities.Store, Core.Domain,
Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" table="Stores">
<key column="CustomerId" />
<many-to-one unique="true" cascade="save-update" fetch="join"
not-null="true" not-found="exception" name="Address"
column="AddressId" />
<many-to-one lazy="proxy" not-null="true" cascade="all" not-
found="exception" name="Client" column="ClientId" />
<property name="StoreName" type="String">
<column name="StoreName" not-null="true" length="50" />
</property>
<bag name="Contacts" table="Contacts_Stores">
<key column="ContactId" foreign-
key="FK_Contacts_Stores_Contacts_ContactId" />
<many-to-many column="StoreId"
class="Core.Domain.Entities.Contact, Core.Domain,
Version=0.1.3397.31993, Culture=neutral,
PublicKeyToken=94dc7dc697cfcfc0" foreign-
key="FK_Contacts_Stores_Stores_StoreId" />
</bag>
</joined-subclass>
</class>
</hibernate-mapping>
Calling Session.Save on an Individual with associated Contact results
in the following error:
NHibernate: INSERT INTO Addresses (Line1, PostalCode, Country,
DateCreated, DateModified, LastModifiedBy) VALUES (#p0, #p1, #p2, #p3,
#p4, #p5); select SCOPE_IDENTITY(); #p0 = 'Order Address Line 1', #p1
= 'CV31 6BW', #p2 = 'United Kingdom', #p3 = '20/04/2009 19:45:32', #p4
= '20/04/2009 19:45:32', #p5 = ''
NHibernate: SELECT address_.Version as Version22_ FROM Addresses
address_ WHERE address_.Id=#p0; #p0 = '1'
NHibernate: INSERT INTO Contacts (FirstName, LastName, DateCreated,
DateModified, LastModifiedBy) VALUES (#p0, #p1, #p2, #p3, #p4); select
SCOPE_IDENTITY(); #p0 = 'Joe', #p1 = 'Bloggs', #p2 = '20/04/2009
19:45:34', #p3 = '20/04/2009 19:45:34', #p4 = ''
NHibernate: SELECT contact_.Version as Version33_ FROM Contacts
contact_ WHERE contact_.Id=#p0; #p0 = '1'
NHibernate: INSERT INTO Customers (AccountNumber, DateCreated,
DateModified, LastModifiedBy) VALUES (#p0, #p1, #p2, #p3); select
SCOPE_IDENTITY(); #p0 = '', #p1 = '20/04/2009 19:45:34', #p2 =
'20/04/2009 19:45:34', #p3 = ''
NHibernate: INSERT INTO Individuals (ContactID, CustomerId) VALUES
(#p0, #p1); #p0 = '1', #p1 = '1'
NHibernate: SELECT individual_1_.Version as Version2_ FROM Individuals
individual_ inner join Customers individual_1_ on
individual_.CustomerId=individual_1_.Id WHERE
individual_.CustomerId=#p0; #p0 = '1'
NHibernate: UPDATE Contacts SET Version = #p0 WHERE Id = #p1 AND
Version = #p2; #p0 = 'System.Byte[]', #p1 = '1', #p2 = 'System.Byte[]'
System.Data.SqlClient.SqlException: Cannot update a timestamp column.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception,
Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning
(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,
SqlCommand cmdHandler, SqlDataReader dataStream,
BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject
stateObj)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader
ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds
(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean
returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior
cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String
method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery
(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at NHibernate.AdoNet.AbstractBatcher.ExecuteNonQuery(IDbCommand cmd)
in c:\CSharp\NH\nhibernate\src\NHibernate\AdoNet\AbstractBatcher.cs:
line 203
at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object
id, Object[] fields, Object[] oldFields, Object rowId, Boolean[]
includeProperty, Int32 j, Object oldVersion, Object obj,
SqlCommandInfo sql, ISessionImplementor session) in c:\CSharp\NH
\nhibernate\src\NHibernate\Persister\Entity
\AbstractEntityPersister.cs: line 2713
NHibernate.Exceptions.GenericADOException: could not update:
[Core.Domain.Entities.Contact#1][SQL: UPDATE Contacts SET Version =
#p0 WHERE Id = #p1 AND Version = #p2]
Any ideas why NHibernate is attempting to update the version column
for Contact, even though it didn't for the Address?
I have found that using dynamic-insert="true" on the class along with causes this issue. I use the following mapping successfully:
...
<class name="Contact" table="Contact">
<id name="ID" column="ID" type="int">
<generator class="identity" />
</id>
<version name="Version" generated="always" unsaved-value="null" type="BinaryBlob"/>
...
The Address doesn't have a version column I assume.
I wonder where you have the sql-type from. Why not this way?
<version name="Version" type="Timestamp" generated="always" unsaved-value="null">
<column name="Version" not-null="false" />
</version>
You need of course a DateTime in the entity.
http://ayende.com/Blog/archive/2009/04/15/nhibernate-mapping-concurrency.aspx

Resources