Gain more Results on enhancing the query on one specific Server - sql

I wrote an Access add-in (VBA) which works perfectly fine on my and other test servers, but on one server I encountered a very strange problem. When I execute the query with the data condition, I get the correct results (which are none). When I add the date condition I get more results than before. These results do not even match with my other conditions. I use the exact same database on both servers.
Here is the Query:
select BE.* from dbo.KHKBuchungserfassung AS BE
left join KHKSachkonten AS SSK on BE.KtoSoll = SSK.SaKto and BE.Mandant = SSK.Mandant
left join KHKSachkonten AS HSK on BE.KtoHaben = HSK.SaKto and BE.Mandant = HSK.Mandant
where ((((BE.KtoSollTyp = 1) or (BE.KtoSollTyp = 2)) and (HSK.Kontenart = 'BF' or HSK.Kontenart = 'BG')) or
(((BE.KtoHabentyp = 1) or (BE.KtoHabentyp = 2)) and (SSK.Kontenart = 'BF' or SSK.Kontenart = 'BG')))
and BE.Mandant= 88 and BE.Buchungsdatum>={d '2016-01-01'} AND BE.Buchungsdatum<={d '2016-06-30'}
If I execute this on that specific server with from date < a specific date ('2016-02-24') it also works perfectly fine (e.g. '2016-01-01' & '2016-06-30'). Any given date higher than this one gives results that violate my first condition... (the one in the parenthesis). I already checked the number of parenthesis. I also rotated the places of the given conditions. In addition to that, I am pretty sure my DBMS would give me an error if there is a problem with my syntax (not sure though).
This is the Result Set:
Dim rs As ADODB.Recordset
sQry = ...
Set rs = goMandant.oData.rsOpenRecordset(sQry, adOpenStatic)
Do Until rs.EOF
...
I am sorry that I can not provide test data due to juridical reasons, but if you would also select SSK.Kontenart and HSK.Kontenart there would be 'NULL' on both sides which is not at all possible in my first condition...
I would also like to note that this is my first question and if I made any mistakes, I would appreciate it if you told me.
Regards
TK

Related

SQL CASE after in WHERE condition with single or multiple where results

I have the following code running in part of excel data query getting information from excel
`
DateTable.FiscalYear IN (?-1,?)
AND DateTable.Week = ?
AND ProductInformation.Brand = 'Drunk Elephant'
AND StoreInformation.Retailer IN CASE
WHEN ? = 'Boots' THEN ('Boots')
WHEN ? = 'Cult Beauty' THEN ('Cult Beauty')
WHEN ? = 'DTC' THEN ('DTC')
WHEN ? = 'Look Fantastic' THEN ('Look Fantastic')
WHEN ? = 'Space NK' THEN ('Space NK')
ELSE (SELECT
DISTINCT StoreInformation.Retailer
FROM
Sellout
INNER JOIN
ProductInformation
ON
Sellout.EAN = ProductInformation.EAN
INNER JOIN
StoreInformation
ON
sellout.StoreNumber = StoreInformation.StoreNumber
WHERE
ProductInformation.Brand = 'Drunk Elephant')
END
`
The ? are referencing cells in excel to bring back values. The code was running originally running fine with = CASE, but the I had to add in the final else clause, which brings back multiple results, so = no longer works. And putting the IN causes it to break, even if I remove the else and have just IN CASE.
Any ideas how to can run this case in the where clause, working if the result of the CASE is either a single value or a sub-query table?
Thanks in advance!
I have tried it with = CASE and without the ELSE, which work. Adding the ELSE brings back an error saying subquery brings back more then 1 value, which is why i tried with IN instead of =.
But having just IN without the ELSE also causes an error.

Hibernate createSQLQuery and Toad SQL query return different results - parameter problems?

I'm a newbie at Hibernate so excuse me if some of this is glaringly obvious but it's been a very, very long day. I am trying to create and execute a simple query in Hibernate and Toad/Oracle.
The Toad/Oracle sql reads:
select
count(*)
from
fpps_owner.fee_claim_payment_lines l,
fpps_owner.fee_claim_payments p
where
l.fee_claim_payment_id = p.fee_claim_payment_id and
p.claim_index = 87167895
The above returns 10 records, which is correct
The following Java code returns 0 records, which is NOT correct
String sLinesAvailable =
"select count(*) from " +
"fpps_owner.fee_claim_payment_lines l, fpps_owner.fee_claim_payments p " +
"where " +
"l.fee_claim_payment_id = p.fee_claim_payment_id and p.claim_index = :id";
Query qLinesAvailable = em.createNativeQuery(sLinesAvailable);
qLinesAvailable.setParameter("id", "87167895"); // fails
qLinesAvailable.setParameter("id", 87167895); // fails
List<Object> out = (List<Object>) qLinesAvailable.getResultList();
BigDecimal x = (BigDecimal) out.get(0);
Returns 0 records. Using .getSingleResult() also returns 0 records.
What am I missing here?
Any help would be GREATLY appreciated!
If you are not seeing any malformed query errors, it seems like the parameter is not binding correctly.
To debug, I'd print out that SQL statement the line after you set the parameter. This is the only way you can see the SQL after the parameter is set in order to compare it with Toad.
What does your binding file look like? Maybe you have a different name in there for the ID, so it's not able to find it based on the name. Trying binding with the parameter's order value, just as a test.
This might give some ideas: http://www.mkyong.com/hibernate/hibernate-parameter-binding-examples/
Best of luck! We've all been there :)
What happens when you try:
(Number) query.getSingleResult();
Your query isn't returning a list, but rather just a count.
Good luck.

Simple, but most likely an incorrect Join is giving a MAX_JOIN_SIZE error

Probably a simple SQL query, but struggling as still learning
The following query runs fine:
SELECT NationalArea. *
FROM NationalArea
WHERE NationalArea.AreaCode = '01922'
This returns about 30 results.
This also runs fine:
SELECT DestinationNames.Name
FROM `DestinationNames`
WHERE DestinationNames.AreaCode = '01922'
This returns just the one
I am trying to run a query that joins the two where the National Area will give a list of area codes and the destination will match those area codes with the names of the towns. The query I have is as follows:
SELECT NationalArea.*, DestinationNames.Name
FROM NationalArea
JOIN DestinationNames
ON NationalArea.AreaCode=DestinationNames.AreaCode
WHERE NationalArea.AreaCode = '01922'
But I get the following error
1104 - The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay
Thanks in advance
You can display the current value with
SHOW VARIABLES LIKE '%MAX_JOIN_SIZE%';
You can change it with:
SET MAX_JOIN_SIZE = 100
Or skip the check entirely with (run this as a separate command before your query):
SET SQL_BIG_SELECTS = 1
But I would first examine why your join returns more than that. It doesn't look like it should. The default value of max_join_size is 4294967295!

SQL Update on joined tables with calculated fields

First of all, I know there are already questions and answers about it, this thread being the one that is closest to what I need:
SQL Update to the SUM of its joined values
However, I get a syntax error (operator missing) that seems to occur close to the FROM clause. However I can't see it. Does it not like the FROM itself ? I am not used to using FROM in an update statement but it seems like it's valid from the QA I just linked :|
Any idea why there would be a syntax error there ?
I am using Access 2007 SP3.
Edit:
Wow, I forgot to post the query...
UPDATE r
SET
r.tempsmoy_requete_min = tmm.moy_mob_requete
FROM
rapports AS r INNER JOIN
(SELECT
id_fichier,
Round(Sum(temps_requete_min)/3,0) As moy_mob_requete,
Round(Sum(temps_analyse_min)/3,0) As moy_mob_analyse,
Round(Sum(temps_maj_min)/3,0) As moy_mob_maj,
Round(Sum(temps_rap_min)/3,0) As moy_mob_rap,
Round(Sum(temps_ddc_min)/3,0) As moy_mob_ddc
FROM maintenances
WHERE
periode In (10,9,8) And
annee=2011
GROUP BY id_fichier) AS tmm ON rapports.id_rapport = tmm.id_fichier
WHERE
1=0
The WHERE 1=0 part is because I want to test further the subquery before running it.
Edit: This is some simpler query I am trying. I get a different error this time. It now tells me that tempsmoy_requete_min (and probably all other left operands) are not part of an aggregate function... which is the point of my query. Any idea ?
UPDATE
rapports INNER JOIN maintenances ON rapports.id_rapport = maintenances.id_fichier
SET
rapports.tempsmoy_requete_min = Round(Sum(temps_requete_min)/3,0),
rapports.tempsmoy_analyse_min = Round(Sum(temps_analyse_min)/3,0),
rapports.tempsmoy_maj_min = Round(Sum(temps_maj_min)/3,0),
rapports.tempsmoy_rap_min = Round(Sum(temps_rap_min)/3,0),
rapports.tempsmoy_ddc_min = Round(Sum(temps_ddc_min)/3,0)
WHERE
maintenances.periode In (10,9,8) And
maintenances.annee=2011 AND
1=0
I tried adapting your first query sample, and was able to make your error go away. However then I encountered a different error ('Operation must use an updateable query').
It may be possible to overcome that error, too. However, I found it easier to use a domain function instead of a join to retrieve the replacement value.
UPDATE rapports
SET tempsmoy_requete_min = Round(DSum("temps_requete_min",
"maintenances",
"periode In (10,9,8) AND annee=2011 "
& "AND id_fichier='" & id_rapport
& "'")/3, 0);
If this suggestion works for tempsmoy_requete_min with your data, you will have to extend it to the other fields you want to replace. That won't be pretty. You could make it less ugly with a saved query which you then use as the "Domain" parameter for DSum() ... that could allow you to use a simpler "Criteria" parameter.
UPDATE r
should be
UPDATE rapports
You can't reliably use an alias in the update target.

My simple sql update query is not working right :(

I've got a simple sql query that is trying to update a single row. The code came from some Linq-to-sql code (i used Profiler to grab it), but please don't worry about the source (L2S) ... that's irrelivant to the question.
Now, when i try and do an update with the where clause, I get 0 rows updated.
I then try and do a select with the same where clause. i get a single result back. Ok, so the data is there.
It's only when i turn off some of the where clause statements does the query finally work. I just don't get it :(
I can't really explain it. So I made a video showing this.
Please watch the video on YouTube here.
Can anyone help? It's really weird :(
Database is MS-SQL 2008.
-- UPDATE
The sql i'm trying to execute (after i've cleaned it up from L2S)..
update tblBoardPost
set IsSticky = 1
where IdBoardPost = 1278
and IdAddress = 212787
and Subject = N'ttreterte'
and Banner is null
and UniqueSubject = N'ttreterte5'
AND (NOT ([IsAnnouncement] = 1))
AND (NOT ([IsSticky] = 1))
AND (NOT ([IsLocked] = 1))
and IsVisible = 1
and IdUserModified = 1
AND [IdNewsArticle] IS NULL
AND [IdList] IS NULL
(note: yes, i know the NOT (blah = 1)) is weird, but that's L2S that made that.
Update 2:
The code in the video is being ran in a transaction/rollback trans. this means that if i do run one of those weird scenario's that works, the change works .. but the rollback undoes it .. so when i run it a 2nd time .. the original value has been returned to the db row.
Also, in the vid, the select query is not exactly the same as the update query .. because i've been trying so many permutations (with no luck) .. so by the time i made the vid .. i forgot to show the original code. That said, the same code in both the select statement and the update/where statement (when i copy/paste on my machine) reproduces the exact same probs :(
Update 2 :)
As per Lieven's request, here's the screenshot showing the code and then the results.
alt text http://img196.imageshack.us/img196/7111/lieven.png
I checked your video, and you should try the same SQL conditions, that you used to return one row in your select query, in your update query. The conditions in the two SQL statements in your video aren't the same, and I don't really care why; they're not apples to apples.
Just copy and paste the working WHERE clause over the non-working one.
In the video, your select goes like
and not IsAnnouncement = 1
wich gives entirely different results as in your update
and not (IsAnnouncement = 1)
It seems you are suffering from an operator precedence issue .
Edit
as the queries don't match in your video and although you say they have matched could you copy paste following script and let us know if or if they do not give you the same results? (I for one would have a hard time believing that the rows affected would differ)
begin tran
select *
from tblBoardPost
where
IdBoardPost = 1278
and IdAddress = 212787
and Subject = N'ttreterte'
and Banner is null
and UniqueSubject = N'ttreterte5'
AND (NOT ([IsAnnouncement] = 1))
AND (NOT ([IsSticky] = 1))
AND (NOT ([IsLocked] = 1))
and IsVisible = 1
and IdUserModified = 1
AND [IdNewsArticle] IS NULL
AND [IdList] IS NULL
update tblBoardPost
set IsSticky = 1
where
IdBoardPost = 1278
and IdAddress = 212787
and Subject = N'ttreterte'
and Banner is null
and UniqueSubject = N'ttreterte5'
AND (NOT ([IsAnnouncement] = 1))
AND (NOT ([IsSticky] = 1))
AND (NOT ([IsLocked] = 1))
and IsVisible = 1
and IdUserModified = 1
AND [IdNewsArticle] IS NULL
AND [IdList] IS NULL
rollback tran