Return a List of typed object via CreateSQLQuery in NHibernate - sql

Been trying to get the following query working for a few hours now and am running out of ideas. Can anyone spot where I'm going wrong. Any pointers much appreciated.
CalEvents = (List<CalEvent>)session.CreateSQLQuery(#"
SELECT *
FROM dbo.tb_calendar_calEvents
INNER JOIN dbo.tb_calEvents
ON (dbo.tb_calendar_calEvents.calEventID = dbo.tb_calEvents.id)
WHERE dbo.tb_calendar_calEvents.calendarID = 'theCalID'"
)
.AddEntity(typeof(CalEvent))
.SetInt64("theCalID", cal.id);
Error:
Kanpeki.NUnit.CalUserTest.Should_return_logged_in_user:
System.ArgumentException : Parameter theCalID does not exist as a
named parameter in [SELECT * FROM dbo.tb_calendar_calEvents INNER JOIN
dbo.tb_calEvents ON (dbo.tb_calendar_calEvents.calEventID =
dbo.tb_calEvents.id) WHERE dbo.tb_calendar_calEvents.calendarID =
'theCalID']

"SELECT * FROM dbo.tb_calendar_calEvents INNER JOIN dbo.tb_calEvents ON (dbo.tb_calendar_calEvents.calEventID = dbo.tb_calEvents.id) WHERE dbo.tb_calendar_calEvents.calendarID = 'theCalID'"
should be
"SELECT * FROM dbo.tb_calendar_calEvents INNER JOIN dbo.tb_calEvents ON (dbo.tb_calendar_calEvents.calEventID = dbo.tb_calEvents.id) WHERE dbo.tb_calendar_calEvents.calendarID = :theCalID"
= 'theCalID' should be written as = :theCalId; :theCalId is how you use named parameters even in Native SQL Queries.

You should remove the query.ExecuteUpdate() call.
Doing the query.List() is enough to issue the query on the session and return the result set.

Related

Convert MS Access Update Query to SQL Query

I have an MS Access Query:
'''UPDATE MyFloridaNetDataDump INNER JOIN RepoVoipAcsNonRecurring ON
'''MyFloridaNetDataDump.service_modified = RepoVoipAcsNonRecurring.Validation
'''SET RepoVoipAcsNonRecurring.Invoice = MyFloridaNetDataDump.Invoice_modified,
'''RepoVoipAcsNonRecurring.Amount = '''Round(MyFloridaNetDataDump.billable_charge*RepoVoipAcsNonRecurring.Percentage,2),
'''RepoVoipAcsNonRecurring.Billing Cycle = MyFloridaNetDataDump.bill_cycle;
I need to convert it to SQL, which I'm using in a .net application.
I have been able to convert most of the query, but when I try to perform the multiplication I get errors depending on what I leave in. Meaning if I take the ROUND function out it doesn't like the * multiplication. If I remove the multiplication line, the query runs.
'''UPDATE MyFloridaNetDataDump
'''SET MyFloridaNetDataDump.InvoiceModified = RepoVoipAcsNonRecurring.Invoice,
'''MyFloridaNetDataDump.BillableCharge * RepoVoipAcsNonRecurring.Percentage = '''RepoVoipAcsNonRecurring.Amount,
'''MyFloridaNetDataDump.BillCycle = RepoVoipAcsNonRecurring.BillingCycle
'''FROM RepoVoipAcsNonRecurring
'''WHERE MyFloridaNetDataDump.ServiceModified = RepoVoipAcsNonRecurring.Validation
I think you inverse all of your set columns
I would suggest you to do like this
UPDATE r
SET
Invoice = m.InvoiceModified,
Amount = Round(m.billablecharge*r.Percentage,2),
[BillingCycle] = m.billCycle
FROM
MyFloridaNetDataDump m
INNER JOIN RepoVoipAcsNonRecurring r ON m.serviceModified = r.Validation

ORA-00936: missing expression Java SQL Exception

I´ve been trying to find the error in this statement for a few hours and can´t seem to find it.
It must have something to do with the AND connecting the two WHERE´s since when deleting the first WHERE it works:
SELECT E_AUFMASS_KOMMENTARE.FIBU_FIRMA,
E_AUFMASS_KOMMENTARE.AUFTR_NR,
E_AUFMASS_KOMMENTARE.KOMMENTAR,
AUFTR_EXT.ART_GRUPPE
FROM HHNG_AU.E_AUFMASS_KOMMENTARE
INNER JOIN HHNG_AU.AUFTR_EXT ON E_AUFMASS_KOMMENTARE.AUFTR_NR = AUFTR_EXT.AUFTR_NR
WHERE (E_AUFMASS_KOMMENTARE.AUFTR_NR = '1248823' )
AND WHERE NOT EXISTS( SELECT * FROM HHNG_AU.EX_KOMMENTARE WHERE EX_KOMMENTARE.AUFTR_NR = '1248823' )
Too many WHERE. You only need where once, then use ANDs and ORs to combine conditions:
SELECT E_AUFMASS_KOMMENTARE.FIBU_FIRMA, E_AUFMASS_KOMMENTARE.AUFTR_NR, E_AUFMASS_KOMMENTARE.KOMMENTAR, AUFTR_EXT.ART_GRUPPE FROM HHNG_AU.E_AUFMASS_KOMMENTARE INNER JOIN HHNG_AU.AUFTR_EXT ON E_AUFMASS_KOMMENTARE.AUFTR_NR = AUFTR_EXT.AUFTR_NR WHERE (E_AUFMASS_KOMMENTARE.AUFTR_NR = '1248823' ) AND NOT EXISTS( SELECT * FROM HHNG_AU.EX_KOMMENTARE WHERE EX_KOMMENTARE.AUFTR_NR = '1248823' )

Oracle sql issue with returning rows that have null values on table joins

I am working on a view (not originally created by myself) and what I am trying to do is return requests (table alias req) that dont have hours allocated to them (table alias te) and i am kinda struggling to get any kind of outer join to return this on the following sql:
SELECT
te.tc_date_yyyy_ww,
te.tc_date_yyyy_mm,
te.tc_date_yyyy,
te.dc_date,
te.nc_request_id_fk,
te.nc_subrequest_id_fk,
te.tc_clock,
te.nc_hours,
te.nc_amount,
te.nc_discipline_id_fk,
te.tc_discipline_desc,
req.tc_request_name,
req.mc_project_cost,
req.nt_fetr_project,
req.nc_bus_unit_id_fk,
req.nc_location_id_fk,
req.nc_cemt_status_id_fk,
coord.tc_user_fname || ' ' || coord.tc_user_lname AS projectcoodname,
assoc.tc_fname || ' ' || assoc.tc_lname AS empname,
assoc_disc.tc_long_desc AS user_discipline,
sr.tc_name AS child_project,
sr.tc_cost_center_or_wbs,
sr.tc_cost_center_nbr,
sr.tc_cost_center_desc,
sr.tc_profit_center_desc,
sr.tc_profit_center_nbr,
sr.tc_wbs_nbr,
sr.tc_wbs_desc,
sr.nc_child_type_id_fk,
sre.nc_estimate,
stat_li.tc_med_desc AS req_status,
NVL(PP.NC_PRIORITY,0) AS PRIORITY
FROM te07fear.tbye10_time_entry te,
te07fear.tbxg100_requests req,
(SELECT *
FROM te07fear.tbye05_form_users
WHERE tc_role_id_fk = 'project_coordinator') coord,
te07fear.tbye07_subrequest sr,
te07fear.tbye08_subrequest_est sre,
te07fear.tbye02_list_items stat_li,
te07fear.tbye04_associates assoc,
te07fear.tbxg95_dropdowns assoc_disc,
TE07FEAR.TBYF43_PROJECT_PRIORITY PP
WHERE
te.tc_clock = assoc.tc_clock
AND
te.nc_subrequest_id_fk = sr.nc_subrequest_id(+)
AND
te.nc_subrequest_id_fk = sre.nc_subrequest_id_fk(+)
AND
te.tc_clock = sre.tc_clock(+)
AND
te.nc_request_id_fk = req.nc_request_id
AND
te.nc_request_id_fk = coord.nc_form_id_fk
AND
assoc.nc_discipline_id_fk = assoc_disc.nc_key_id
AND
req.nc_cemt_status_id_fk = stat_li.nc_value_id
AND
TE.NC_REQUEST_ID_FK = PP.NC_REQUEST_ID_FK
AND
TE.NC_SUBREQUEST_ID_FK = PP.NC_SUB_REQUEST_ID_FK
AND
TE.TC_DATE_YYYY_WW = PP.TC_DATE_YYYY_WW
AND
TE.TC_CLOCK = PP.TC_CLOCK_NUMBER
Any help or ideas would be greatly appreciated.I am assuming that a left outer join should enable me to to this.
Thanks
JC
As far as I understand, you actually need NOT EXISTS clause, not outer join.
Here's a simplified snippet, to show you what I mean (skipped most of the query, but you'l get the idea):
SELECT req.tc_request_name,
req.mc_project_cost,
req.nt_fetr_project,
req.nc_bus_unit_id_fk,
req.nc_location_id_fk,
req.nc_cemt_status_id_fk
FROM te07fear.tbxg100_requests req
WHERE NOT EXISTS (select 1
from te07fear.tbye10_time_entry te
where te.nc_request_id_fk = req.nc_request_id)
;
Let me know if that's the case and if it serves the purpose.
Cheers

Entity framework join with a subquery via linq syntax

I'm trying to translate a sql query in linq sintax, but I'm having big trouble
This is my query in SQL
select * FROM dbo.ITEM item inner join
(
select SUM([QTA_PRIMARY]) QtaTotale,
TRADE_NUM,
ORDER_NUM,
ITEM_NUM
from [dbo].[LOTTI]
where FLAG_ATTIVO=1
group by [TRADE_NUM],[ORDER_NUM],[ITEM_NUM]
)
TotQtaLottiGroupByToi
on item.TRADE_NUM = TotQtaLottiGroupByToi.TRADE_NUM
and item.ORDER_NUM = TotQtaLottiGroupByToi.ORDER_NUM
and item.ITEM_NUM = TotQtaLottiGroupByToi.ITEM_NUM
where item.PRIMARY_QTA > TotQtaLottiGroupByToi.QtaTotale
and item.FLAG_ATTIVO=1
How can I translate into linq sintax?
This approach doesn't work
var res= from i in context.ITEM
join d in
(
from l in context.LOTTI
group l by new { l.TRADE_NUM, l.ORDER_NUM, l.ITEM_NUM } into g
select new TotQtaByTOI()
{
TradeNum = g.Key.TRADE_NUM,
OrderNum = g.Key.ORDER_NUM,
ItemNum = g.Key.ITEM_NUM,
QtaTotale = g.Sum(oi => oi.QTA_PRIMARY)
}
)
on new { i.TRADE_NUM, i.ORDER_NUM, i.ITEM_NUM} equals new { d.TradeNum, d.OrderNum, d.ItemNum }
I get this error
The type of one of the expressions in the join cluase is incorrect. Type inference failed in the call to 'Join'
Can you help me with this query?
Thank you!
The problem is Anonymous Type comparison. You need to specify matching property names for your two anonymous type's properties (e.g. first, second, third)
I tried it out, here's an example: http://pastebin.com/hRj0CMzs

Magento SQL query: Get all simple products that are "not visible individually"

I am looking to write an SQL query directly on the Magento database that gets all Simple products that have the visibility attribute of "not visible individually", for which the value is 1 i believe.
Can anyone help with this? So far I have discovered that the visibility value is set in the table
catalog_product_entity_int
but have been unable to progress further. Thanks
There are many reasons to not do this without the ORM, all of which may (or may not) apply to your needs (store filters, reading data from the correct table, etc). At the very least, you can use the product collection object to build the query which you would run:
$coll = Mage::getModel('catalog/product')->getCollection();
$coll->addAttributeToFilter('visibility' , Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
echo $coll->getSelect();
The resulting query will look like this:
SELECT `e`.*, IF(at_visibility.value_id > 0, at_visibility.value, at_visibility_default.value) AS `visibility`
FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_entity_int` AS `at_visibility_default`
ON (`at_visibility_default`.`entity_id` = `e`.`entity_id`)
AND (`at_visibility_default`.`attribute_id` = '526')
AND `at_visibility_default`.`store_id` = 0
LEFT JOIN `catalog_product_entity_int` AS `at_visibility` ON (`at_visibility`.`entity_id` = `e`.`entity_id`)
AND (`at_visibility`.`attribute_id` = '526')
AND (`at_visibility`.`store_id` = 1)
WHERE (IF(at_visibility.value_id > 0, at_visibility.value, at_visibility_default.value) = '1')
I hope this might work not tested.
$sql = "SELECT * FROM catalog_product_entity_int WHERE visibility=1";
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
foreach ($connection->fetchAll($sql) as $arraytest) {
echo $arraytest['name'];
}