BizTalk adapter generate hierarchical schema - sql

I am trying to use BizTalk adapter (SQL or WCF-SQL) to create a multilevel hierarchical schema. My query is like this
SELECT
CustomerQueueMessage.customer_queue_ID AS CustomerQueueMsgID,
CustomerMaster.customer_ID AS [CustomerID],
CustomerAddressType.address_type_ID AS [AddressTypeID],
CustomerSalesRep.sales_rep_type_code AS [SalesRepTypeCode]
FROM CustomerQueue AS CustomerQueueMessage
INNER JOIN dbo.MDM_Customer AS CustomerMaster ON (CustomerQueueMessage.customer_queue_ID = CustomerMaster.customer_queue_ID)
INNER JOIN dbo.MDM_CustomerAddressType AS CustomerAddressType ON (CustomerAddressType.customer_queue_ID = CustomerMaster.customer_queue_ID)
INNER JOIN dbo.MDM_CustomerSalesRep AS CustomerSalesRep ON (CustomerSalesRep.customer_queue_ID = CustomerMaster.customer_queue_ID)
FOR XML AUTO, TYPE, ELEMENTS, XMLDATA
When I generate the schema using SQL adapter I get this structure
what I want is
Table CustomerSalesRep is under CustomerAddressType. I want CustomerSalesRep to be at the same level with CustomerAddressType, both immediately under CustomerMaster.
If I remove the FOR XML statement and use WCF-SQL adapter to generate the schema, I get no hierarchy, all the columns are at the same level.
Any ideas? Preferably I would like to do it using WCF-SQL adapter, but also SQL would be OK.

You can use this.
SELECT
CustomerSalesRep.sales_rep_type_code AS "CustomerQueueMessage/CustomerMaster/CustomerSalesRep/SalesRepTypeCode",
CustomerAddressType.address_type_ID AS "CustomerQueueMessage/CustomerMaster/CustomerAddressType/AddressTypeID",
CustomerMaster.customer_ID AS "CustomerQueueMessage/CustomerMaster/CustomerID",
CustomerQueueMessage.customer_queue_ID AS "CustomerQueueMessage/CustomerQueueMsgID"
FROM CustomerQueue AS CustomerQueueMessage
INNER JOIN dbo.MDM_Customer AS CustomerMaster ON (CustomerQueueMessage.customer_queue_ID = CustomerMaster.customer_queue_ID)
INNER JOIN dbo.MDM_CustomerAddressType AS CustomerAddressType ON (CustomerAddressType.customer_queue_ID = CustomerMaster.customer_queue_ID)
INNER JOIN dbo.MDM_CustomerSalesRep AS CustomerSalesRep ON (CustomerSalesRep.customer_queue_ID = CustomerMaster.customer_queue_ID)
FOR XML PATH('SAPcustom'), TYPE, ELEMENTS
It generates an XML like this.
<SAPcustom>
<CustomerQueueMessage>
<CustomerMaster>
<CustomerSalesRep>
<SalesRepTypeCode>1</SalesRepTypeCode>
</CustomerSalesRep>
<CustomerAddressType>
<AddressTypeID>1</AddressTypeID>
</CustomerAddressType>
<CustomerID>1</CustomerID>
</CustomerMaster>
<CustomerQueueMsgID>1</CustomerQueueMsgID>
</CustomerQueueMessage>
</SAPcustom>

Related

Spring Data JPA - Left Join with Multiple Criteria

I need to do a join using a JOIN TABLE ON ... AND ... using Spring Data JPA criteria builder.
I know I can do a basic join like so:
Join<ReportEntity, ProductEntity> productJoin = root.join("products", JoinType.LEFT);
But can I specify extra criteria for the join? If not, is there another way to achieve this using the Criteria Builder? This is the SQL query I'd like to reproduce:
SELECT r.id, p.rare
FROM REPORT r
LEFT JOIN PRODUCT p
ON r.id = p.report_id AND p.rare = 1
WHERE p.report_id IS NULL;
Note that specifying p.rare = 1 in the above query in the WHERE clause does not give the desired result, it needs to go in the ON clause.
Join<ReportEntity, ProductEntity> productJoin = root.join("products", JoinType.LEFT);
Predicate joinPredicate = criteriaBuilder.equal(root.get("id"), productJoin.get("reportId"));
Predicate rarePredicate = criteriaBuilder.equal(productJoin.get("rare"), 1);
productJoin.on(joinPredicate, rarePredicate);

Is there any way to use raw sql or i'm stuck with laravel-eloquent?

I'm trying to migrate an application developed in 2007 and add and api to it so I can easy develop and sync data with the new App I started a laravel project with the old database just to create the api the old database diagram look like this: Too many relationships and crazy polymorphic ones at that.
I want to migrate my query to laravel eloquent but I couldn't do it in time I tried to do it with eloquent but it took too much time is there any way to use raw sql like this or i'm stuck with eloquent this is my laravel code : here
the sql query looks like this
SELECT n.DOC_ID, n.TYP_ID, n.TYP_ID,
type_document.TYP_LIBELLE_AR, n.IND_ID,indication.IND_LIBELLE_AR,n.LAN_ID,
langue.LAN_LIBELLE_AR,
n.DOC_TITRE_PROPRE,
n.DOC_TITRE_COMPLEMENT,
n.DOC_TITRE_PARALLELE,
n.DOC_TITRE_ENSEMBLE,
n.DOC_NUMERO_PARTIE,
editeur.EDT_NOM_AR,
editeur.EDT_KEYWORDS,
n.DOC_LIEU_EDITION,
n.DOC_ANNEE,
n.DOC_EDITION,
periodicite.PER_LIBELLE_AR,
n.DIP_ID,
specialite.SPE_LIBELLE_AR,
n.COL_ID,
n.DOC_NUM,
n.COL_NUMERO,
n.COT_NOTICE,
n.SCL_ID,
n.SCL_NUMERO,
n.DOC_NBR_UNITE,
n.DOC_ILLUSTRATION,
n.DOC_FORMAT,
n.DOC_MATERIEL,
n.DOC_ISBN,
n.DOC_ISSN,
n.DOC_NBR_EXEMPLAIRE,
n.STA_ID,
n.REL_VOLUME,
n.PAY_ID,
n.DOC_AGENCE,
n.DOC_PRET_INTERNE,
n.DOC_PRET_EXTERNE,
n.DOC_KEYWORDS,
n.CREATE_DATE,
n.UPDATE_DATE,
statut_notice.STA_LIBELLE_AR,
notice_auteur.VED_ID,
notice_auteur.FON_ID,
notice_auteur.AUT_TYPE,
GROUP_CONCAT(DISTINCT vmath.VED_NOM) AS mats,
vmath.VED_KEYWORDS,
GROUP_CONCAT( DISTINCT vauth.VED_NOM) AS auths,
vauth.VED_KEYWORDS,
GROUP_CONCAT( DISTINCT notice_exemplaire.EXP_COTE) AS examp_cote,
GROUP_CONCAT( DISTINCT notice_exemplaire.LOC_ID) AS exmp_location
FROM notice n
INNER JOIN editeur ON editeur.EDT_ID =n.EDT_ID
INNER JOIN type_document ON type_document.TYP_ID = n.TYP_ID
INNER JOIN langue ON langue.LAN_ID = n.LAN_ID
INNER JOIN statut_notice ON statut_notice.STA_ID = n.STA_ID
INNER JOIN indication ON indication.IND_ID = n.IND_ID
INNER JOIN notice_exemplaire ON n.DOC_ID = notice_exemplaire.DOC_ID
LEFT JOIN specialite ON n.SPE_ID = specialite.SPE_ID
LEFT JOIN periodicite ON periodicite.PER_ID = n.PER_ID
INNER JOIN notice_auteur ON n.DOC_ID = notice_auteur.DOC_ID
INNER JOIN vedette vauth ON notice_auteur.VED_ID = vauth.VED_ID
LEFT JOIN notice_matiere ON n.DOC_ID = notice_matiere.DOC_ID
LEFT JOIN vedette vmath ON notice_matiere.VED_ID = vmath.VED_ID
GROUP BY n.DOC_ID
1:
For something like this you can use DB::select('YOUR-QUERY').
DB::select('...') calls select() on the underlying Connection class so it's not the same as using DB::table('...')->select('...') which is calling the select method on the Builder class.
For more information on running raw queries you can refer to the documentation.

MS Access INNER JOIN/LEFT JOIN problems

I have the following SQL string which tries to combine an INNER JOIN with a LEFT JOIN in the FROM section.
As you can see I use table VIP_APP_VIP_SCENARIO_DETAIL_LE to perform the query. When I use it against this table, Access give me an "Invalid Operation" error.
Interestingly, when I use the EXACT same query using the VIP_APP_VIP_SCENARIO_DETAIL_BUDGET or VIP_APP_VIP_SCENARIO_DETAIL_ACTUALS table, it performs flawlessly.
So why would it work on two tables but not the other? All fields are in all tables and the data types are correct.
As a side note: on the query with the error, if I change the LEFT JOIN to an INNER JOIN, it runs with no problem! I really need a LEFT JOIN though.
SELECT
D.MATERIAL_NUMBER,
D.MATERIAL_DESCRIPTION,
D.PRODUCTION_LOT_SIZE,
D.STANDARDS_NAME,
D.WORK_CENTER,
S.OP_SHORT_TEXT,
S.OPERATION_CODE,
D.LINE_SPEED_UPM,
D.PERCENT_STD,
D.EQUIPMENT_SU,
D.EQUIPMENT_CU,
D.OPERATOR_NUM,
V.COSTING_LOT_SIZE,
V.VOL_TOTAL_ADJ
FROM
([STDS_SCENARIO: TEST] AS D INNER JOIN MASTER_SUMMARY AS S ON
D.MATERIAL_NUMBER = S.MATERIAL_NUMBER AND D.WORK_CENTER = S.WORK_CENTER)
LEFT JOIN
(SELECT ITEM_CODE, COSTING_LOT_SIZE, VOL_TOTAL_ADJ
FROM
VIP_APP_VIP_SCENARIO_DETAIL_LE
WHERE SCENARIO_ID = 16968) AS V ON D.MATERIAL_NUMBER = V.ITEM_CODE
ORDER BY D.MATERIAL_NUMBER, D.STANDARDS_NAME, S.OPERATION_CODE;
tried to mock this up in SQL server with some tables of my own, but the structure seemed to work, this follows the pattern referenced above. (hopefully no syntax errors left here)
SELECT * FROM (
select
D.MATERIAL_NUMBER,
D.MATERIAL_DESCRIPTION,
D.PRODUCTION_LOT_SIZE,
D.STANDARDS_NAME,
D.WORK_CENTER,
S.OP_SHORT_TEXT,
S.OPERATION_CODE,
D.LINE_SPEED_UPM,
D.PERCENT_STD,
D.EQUIPMENT_SU,
D.EQUIPMENT_CU,
D.OPERATOR_NUM
FROM [STDS_SCENARIO: TEST] D
INNER JOIN MASTER_SUMMARY S
ON D.MATERIAL_NUMBER = S.MATERIAL_NUMBER AND D.WORK_CENTER = S.WORK_CENTER) AS J
LEFT JOIN
(SELECT ITEM_CODE, COSTING_LOT_SIZE, VOL_TOTAL_ADJ
FROM
VIP_APP_VIP_SCENARIO_DETAIL_LE
WHERE SCENARIO_ID = 16968) AS V ON J.MATERIAL_NUMBER = V.ITEM_CODE
ORDER BY J.MATERIAL_NUMBER, J.STANDARDS_NAME, J.OPERATION_CODE;
Had help from a friend and we discovered that it was a casting problem between a linked Oracle table and the Access table. To fix the problem we casted both sides of the linked fields to a string:
CSTR(D.[MATERIAL_NUMBER]) = CSTR(V.[ITEM_CODE])

SQL Query: Comparing two dates in returned record

I'm trying to come up with an automated solution for something I do manually now and I only have minimal, bare-bones SQL skill. I usually modify simple queries others have built or will build basic select queries. I have done some reading but don't know how to make it do what I need in this case. I need to come up with something others can use while I am out for a month (and which will save me time when I return).
What I need is to return the fields below where tblThree.EndDate is later than tblFive.ServiceEnd. I have to do a couple of other compares on the dates, but if I get a working query of the first one I can make it work with the others. We use MS SQL Server 2008.
I tried creating sub-queries with aliases and failed miserably at making it work.
These are the table and fields I am working with:
tblOne.ServiceID
tblOne.ServiceYear
tblOne.Status
tblTwo.AccountNbr
tblTwo.AcctName
tblThree.BeginDate (smalldatetime, null)
tblThree.EndDate (smalldatetime, null)
tblFour.ClientID
tblFour.ServiceName
tblFive.ContractID
tblFive.ServiceBegin (smalldatetime, null)
tblFive.ServiceEnd (smalldatetime, null)
This is how the tables are related:
tblOne.ServiceID = tblThree.ServiceID
tblOne.ContractID = tblFive.ContractID
tblOne.ClientID = tblFour.ClientID
tblTwo.AccountNbr = tblFour.Account
I used MS Access 2003 to generate the Join SQL:
SELECT tblOne.ServiceID, tblTwo.AccountNbr,
tblTwo.AcctName, tblFour.ServiceName, tblOne.Status,
tblThree.BeginDate, tblThree.EndDate,
tblOne.ServiceYear, tblFive.ServiceBegin,
tblFive.ServiceEnd
FROM ((tblTwo INNER JOIN tblFour
ON tblTwo.AccountNbr=tblFour.AccountNbr) INNER JOIN (tblThree INNER JOIN tblOne
ON tblThree.ServiceID=tblOne.ServiceID)
ON tblFour.ClientID=tblOne.ClientID) INNER JOIN tblFive
ON tblOne.ContractID=tblFive.ContractID;
Thanks for any help.
Just add a WHERE clause to get started:
SELECT tblOne.ServiceID, tblTwo.AccountNbr,
tblTwo.AcctName, tblFour.ServiceName, tblOne.Status,
tblThree.BeginDate, tblThree.EndDate,
tblOne.ServiceYear, tblFive.ServiceBegin,
tblFive.ServiceEnd
FROM ((tblTwo INNER JOIN tblFour
ON tblTwo.AccountNbr=tblFour.AccountNbr) INNER JOIN (tblThree INNER JOIN tblOne
ON tblThree.ServiceID=tblOne.ServiceID)
ON tblFour.ClientID=tblOne.ClientID) INNER JOIN tblFive
ON tblOne.ContractID=tblFive.ContractID
WHERE tblThree.EndDate > tblFive.ServiceEnd;
SELECT
tblOne.ServiceID,
tblOne.ServiceYear,
tblOne.Status,
tblTwo.AccountNbr,
tblTwo.AcctName,
tblThree.BeginDate,
tblThree.EndDate,
tblFour.ClientID,
tblFour.ServiceName,
tblFive.ContractID,
tblFive.ServiceBegin,
tblFive.ServiceEnd
FROM tblOne
INNER JOIN tblThree
ON tblOne.ServiceID = tblThree.ServiceID
INNER JOIN tblFive
ON tblOne.ContractID = tblFive.ContractID
INNER JOIN tblFour
ON tblOne.ClientID = tblFour.ClientID
INNER JOIN tblTwo
ON tblTwo.AccountNbr = tblFour.Account
WHERE tblThree.EndDate > tblFive.ServiceEnd

How to join dbo.LocalizedLabelView to get form labels in Dynamics CRM?

In Dynamics CRM, I often get requirements from business users to create reports. Business users know and speak about entity display names and attribute labels. To write a query, I need to map those to entity names and attribute names. I would like to use a query to look this up.
To what do I join the dbo.LocalizedLabelView view to get the AttributeLabel column in the following query? I can't figure out what ObjectId is supposed to reference. (And if you can tell me how you figured out the answer I'd be especially appreciative!)
select
[EntityName] = entityNames.Name,
[EntityDisplayName] = entityDisplayNames.Label,
[AttributeName] = attributeNames.PhysicalName,
[AttributeDisplayName] = attributeDisplayNames.Label
--[AttributeLabel] = attributeLabels.Label
from
dbo.EntityView entityNames
inner join dbo.LocalizedLabelView entityDisplayNames
on entityDisplayNames.ObjectId = entityNames.EntityId
and entityDisplayNames.ObjectColumnName = 'LocalizedName'
left outer join dbo.AttributeView attributeNames
on attributeNames.EntityID = entityNames.EntityID
inner join dbo.LocalizedLabelView attributeDisplayNames
on attributeDisplayNames.ObjectId = attributeNames.AttributeID
and attributeDisplayNames.ObjectColumnName = 'DisplayName'
and attributeDisplayNames.LanguageID = entityDisplayNames.LanguageID
--inner join dbo.LocalizedLabelView attributeLabels
-- on attributeLabels.ObjectId = ?????
-- and attributeLabels.LanguageID = entityDisplayNames.LanguageID
where
entityDisplayNames.LanguageID = 1033
order by
entityDisplayNames.Label,
attributeDisplayNames.Label
ObjectId is a reference to the internal ID of a thing in the CRM database. This thing can be an attribute, entity, label, or whatever.
Since you want the label for your attribute, use that attribute's id as the ObjectId here. I think that you want your join condition to look like this:
inner join dbo.LocalizedLabelView attributeLabels
on attributeLabels.ObjectId = attributeNames.AttributeID
and attributeLabels.LanguageID = entityDisplayNames.LanguageID
and attributeLabels.ObjectColumnName = 'DisplayName'
If you want the description for the attribute, you can change the ObjectColumnName to 'Description'.