I just started learning and reading about TRIGGERs in Derby which I use as Database for a project in Java. I used to have MSSQL as database in the past.
There's just one problem I have with triggers. I can't find a syntax and I don't know how to specify the ID of which row to update during an UPDATE trigger.
This is what I have.
CREATE TRIGGER COPY_UPDATED_USERNAME
AFTER UPDATE ON ALLUSERS
REFERENCING NEW AS NEWUSERNAME_ROW
FOR EACH ROW MODE DB2SQL
UPDATE ALLUSERS_MAINTABSPERMISSION SET USERNAME = NEWUSERNAME_ROW.USERNAME
It's able to update the USERNAME column but it affects/updates all rows instead of just one row. That's where I'm having problems with.
In MSSQL I can simply put SET #ID = (SELECT ID FROM INSERTED) but I don't know how to do it in Derby.
How to do this on Derby?
Thanks.
I was able to solve my problem. Just had to add the WHERE clause and put the Id.
I hope this helps new derby users.
CREATE TRIGGER COPY_UPDATED_USERNAME
AFTER UPDATE OF USERNAME ON ALLUSERS
REFERENCING NEW AS NEWUSERNAME_ROW
FOR EACH ROW MODE DB2SQL
UPDATE ALLUSERS_MAINTABSPERMISSION SET USERNAME = NEWUSERNAME_ROW.USERNAME
WHERE USERID = NEWUSERNAME_ROW.USERID;
Related
Hi I have a task of assigning nick_name to new users, I added a new column and when a new user is getting created, a nick_name is assigned to it, but what should I do for already existing users (default is not an option here). Every time I do this manually using update statement, Is there a better way to do it?
Thanks
The basic sql-command is
update my_table set nick = username where nick is null;
So you can adjust it to fit the nickname to your needs.
I have SQLite database and mostly the same scheme in ":memory:" database. I create first, then attach second (memory). And I need to update table in on-disk database with values from table located in-memory database. I created trigger like:
create temporary trigger trg after update of _flushMem on mem.tbl "
begin
update tbl set
version = old.version,
...;
update tbl set
...;
end;
(there are 2 steps of update, so I have 2 "update" statements. OK. I have special field _flushMem which I use to run trigger with SQL statement like: update mem.tbl set _flushMem=1. As I understand, SQLite supports triggers between 2 databases with some limitations (but I update current database, not attached). Test shows that trigger does not run. Never. How to write and to launch such trigger?
I have a table with auto increment column (ID) and have already filled my table with records. Then, after sometime I noticed that the auto increment (ID) column started from 2 instead of 1. I really wanted the count to start from 1. So, what I want to do is decrease the ID column by one for all the records using SQL statement UPDATE SET. I have used this SQL Statement on MySQL database and it worked. However, on LibreOffice base, it won't even allow me to execute Update statement saying that it is NOT a query. So, following is what I want to do.
UPDATE Accounts SET ID=ID-1;
Apparently, LibreOffice base doesn't like that sql statement. So, how can I do this?
It sounds like you tried to create a query, but that is not how to run an update command. Instead, go to Tools -> SQL and enter the following:
UPDATE "Accounts" SET ID=ID-1;
This was tested using the default HSQLDB engine.
In my asp.net mvc application , I use Sql Data Adapter to update a record.
For example
UPDATE sample SET status = 1 WHERE id = #id
I need to test a scenerio where this sql code cannot be run and not any records are updated. How can i make this?
Should i make a lock for sample table and how can i do that ?
How can i make this UPDATE query somehow not works and not updates any record ?
Forgot to note that: I cannot change this code or application code and cannot give random #id parameter.
I need to do that in database level.
You can make the specific table in database read only by using one of the below techniques.
Insert, Update, Delete Trigger
Check Constraint and Delete Trigger
Make the Database Read Only
Put the Table in a Read Only File Group
DENY Object Level Permission
Create a View
You could set id to a value that does not exist for sure (e.g. -1).
Or you could remove the user rights of the user accessing the database (in the connection string) for updating this table. Hoping it's not sa :)
Add an [Authorize] attribute to the controller in question and attempt to perform the operation as a guest user.
Edit. To do it server side, enter the following to a query window on the database
Begin Transaction
That should lock up the database while you do your tests
I'm trying to create a rough emulation of Oracle's ORA_ROWSCN in my Derby database. I'm using Derby for unit testing so that a tester doesn't have to install Oracle Express or something like that. So I have a statement that looks like this:
CREATE TRIGGER
my_table_bi NO CASCADE BEFORE
INSERT ON my_table
REFERENCING NEW AS NEW
FOR EACH STATEMENT MODE DB2SQL
NEW.ORA_ROWSCN = 1
I eventually want to make this more complex, but I'd like to start with making it even possible. I get an error saying
Syntax error: Encountered "NEW" at line 6, column 5
What am I missing? Is this even possible?
I use this to simulate a before insert, may be it will help you if it isn't too late.
DROP TRIGGER TRI_UPPER_FIRSTNAME_PERSONNES;
CREATE TRIGGER TRI_UPPER_FIRSTNAME_PERSONNES
AFTER INSERT ON PERSONNES
REFERENCING NEW AS NEW
FOR EACH ROW
UPDATE PERSONNES SET FIRSTNAME_PER = UPPER(NEW.FIRSTNAME_PER) WHERE NEW.SID_PER = SID_PER;