SQLite CASE/WHEN statement - sqlite

this is my CASE/WHEN statement. But as you may see I get this error. I don't know why.
All I want to do is detect when something in field MAJKA is changed. So if some other fields of column MAJKA are empty don't touch them, but change the value to new value of those fields of column MAJKA which are NOT empty.
SQL Error: near "UPDATE": syntax error <CREATE TRIGGER [test]
AFTER UPDATE OF [MAJKA]
ON [unos_golub]
FOR EACH ROW
BEGIN
SELECT majka,
CASE WHEN majka=''
THEN
(UPDATE unos_golub SET broj_goluba=NEW.majka WHERE broj_goluba=OLD.majka)
ELSE
(UPDATE unos_golub SET broj_goluba=NEW.majka WHERE broj_goluba=OLD.majka
UPDATE unos_golub SET majka=NEW.majka WHERE majka=OLD.majka)
END
FROM unos_golub;

SQLite has no general mechanism to execute commands conditionally.
However, in triggers, you can use a WHEN condition on the entire trigger:
CREATE TRIGGER test_for_broj_goluba
AFTER UPDATE OF majka ON unos_golub
BEGIN
UPDATE unos_golub SET broj_goluba=NEW.majka WHERE broj_goluba=OLD.majka;
END;
CREATE TRIGGER test_for_majka
AFTER UPDATE OF majka ON unos_golub
WHEN NEW.majka <> ''
BEGIN
UPDATE unos_golub SET majka=NEW.majka WHERE majka=OLD.majka;
END;
(The first UPDATE is the same in both cases and thus does not need a WHEN.)

You're putting data manipulation (DML) statements where you need an expression that returns a value. Consider:
SELECT majka,
CASE WHEN majka=''
THEN
'it is empty'
ELSE
'it is not empty'
END
This will give you a column where the value is either 'it is empty' or 'it is not empty', depending on the value in the previous column.
You can't put a DML statement in that spot in the SQL -- the DML statement is not an expression with a value.

Related

skip deleting records when a child record is found

i have a pl/sql procedure to modify/delete records based on a checkbox selection in my Apex application:
delete from s_objectif_operation where id_operation=:p124_id_operation;
for a in (select distinct id from s_objectif
where id in (
SELECT trim(regexp_substr(:P124_S_OBJECTIF, '[^:]+', 1, LEVEL)) str
FROM dual
CONNECT BY instr(:P124_S_OBJECTIF, ':', 1, LEVEL - 1) > 0
))
loop
insert into s_objectif_operation
(id_s_objectif, id_operation)
values
(a.id, :p124_id_operation);
end loop;
for every modification, this procedure deletes all the records and insert the correct ones back so i had to remove the "cascade on delete" option the foreign key constraint to suspend any child record removal but now the procedure is not working.
maybe "raise an exception" can work?
There is no need to delete all the records and re-inserting only the checked ones. That is a brute force approach and it works but it does not capture the real user action.
As an alternative you can just delete/insert the changes. To do that, create an additional page item P124_S_OBJECTIF_OLD and set it to the value P124_S_OBJECTIF with a computation after header (or any pre-rendering processing point after P124_S_OBJECTIF has been initialized). In your pl/sql code use APEX_STRING.SPLIT to process the checkbox values and the MULTISET operator to decide which values have been touched.
Then your pl/sql process code could look like this.
DECLARE
l_objectif_old apex_t_varchar2;
l_objectif_new apex_t_varchar2;
l_objectifs_added apex_t_varchar2;
l_objectifs_removed apex_t_varchar2;
BEGIN
l_objectif_old := apex_string.split(:P124_S_OBJECTIF_OLD,':');
l_objectif_new := apex_string.split(:P124_S_OBJECTIF,':');
l_objectifs_added := l_objectif_new MULTISET EXCEPT l_objectif_old;
l_objectifs_removed := l_objectif_old MULTISET EXCEPT l_objectif_new;
-- add new
FOR i IN 1 .. l_objectifs_added.COUNT LOOP
INSERT INTO s_objectif_operation (id_s_objectif, id_operation)
VALUES (l_objectifs_added(i), :P124_ID_OPERATION);
END LOOP;
-- delete old
FOR i IN 1 .. l_objectifs_removed.COUNT LOOP
BEGIN
DELETE FROM s_objectif_operation
WHERE id = l_objectifs_removed(i);
EXCEPTION WHEN OTHERS THEN
-- this will fire if there are child records. Add your own code.
NULL;
END;
END LOOP;
END;
Note that you might have to tweak the insert and delete statement to match your data structure.

trigger with insert inside a select

I'm trying to create a trigger in which, only under certain circumstances, an insert is performed on another table. Consider the following code:
create table journal (
pk integer primary key autoincrement,
dsc varchar(10) not null
);
create table users (
name varchar(30) primary key not null
);
create trigger users_ai
after insert on users
begin
select
case
when 1 then
insert into journal(dsc) values('foo')
end;
end;
I get the following error when I run this code:
Error: near line 10: near "insert": syntax error
In production, the "1" in the when clause would be replaced by a more complex expression. I've also tried "true" and get the same results. I've also tried surrounding the insert statement in parens and get the same results. Any ideas how to accomplish what I want?
If you look at the syntax diagram for "CREATE TRIGGER", you'll see your attempt just doesn't match. You can, however, simply use the WHEN branch (without needing FOR EACH ROW):
create trigger users_ai
after insert on users
when 1 begin
insert into journal(dsc) values('foo');
end;
OK, figured it out. Instead of putting a conditional expression in the block of the trigger, I used a when clause. Here's the code that works:
create trigger users_ai
after insert on users when 1
begin
insert into journal(dsc) values('foo');
end;
If that when expression is changed to something that returns false (say 0) then the insert isn't done. In production, the expression will sometimes return true, sometimes false, which, of course, is the point of this code. Thanks everybody!
I think that you want a CASE statement, not a CASE expression.
create trigger users_ai after insert on users
begin
case
when ... then insert into journal(dsc) values('foo');
when ... then ...;
else ...;
end case;
end;
Note: if your trigger needs access to the data that was just inserted, its definition should the for each row option.
You can try to use an INSERT ... SELECT and your expression in the WHERE clause.
...
INSERT INTO journal
(dsc)
SELECT 'foo'
WHERE 1 = 1;
...
1 = 1 needs to be replaced by your Boolean expression.

ORA-00969: missing ON keyword 00969. 00000 - "missing ON keyword"

I am getting 0099 error while creating below trigger:
create trigger AuditTrigger1
before update, insert
on MPUZNTAB
for each row
declare
begin
insert into AuditTable1
(ZNCODE, DES , SHDES, WhenChanged)
values
(:new.ZNCODE,:new.DES,:new.SHDES, getdate())
end;
Please recomment changes
You missed a semicolon at the end of the insert statement
You should write before update OR insert
If getdate() is not a user defined function, you should use SYSDATE instead, to get the current time
I'm not sure the :new values are correct. Are they fields of the table MPUZNTAB?
This code should work:
Create Or Replace Trigger AuditTrigger1
Before Update Or Insert On MPUZNTAB
For Each Row
Declare
Begin
Insert Into AuditTable1
(ZNCODE,
DES,
SHDES,
WhenChanged)
Values
(:new.ZNCODE,
:new.DES,
:new.SHDES,
SYSDATE);
End;

<SQLITE> compare old value and new value

I have a trigger.
I want to do insert changed values after every update.
But I get syntax error on line IF .. THEN, and I can't find example.
IF (IFNULL(NEW.symbol,'') <> IFNULL(OLD.symbol,'')) THEN
INSERT INTO LOG(old_value, new_value, DATA, user)
VALUES ('Symbol: '|| IFNULL(OLD.symbol,''), 'Symbol: ' (IFNULL(NEW.symbol,''), CURRENT_TIMESTAMP, id_user)
Can You help me?
SQLite has no IF statement.
You must put the condition into the WHEN clause of the trigger.

Modify a column to NULL - Oracle

I have a table named CUSTOMER, with few columns. One of them is Customer_ID.
Initially Customer_ID column WILL NOT accept NULL values.
I've made some changes from code level, so that Customer_ID column will accept NULL values by default.
Now my requirement is that, I need to again make this column to accept NULL values.
For this I've added executing the below query:
ALTER TABLE Customer MODIFY Customer_ID nvarchar2(20) NULL
I'm getting the following error:
ORA-01451 error, the column already allows null entries so
therefore cannot be modified
This is because already I've made the Customer_ID column to accept NULL values.
Is there a way to check if the column will accept NULL values before executing the above query...??
You can use the column NULLABLE in USER_TAB_COLUMNS. This tells you whether the column allows nulls using a binary Y/N flag.
If you wanted to put this in a script you could do something like:
declare
l_null user_tab_columns.nullable%type;
begin
select nullable into l_null
from user_tab_columns
where table_name = 'CUSTOMER'
and column_name = 'CUSTOMER_ID';
if l_null = 'N' then
execute immediate 'ALTER TABLE Customer
MODIFY (Customer_ID nvarchar2(20) NULL)';
end if;
end;
It's best not to use dynamic SQL in order to alter tables. Do it manually and be sure to double check everything first.
Or you can just ignore the error:
declare
already_null exception;
pragma exception_init (already_null , -01451);
begin
execute immediate 'alter table <TABLE> modify(<COLUMN> null)';
exception when already_null then null;
end;
/
You might encounter this error when you have previously provided a DEFAULT ON NULL value for the NOT NULL column.
If this is the case, to make the column nullable, you must also reset its default value to NULL when you modify its nullability constraint.
eg:
DEFINE table_name = your_table_name_here
DEFINE column_name = your_column_name_here;
ALTER TABLE &table_name
MODIFY (
&column_name
DEFAULT NULL
NULL
);
I did something like this, it worked fine.
Try to execute query, if any error occurs, catch SQLException.
try {
stmt.execute("ALTER TABLE Customer MODIFY Customer_ID nvarchar2(20) NULL");
} catch (SQLException sqe) {
Logger("Column to be modified to NULL is already NULL : " + sqe);
}
Is this correct way of doing?
To modify the constraints of an existing table
for example... add not null constraint to a column.
Then follow the given steps:
1) Select the table in which you want to modify changes.
2) Click on Actions.. ---> select column ----> add.
3) Now give the column name, datatype, size, etc. and click ok.
4) You will see that the column is added to the table.
5) Now click on Edit button lying on the left side of Actions button.
6) Then you will get various table modifying options.
7) Select the column from the list.
8) Select the particular column in which you want to give not null.
9) Select Cannot be null from column properties.
10) That's it.

Resources