×
The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.
have you checked the SQLCODE or SQLSTATE immediately after the
Count-Statement?
Either declare these fields and check them or use the GET DIAGNOSTICS
statement to get the SQLCODE or SQLSTATE (or even the Message Text).
Mit freundlichen Grüßen / Best regards
Birgitta Hauser
"Shoot for the moon, even if you miss, you'll land among the stars." (Les
Brown)
"If you think education is expensive, try ignorance." (Derek Bok)
"What is worse than training your staff and losing them? Not training them
and keeping them!"
-----Ursprüngliche Nachricht-----
Von: midrange-l-bounces@xxxxxxxxxxxx
[mailto:midrange-l-bounces@xxxxxxxxxxxx] Im Auftrag von Charles Wilt
Gesendet: Saturday, 04. April 2009 20:40
An: Midrange Systems Technical Discussion
Betreff: Strange SQL problem, same where clause works in one statement but
not the other....
All,
Running into a very strange SQL problem. I'm building two dynamic
statements, one to get the count of the number of rows to be returned
and one to actually return them. I use the same WEHER clause in both
places. But the count statement is returning a zero row count, but
the actual select returns one row...
Here's the releivent parts of the code"
DECLARE wDesc VARCHAR ( 31 ) ;
set wSQLselect =
'select row_number() Over (order by jordte desc, '
CONCAT ' description asc,'
CONCAT ' ID asc) as rowNbr,'
CONCAT ' cast(? as integer) as Total_Rows,'
CONCAT ' CVTOLSDATE(JORDTE) as Order_Date,'
CONCAT ' Description, size, ID, TypeName,'
CONCAT ' deacode, Qty_Ordered, qty_shipped,'
CONCAT ' invoice_price, '
concat ' cast(round(qty_shipped * Invoice_price,2) as decimal (11,2)) '
concat ' as Extended_Amount, '
concat ' Invoice_Number,'
CONCAT ' message';
set wSQLFromAndWhere =
' from Customer_Item_History A'
CONCAT ' where jdivsn = 0 and jcusnr = ?'
CONCAT ' and jordte between ? and ?'
concat ' AND ( ( cast(? as varchar(30)) IS NULL ) '
concat ' OR ( A.DESCRIPTION LIKE ? ) ) '
CONCAT ' and ((cast(? as integer) is NULL) or (? = typecode))'
CONCAT ' and ((cast(? as integer) is NULL)'
CONCAT ' or (? = 0)'
CONCAT ' or (? = 1 and deacode > 1)'
CONCAT ' or (? = 2 and deacode = 2)'
CONCAT ' or (? = 3 and deacode > 2))'
CONCAT ' AND ((cast(? as varchar(12)) IS NULL )'
CONCAT ' OR (A.ID = ? )'
CONCAT ' OR (A.NDCNBR = ? )'
CONCAT ' OR (A.NDCNBR = ? )'
CONCAT ' OR (A.NDCNBR = ? )'
CONCAT ' or (A.UPCNBR = ? )'
CONCAT ' or (a.upcinner = ?)'
CONCAT ' or (a.upcouter = ?))';
SET wDesc = UPPER ( V_DESCRIPTION CONCAT '%' ) ;
set wSQLcount = 'values (select count(*) ' concat wSQLFromAndWhere
concat ' ) into ?';
if v_PageNumber is NULL or v_RowsPerPage is NULL
or v_PageNumber <= 0 or v_RowsPerPage <= 0
or wTotalRows = 0 then
set wSQL = wSQLselect CONCAT wSQLFromAndWhere
CONCAT ' order by rowNbr asc ';
else
set wRowsRemaining = wTotalRows - ( (v_PageNumber - 1) * v_RowsPerPage);
if wRowsRemaining < v_RowsPerPage then
set wRowsPerPage = wRowsRemaining;
set wRowsToFetch = wTotalRows;
else
set wRowsPerPage = v_RowsPerPage;
set wRowsToFetch = v_PageNumber * v_RowsPerPage;
end if;
set wSQL = 'select * from ( select * from ( '
CONCAT wSQLselect CONCAT wSQLFromAndWhere
CONCAT ' fetch first ' CONCAT char(wRowsToFetch)
CONCAT ' rows only) as T1 order by rowNbr desc '
CONCAT ' fetch first ' CONCAT char(wRowsPerPage)
CONCAT ' rows only) as T2 order by rowNbr asc ';
end if;
Now both statements work as expected UNLESS the v_Description
parameter passed in is not NULL. In that case, the count statement
returns a count of zero but the select statement returns 1 or more
rows. As you can see, I use the same WHERE cluse for both statements,
but in one case it works and in the other it doesn't.
I've looked at this, ran it in debug (no errors after the cout
statement is executed), payed around with simialr statements....I
can't figure out what is going on.
In fact. if it helps, here's the values of the statements from the
debugger (prettied up of course):
"values (select count(*)
from Customer_Item_History A
where jdivsn = 0
and jcusnr = ?
and jordte between ? and ?
AND ( ( cast(? as varchar(30)) IS NULL )
OR ( A.DESCRIPTION LIKE ? ) )
and ((cast(? as integer) is NULL)
or (? = typecode))
and ((cast(? as integer) is NULL)
or (? = 0)
or (? = 1 and deacode > 1)
or (? = 2 and deacode = 2)
or (? = 3 and deacode > 2))
AND ((cast(? as varchar(12)) IS NULL )
OR (A.ID = ? )
OR (A.NDCNBR = ? )
OR (A.NDCNBR = ? )
OR (A.NDCNBR = ? )
or (A.UPCNBR = ? )
or (a.upcinner = ?)
or (a.upcouter = ?))
) into ?"
"select row_number() Over (order by jordte desc, description asc, ID
asc) as rowNbr,
cast(? as integer) as Total_Rows,
CVTOLSDATE(JORDTE) as Order_Date,
Description, size, ID, TypeName, deacode,
Qty_Ordered, qty_shipped, invoice_price,
cast(round(qty_shipped * Invoice_price,2) as decimal (11,2))as
Extended_Amount,
Invoice_Number, message
from Customer_Item_History A
where jdivsn = 0
and jcusnr = ?
and jordte between ? and ?
AND ( ( cast(? as varchar(30)) IS NULL )
OR ( A.DESCRIPTION LIKE ? ) )
and ((cast(? as integer) is NULL)
or (? = typecode))
and ((cast(? as integer) is NULL)
or (? = 0)
or (? = 1 and deacode > 1)
or (? = 2 and deacode = 2)
or (? = 3 and deacode > 2))
AND ((cast(? as varchar(12)) IS NULL )
OR (A.ID = ? ) OR (A.NDCNBR = ? )
OR (A.NDCNBR = ? ) OR (A.NDCNBR = ? )
or (A.UPCNBR = ? ) or (a.upcinner = ?)
or (a.upcouter = ?))
order by rowNbr asc"
Does anybody see anything I missed?
I'm thinking it has something to do with the VALUES INTO and the
LIKE....but I don't know what it could be...
Thanks,
Charles
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact
[javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.