Hibernate formula SQL Server: Generated invalid SQL - sql-server

My XML mapping file is as follows
<property name="lineNo" type="java.lang.Float"
update="false" insert="false"
formula="(select l.[LineNo] from DLVLPoint l where l.StationID=StationID)"/>
The expected SQL statement is:
SELECT (
SELECT l.[LineNo] FROM DLVLPoint l WHERE l.StationID=v.StationID)
FROM view_DLVLImage v WHERE v.ProjectID=181
However, the SQL automatically generated by hibernate is:
SELECT (
SELECT l.v.[LineNo] FROM DLVLPoint l WHERE l.StationID=v.StationID)
FROM view_DLVLImage v WHERE v.ProjectID=181
Added alias v before LineNo, Causes invalid SQL statements.
In SQL server database '[]' is required
I tried to wrap l.[LineNo] with upper(), as follows
<property name="lineNo" type="java.lang.Float"
update="false" insert="false"
formula="(select upper(l.[LineNo]) from DLVLPoint l where l.StationID=StationID)"/>
but, it still didn't solve the problem.

Related

SSIS - Extract from XML dtsx Package every SQLCommand / TableorViewName From Data flow task

Basically, my purpose is urbanization context is to retrieve ALL input Datasource (sqlcommand or tableorviewname) and ALL output datasources
I did a first try succesfully with one of our dtsx packages :
SELECT CONVERT(XML, BulkColumn) AS xContent INTO Packages
FROM OPENROWSET(BULK 'F:\Repos\DW_all\DW_all\anaplan_sales_mrr_v2.dtsx', SINGLE_BLOB) AS x;
SELECT
X.Exe.value('(./#DTS:Description)','nvarchar(20)') as description
,X.Exe.value('(./#DTS:ObjectName)','nvarchar(20)') as ObjectName
,X.Exe.value('(./DTS:ObjectData/pipeline/components/component/properties/property)[1]','nvarchar(25)') as TargettedTable
,X.Exe.value('(./DTS:ObjectData/pipeline/components/component/connections/connection/#connectionManagerRefId)[1]','nvarchar(100)') as TargettedConnect
,X.Exe.value('(./DTS:ObjectData/pipeline/components/component[2]/properties/property[1]/#name)[1]','nvarchar(max)') as SourceType
,X.Exe.value('(./DTS:ObjectData/pipeline/components/component[2]/properties/property)[1]','nvarchar(max)') as [Source]
,X.Exe.value('(./DTS:ObjectData/pipeline/components/component[2]/connections/connection/#connectionManagerRefId)[1]','nvarchar(100)') as SourceConnect
FROM (SELECT XContent AS pkgXML FROM [dbo].[TestPackage]) t
CROSS APPLY pkgXML.nodes('/DTS:Executable/DTS:Executables/DTS:Executable/DTS:Executables/DTS:Executable') X(Exe)
Where X.Exe.value('(./#DTS:Description)[1]','nvarchar(max)') = 'Data Flow Task'
RESULT :
description
ObjectName
TargettedTable
...
Data Flow Task
1rst flowname
First Table
Data Flow Task
Other Flow
other Table
Data Flow Task
Another Flow
another table
Well, till now everything's fine.
Unfortunately, the properties position and the path is now always the same. In my exemple above, target is the first tag in the XML file (that's why [1]) and source is mentioned after (that's why [2]). But in other packages, that's the reverse case.
In the same idea, the property indicating the type of the datasource (name=sqlcommand or name=tableorviewname) is not always in the same position, so the pointer [1] is not relevant.
Moreover, in my exemple above the path is '/DTS:Executable/DTS:Executables/DTS:Executable/DTS:Executables/DTS:Executable' (with one Sequence Container) but other packages don't have container and have a different path (ex: /DTS:Executable/DTS:Executables/DTS:Executable').
I have tried some test with kind of wildcard like [.] or [*] but i'm not confortable with this and my tests are still on failure.
<DTS:ObjectData xmlns:DTS="DTS">
<pipeline
version="1">
<components>
<component
refId="Package\blabla my description id"
componentClassID="Microsoft.OLEDBSource"
contactInfo="OLE DB Source;Microsoft Corporation; Microsoft SQL Server; (C) Microsoft Corporation; All Rights Reserved; http://www.microsoft.com/sql/support;7"
description="OLE DB Source"
name="My DataFlow Name"
usesDispositions="true"
version="7">
<properties>
<property
dataType="System.Int32"
description="The number of seconds before a command times out. A value of 0 indicates an infinite time-out."
name="CommandTimeout">0</property>
<property
dataType="System.String"
description="Specifies the name of the database object used to open a rowset."
name="OpenRowset"></property>
<property
dataType="System.String"
description="Specifies the variable that contains the name of the database object used to open a rowset."
name="OpenRowsetVariable"></property>
<property
dataType="System.String"
description="The SQL command to be executed."
name="SqlCommand"
UITypeEditor="Microsoft.DataTransformationServices.Controls.ModalMultilineStringEditor, Microsoft.DataTransformationServices.Controls, Version=15.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91">SELECT * From OneTable Inner Join AnotherTable ...
</property>
</properties>
</component>
</components>
</pipeline>
</DTS:ObjectData>
Can anyone help me please to improve the initial script to make it efficient for whatever packages, resulting all sqlcommand or tableorviewname in input of the dataflows included in the package, and the same in output.
TIA for your help and advices :-)
Fred.M.

Unwanted square brackets inserted in SQL statement from ORM

I'm trying to make a simple select on my table mapped with sqlalchemy but I can't get it to match the exact table name.
As I noticed, the output of this:
class Users(base):
__tablename__ = "users"
[...]
was
[Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid object name 'users'
And I tried to fix the error by explicitly writing the database I'm refering to
class Users(base):
__tablename__ = "[homework-3-cc-database].users"
[...]
giving the output:
[Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near ']'.
I noticed that it inserted some square brackets, unwanted. Here is their SQL statement:
[SQL: SELECT TOP 1 [[homework-3-cc-database].users].user_id AS [[homework-3-cc-database].users_user_id]
FROM [[homework-3-cc-database].users]]
This statement, ran from DataGrip works just fine:
SELECT TOP 1 [homework-3-cc-schema].users.user_id
FROM [homework-3-cc-schema].users
Do you have any suggestions regarding how should I fix this?
I had to specify the schema name in the following way:
__table_args__ = {"schema": "homework-3-cc-schema"}

SQL Server 2008 xpath on node array

I am trying to run a query against some xml in SQL Server 2008 and I am not getting a result. I have done some online research and came up with the following query. I played around and was able to return the root node, but I need the values from inside and there is an array of call nodes and I need values from it.
WITH XMLNAMESPACES ('http://api.myapi.com/resource' as r)
select c.value('#AgentCall','varchar(max)') as value
from mytable cl
outer apply cl.callinfoxml.nodes('//Call') as q(c)
Sample XML:
<r:ResourceList xmlns:r="http://api.myapi.com/resource" xmlns="http://api.myapi.com/data" totalResults="1">
<Call id="1123570170003">
<FromNumber>14062618272</FromNumber>
<ToNumber>14062618272</ToNumber>
<State>READY</State>
<BatchId>12827094003</BatchId>
<BroadcastId>14633834003</BroadcastId>
<ContactId>818582749003</ContactId>
<Inbound>false</Inbound>
<Created>2016-09-22T06:22:18Z</Created>
<Modified>2016-09-22T06:22:18Z</Modified>
<AgentCall>false</AgentCall>
</Call>
</r:ResourceList>
You're not paying attention to the default XML namespace in your document - you need to reference that as well. Try this code:
WITH XMLNAMESPACES (DEFAULT 'http://api.myapi.com/data',
'http://api.myapi.com/resource' as r)
SELECT
c.value('#AgentCall', 'varchar(max)') AS value
FROM
mytable cl
OUTER APPLY
cl.callinfoxml.nodes('/r:ResourceList/Call') as q(c)

Query XML data in SQL Server 2008 R2

I am trying to parse through an XML in SQL Server 2008 R2 and I am having some issues. I am trying to parse through the items for each parent node, but I'm not getting the solution The XML data is below:
<MerchantInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Active>true</Active>
<CreatedDate>2015-04-16T00:00:00+05:30</CreatedDate>
<CreatedBy>63747</CreatedBy>
<ModifiedBy>63747</ModifiedBy>
<ModifiedUserName>charucsrawat#hpcl</ModifiedUserName>
<MerchantCode>0</MerchantCode>
<RetailOutletName>Gayatri Automobiles</RetailOutletName>
<DealerName>Vandana Singh</DealerName>
<ERPCode>16622710</ERPCode>
<OwnerID>0</OwnerID>
<ICICIAcDetailsRequired>false</ICICIAcDetailsRequired>
<SupplyLocationCode>Mirzapur</SupplyLocationCode>
<IsLive>false</IsLive>
<LiveSAM>1</LiveSAM>
<TestSAM>0</TestSAM>
<OutletCategory>8001</OutletCategory>
<HighwayNo>NH7</HighwayNo>
<HighwayName>Mirzapur Rewa Road</HighwayName>
<SecurityDeposit>10000</SecurityDeposit>
<HSDSaleMonthly>200</HSDSaleMonthly>
<Comments />
<VerifiedDate>0001-01-01T00:00:00</VerifiedDate>
<VerifiedBy>0</VerifiedBy>
<isCloned>false</isCloned>
<VerifiedByUserName />
<ApprovedDate>0001-01-01T00:00:00</ApprovedDate>
<ApprovedBy>0</ApprovedBy>
</MerchantInfo>
my code is :
;WITH XMLNAMESPACES ('http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"' AS mi)
SELECT
T.C.value('mi:erpcode[1]','numeric') as erpcode,
T.C.value('mi:SecurityDeposit[1]','varchar(50)') AS securitydeposit
FROM ChangeEvent ce
CROSS APPLY changeddata.nodes('mi/erpcode[1]') AS T(C)
WHERE Ce.EntityTypeId = 2
AND CAST(Ce.ChangedData AS VARCHAR(MAX)) LIKE '%16622710%'
GO
What I am looking for is:
erpcode securitydeposit
16622710 10000
Kindly help
You don't need ;WITH XMLNAMESPACES for this purpose since XML elements involved in the query doesn't use any namespace prefix and the XML document doesn't have defult namespace too.
Also note that XML element/attribute name are case-sensitive (f.e erpcode != ERPCode) :
SELECT
T.C.value('ERPCode[1]','numeric') as erpcode,
T.C.value('SecurityDeposit[1]','varchar(50)') AS securitydeposit
FROM ChangeEvent ce
CROSS APPLY ChangedData.nodes('MerchantInfo') AS T(C)
WHERE Ce.EntityTypeId = 2
AND T.C.value('ERPCode[1]','numeric') = 16622710
Demo

View from DB2 to SQL Server 2005

I'm attempting to move a view between DB2 and SQL Server.
CREATE VIEW msu.bad_bus_cnty_st_mstr
AS
SELECT id,
bus_cnty_cntry_cd,
bus_st,
bus_zip
FROM summit.mstr
WHERE ( bus_cnty_cntry_cd, bus_st ) IN (SELECT cnty_cntry_cd,
st
FROM uhelp.cnty_cntry_cd
WHERE
cnty_cntry_descr LIKE '%invalid%');
The view works in DB2, but doesn't work with SQL Server because of the WHERE clause. Can I have a recommendation on how to rewrite this view to work with SQL Server?
It usually helps to define what "doesn't work" means (e.g. what error did you get) and also to specify the version of SQL Server you are using.
Unfortunately SQL Server doesn't support IN() with more than one clause. However you can re-write your view this way:
ALTER VIEW msu.bad_bus_cnty_st_mstr
AS
SELECT id,
bus_cnty_cntry_cd,
bus_st,
bus_zip
FROM summit.mstr AS mstr
WHERE EXISTS
(
SELECT 1
FROM uhelp.cnty_cntry_cd
WHERE cnty_cntry_descr LIKE '%invalid%'
AND cnty_cntry_cd = mstr.bus_cnty_cntry_cd
AND st = mstr.bus_st
);
one way
CREATE VIEW msu.bad_bus_cnty_st_mstr
AS
SELECT id,
bus_cnty_cntry_cd,
bus_st,
bus_zip
FROM summit.mstr m
WHERE EXISTS( SELECT 1 FROM uhelp.cnty_cntry_cd c
WHERE c.cnty_cntry_descr LIKE '%invalid%'
AND c.bus_cnty_cntry_cd = m.bus_cnty_cntry_cd
AND c.st = m.bus_st)

Resources