I have a table MRU, that has 3 columns.
(VALUE varchar(255); TYPE varchar(20); DT_ADD datetime)
This is a table simply storing an entry and recording the date time it was recorded. What I wanted to do is: delete the oldest entry whenever I add a new entry that exceeds a certain number.
Here is my query:
delete from MRU
where type = 'FILENAME'
ORDER BY DT_ADD limit 1;
The error message is:
SQL Error: near "ORDER": syntax error ...
The query returns an error.
First of all, it always helps posting as much information as you have. In this particular instance, "an error" is unhelpful and it would've taken you perhaps 2 seconds to copy and paste the actual error message given, which would give us valuable clues when helping you.
Instead, I went to the documentation for the DELETE statement of SQLite, found here, and notice that lo and behold, DELETE does not have ORDER BY, unless it is compiled in a specific manner. I assume your version isn't, although without the error message it is hard to tell.
What you can try instead is this:
delete from MRU where DT_ADD = (
SELECT MIN(DT_ADD) FROM MRU WHERE type = 'FILENAME'
)
I'm not saying that you should do so, since it's completely non-portable, but if there's a compelling need, this will work:
In SQLite the rowid column always exists unless an integer primary key is defined elsewhere. This can be used in something like:
delete from MRU where rowid = (
select rowid from MRU order by DT_ADD limit 1
)
Related
I'm trying to delete some information from a table that has a date < 16/16/2019 in C#. The query gave me an exception so I'm trying to do it directly in MS Access and even here it gave me error.
SELECT *
FROM TableName
WHERE (((TableName.Date)<= #16/06/2019#));
If I use the above, the query gives me the result that I expect which is all information which is stored before a specific date.
But if I use the DELETE statement:
DELETE
FROM TableName
WHERE (((TableName.Date)<= #16/06/2019#));
It gives me the error:
The search key was not found in any record
Why?
Given the information in the comments, I would suggest the following:
delete from TableName t where t.data <= #2019-06-16#
I have a product entry page through we keep on adding product entries in our database.
Product location wise there are 2 series. e.g., ABCTMP(Series(1-max)) and XYZ(Series(1-max)).
Table is having primary key constraint which is a combination of 4 columns. Out of 4, only one is giving an issue while increment series combination wise.
That first column is location wise product code as stated above and it is of data type char(20) as it stores values like ABCTMP01 and through classic asp code. We increment that last 01 value by addition of one into existing value.
Right now, facing issue when last value reaches 99 and turns to 100. It generates code 100 through code but unable to insert in database and giving this error that was due to existing entry in database.
Duplicate key part is same one which I mentioned above in subject/header. If I delete record from table of record no. 100 to check, it gives me proper record of 99 through above query and through above classic asp code, it generates next code as 99+1 = 100.
But when I again try to add next series record for 101, even through SQL mgt studio, it gives me below error.
Violation of PRIMARY KEY constraint 'PK'. Cannot insert duplicate key in object 'prdct_mst_tab'. The duplicate key value is (PWATERTMP100 , 006, Y, 01). The statement has been terminated.
Have tried by dropping constraint and changing size of data type char(20) to char(30) as there are dependencies on table. But not worked. Then, have tried by changing data type from char(30) to varchar(30), still not worked. Then again tried by manually
executing insert command
in SQL itself, but same error occurred for 101th record.
Before generating next series, there is select statement to check latest inserted record which will get incremented later.
For generating next record of 101,there select statement must show last inserted record of 100, but it's still giving 99th record and code is generating as 100 again and the error continues the same. I do not understand why it's not taking 100th record when I execute SELECt statement in SQL mgt studio. DataType of that PWATERTMP100 column is char(20).
Below my classic asp code for series generation and SQL 'SELECT top 1 *' statement for record count for location wise product.
select top 1 *
from prdct_mst_tab
where pmt_prdct_cd like 'PWATER%'
and pmt_umt_unit_cd='006'
AND PMT_CMT_CMPNY_CD='01'
order by pmt_prdct_cd desc
Classic ASP Code: -
If recordset.eof Then
getcode="ABCTMP01"
Else
getcode = clng(Mid(recordset("Column1"),10,20))
response.write("Hello" & getcode)
getcode = getcode +1
response.write("<br />Hello" & getcode)
getcode = "ABCTMP" & getcode
response.write("<br />Hello" & getcode)
End if
Below for adding generated product code in database table.
Sql is as below
select * from Table1
recordset.open sql,con,3,2
recordset.addnew
recordset("Column1")=getcode
recordset.update
recordset.close
Note : Values given above are sample one.
I want the record gets inserted even when it turns from 99 to 100, means code will become ABCTMP99 - ABCTMP100 and continue from series starting with 100 range(3 digits)like 100, 101, 102....
The problem is that the order by in this:
select top 1 * from prdct_mst_tab
where pmt_prdct_cd like 'PWATER%'
and pmt_umt_unit_cd='006'
AND PMT_CMT_CMPNY_CD='01'
order by pmt_prdct_cd desc
Does not do what you expect.
Try running this in management studio:
select * from prdct_mst_tab
where pmt_prdct_cd like 'PWATER%'
and pmt_umt_unit_cd='006'
AND PMT_CMT_CMPNY_CD='01'
order by pmt_prdct_cd desc
You'll see that the 100 appears before 99 because it is ordering it alphanumerically not numerically.
In fact you will also see that 10 appears before 9 - how did you ever get past this?
You have a fundamental design flaw. I will add to that by posing a solution which cements the design flaw in place and introduces new bugs. But it will give you a result.
One workaround is to do something this:
select
MAX(
CASE
WHEN ISNUMERIC(RIGHT(RTRIM(pmt_prdct_cd),3)) = 1
THEN RIGHT(RTRIM(pmt_prdct_cd),3)
ELSE '0' + RIGHT(RTRIM(pmt_prdct_cd),2)
END
) As LargestNumber
from prdct_mst_tab
where pmt_prdct_cd like 'PWATER%'
and pmt_umt_unit_cd='006'
AND PMT_CMT_CMPNY_CD='01'
What does this do?
It checks if the last three characters are a number. If it is it uses it.
If it isn't a number it grabs the last two characters and puts a zero in front.
Then it picks the largest number out of all of those.
note - this returns a number, it doesn't return the full product code. So you'll need to remove the ASP Mid code that tries to pull the number out.
This might work until you find some other data or case that you haven't mentioned yet. Like for example if there are trailing characters that aren't numeric. Or for when you need a four character number
Make no mistake - you have a fundamental design flaw and this just prolongs the issue, adds complexity, and introduces more bugs down the track
Some basic observations:
char is a bad data type for this
It has concurrency issues - if two requests call this at the same time (easily done from a web app), it returns the same number and they both try and insert a duplicate value
You should not be assigning and storing incrementing numbers like this. Just use an IDENTITY in the database.
I guess since you are using classic ASP, you are not in a situation that you can redesign this.
You need to decide whether you are going to patch this with something that will introduce new bugs or fix it properly.
Does each product code really need to be incremented within it's own domain like that? Is there any issue with having ABC01 then DEF02 then XYZ03?
Working with embedded database derby version 10.12.1.1.
I have created a sequence successfully as below
CREATE SEQUENCE BUCKET_SEQ AS BIGINT START WITH 1000;
But when trying to get next value using
SELECT NEXT VALUE FOR BUCKET_SEQ
below error encountered:
Syntax error: Encountered "<EOF>" at line 1, column 40.
Please suggest any pointers.
You have to SELECT from something, and the something has to be some sort of a table.
The simplest thing to do is to use the SQL VALUES keyword, which makes an (unnamed, temporary) table for you.
You then give the table a name, and the table's column a name, and select the value from that:
select t from ( values next value for bucket_seq ) s( t);
T
--------------------
1000
There are other syntax forms possible, but this is a simple one that you can use.
I have question regarding SQLite's changes() function, which, according to the documentation, "returns the number of database rows that were changed or inserted or deleted by the most recently completed INSERT, DELETE, or UPDATE statement" (also see the documentation of the underlying C/C++ function).
I was hoping to use this function to check whether the execution of an UPDATE statement pertaining to a single row has really caused that row to be changed or not.
By changed I do not just mean that the row matched the statement's WHERE clause. No, instead what I mean is that, for the row in question, the value of at least 1 column is actually different after the execution compared to before. If you ask me this is the only proper definition of a change in this context.
So I was hoping to detect such changes by checking whether changes() returns 1 (row changed) or 0 (row unchanged) when called right after the execution of the UPDATE statement.
But much to my despair this does not seem to work as expected.
Allow me to illustrate:
CREATE TABLE People (Id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT NOT NULL);
INSERT INTO People (Name) VALUES ("Astrid");
SELECT changes();
Here changes() returns 1, as expected because we just INSERTed 1 row.
UPDATE People SET Name = "Emma" WHERE Id = 1;
SELECT changes();
Here changes() returns 1, as expected because 1 row was UPDATEd (i.e. actually changed: the Name of the Person with Id = 1 was "Astrid" but is now "Emma").
UPDATE People SET Name = "John" WHERE Id = 200;
SELECT changes();
Here changes() returns 0, as expected because there is no row with Id = 200.
So far so good. But now have a look at the following UPDATE statement, which does indeed match an existing row, but does not actually change it at all (Name remains set to "Emma")...
UPDATE People SET Name = "Emma" WHERE Id = 1;
SELECT changes();
Here changes() returns 1, while I was of course hoping for 0 :-(.
Perhaps this would have made sense if the function was called something like matched_rows() or affected_rows(). But for a function called changes(), and documented as it is, this behaviour strikes me as illogical, or confusing at best.
So anyway, can somebody explain why this happens, or, even better, suggest an alternative strategy to achieve my goal in a reliable (and efficient) way?
All I can think of is to actually do something like SELECT * FROM People WHERE Id = x, compare all returned column values with the values I'm about to set in the UPDATE statement and thereby decide whether I need to execute the UPDATE at all. But that can't be very efficient, right?
Of course in this toy example it might not matter much, but in my actual application I'm dealing with tables with many more columns, some of which are (potentially big) BLOBs.
The database does not compare old and new values; any UPDATEd row always counts as "changed" even if the values happen to be the same.
The documentation says that
the UPDATE affects … those rows for which the result of evaluating the WHERE clause expression as a boolean expression is true.
If you want to check the old value, you have to do it explicitly:
UPDATE People SET Name = 'Emma' WHERE Id = 1 AND Name IS NOT 'Emma';
I would like to return the ids which have been deleted by a DELETE query.
On Stackoverlow, I found this:
How to get ID of the last updated row in MySQL?
The top1 answer has a very nice solution, but it's for mysql. I tried to do the same in Firebird after reading some part of the Firebird manual:
set term ^ ;
EXECUTE BLOCK
AS
DECLARE VARIABLE uids BLOB SUB_TYPE TEXT;
begin
DELETE FROM CATEGORY WHERE name = 'Haló'
AND ( SELECT id INTO :uids );
SELECT #uids;
end
^
Yes, I know that 'uids' will always contain one ID, since I'm overwriting the variable, but it's only a test, and what is more, it doesn't work. It stops at 'INTO' saying "Token unknown - line 8, column 21". I don't know what to do, what to continue with.. :\
Thanks for Your help!
For this, please run separate queries
First fetch the record ids those you want to delete by the same where condition for DELETE
like, SELECT ID FROM CATEGORY WHERE name = 'Haló'
Then delete the records
you could try the "RETURNING" clause, like this:
delete from Scholars
where firstname = 'Henry' and lastname = 'Higgins'
returning lastname, fullname, id
look here for more details