select DISTINCT TOP 10 o.MaterialNumber from MaterialCreation_MDO o where o.MaterialGroup= ?1 and o.Noun= ?2 and o.MaterialNumber like ?3 order by o.MaterialNumber desc
this query is throwing exception on encountering 'TOP' keyword in ejb-ql statement....
TOP is unrecognized in EJB-QL, use LIMIT instead.
You can try the below modified query :
select DISTINCT o.MaterialNumber from MaterialCreation_MDO o where o.MaterialGroup= ?1 and o.Noun= ?2 and o.MaterialNumber like ?3 order by o.MaterialNumber desc LIMIT 10;
or alternatively you can give explicitly the number of results to be fetched by :
entityManager.createNativeQuery(query,YourClass.class).setParameter("name",value).setMaxResults(10).getResultList();
NamedQuery will also work fine.
Related
I want to apply this query on the database. There is problem in limit function. Please help me to solve this probem.
This is the database. https://drive.google.com/file/d/1-mwsQHaJHbVzIDXVsj9jw35ZO8TyV6qM/view?usp=drivesdk
SELECT Track.Track, Artist.Artist, Album.Album, Genre.Genre FROM Track JOIN Genre JOIN Album JOIN Artist ON Track.genre_id = Genre.ID and Track.album_id = Album.id AND Album.artist_id = Artist.id ORDER BY Artist.Artist, Track.Track LIMIT 3
Error
android.database.sqlite.SQLiteException: near "limit": syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT Track.Track, Artist.Artist, Album.Album, Genre.Genre FROM Track JOIN Genre JOIN Album JOIN Artist ON Track.genre_id = Genre.ID and Track.album_id = Album.id AND Album.artist_id = Artist.id ORDER BY Artist.Artist, Track.Track LIMIT 3 limit 20 offset 0
Read the error carefully: the end of the message says the following
... ORDER BY Artist.Artist, Track.Track LIMIT 3 limit 20 offset 0
There is a second "limit" clause appended to the query and this is why sqlite can't process it.
I believe your code appends "limit 20 offset 0" to the query automatically. Check the way how you create the query. The error is definitely there.
i making get topX% query
SELECT CAST(ROUNT(COUNT(*) * 0.1)AS INTEGER) FROM person;
But syntax error ,,
This is the kind of query I want to finally make.
SELECT*FROM person
ORDER BY vote DESC LIMIT (SELECT CAST(ROUNT(COUNT(*) * 0.1)AS INTEGER)
FROM person);
how to fix it ?
I am not sure why my 2nd DBMS_OUTPUT.OUT_LINE doesn't print out my list? I recieve anonymous block completed and the headings (RANK: BABY NAMES: ) print out however my requested list does not. Here is my code:
SET SERVEROUTPUT ON
SET VERIFY OFF;
DECLARE
V_SEARCH VARCHAR2(20):= '&SV_SEARCH';
V_ROWS NUMBER(11) := '&SV_ROWS';
CURSOR C_NAME IS
SELECT RANK() OVER (ORDER BY CODE DESC)
FROM (SELECT * FROM "NAME_LIST" WHERE "NAMES" LIKE (V_SEARCH)
ORDER BY CODE DESC)WHERE ROWNUM = V_ROWS;
R_NAME C_NAME%ROWTYPE;
BEGIN
OPEN C_NAME;
DBMS_OUTPUT.PUT_LINE('RANK: BABY NAMES:');
LOOP
FETCH C_NAME INTO R_NAME;
EXIT WHEN C_NAME%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(' '||V_ROWS||' '||V_SEARCH);
END LOOP;
CLOSE C_NAME;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error');
END;
As a result I receive this as my output:
anonymous block completed
RANK: BABY NAMES:
I would run your cursor query by itself to verify that you are getting data. My guess is your ROWNUM at the end is causing you to pull no data. Try this cursor query instead:
CURSOR C_NAME IS
SELECT RANK() OVER (ORDER BY CODE DESC)
FROM (SELECT ROWNUM AS RN, NL.* FROM "NAME_LIST" NL WHERE "NAMES" LIKE (V_SEARCH)
ORDER BY CODE DESC)WHERE RN = V_ROWS;
ROWNUM=1 or it's inferior to something.
In SQL this command works ok:
Query
SELECT TOP 20 * FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY t0.ProductId) AS [ROW_NUMBER], *
FROM Product AS [t0]
) AS [t1]
WHERE [t1].[ROW_NUMBER] > 0 * 20;
Now I try the same with SQLite. I know that I must replace TOP with LIMIT, but don't know where to put it.
I always get something like
Error
SQLite error near "(": syntax error" or "SQLite error near "*": syntax error".
And I am not sure that the command [ROW_NUMBER] or ROW_NUMBER() works in SQlite.
Query
SELECT *,
(
SELECT COUNT(*)
FROM Product b
WHERE a.ProductId >= b.ProductId
) AS rnum
FROM Product a LIMIT 20;
Screen Shot
See the documentation:
SELECT *
FROM Product
LIMIT 20
OFFSET 0 -- optional
SQlite doesn't support TOP. That is sql-server syntax. You have to use limit 20 instead.
How to solve this error "Error converting data type varchar to numeric".I have tried
SET ANSI_WARNINGS off;
but only warning message is gone but error is still there.
My code is
SELECT tblLogin.FullName,
SUM(CAST(tblDetails.Productivity_Hours AS decimal(9,2))) AS [hours]
FROM tblTaskDetails
INNER JOIN tbltsheet ON tblTaskSheet.TaskId=tblDetails.TaskId
INNER JOIN tblLogin ON tblLogin.UserId=tblTaskSheet.UserId
INNER JOIN tblTCategory ON tblDetails.CatId=tblTCategory.CatId
WHERE dbo.tbltsheet.TaskDate>= '12/28/2013'
AND tbltsheet.TaskDate<='12/31/2013'
AND tblTCategory.CatName LIKE 'abc'
GROUP BY tblLogin.FullName
ORDER BY [hours] DESC
Try to find these values which can't be converted to a numeric:
select Productivity_Hours from tblDetails where ISNUMERIC(Productivity_Hours)=0
Also you can use CASE statement to avoid these wrong values (accept them as 0 value):
SELECT tblLogin.FullName,
SUM(CASE WHEN ISNUMERIC(tblDetails.Productivity_Hours)=1
THEN CAST(tblDetails.Productivity_Hours AS decimal(9,2))
ELSE 0
END
) AS [hours]
FROM ....
Use IsNumeric to validate your type.
WHERE dbo.tbltsheet.TaskDate>= '12/28/2013'
AND tbltsheet.TaskDate<='12/31/2013'
AND tblTCategory.CatName LIKE 'abc'
AND ISNUMERIC(tblDetails.Productivity_Hours) =1
DEMO