firebird trigger +add value - sql

I'm using firebird 2.5. I want to create a trigger. I want it to add the new value on top of the existing value. I tried something but it didn't work. Can you help me?
If we think that old.MINIMUM = 5 and new.MINIMUM = 3, I want to save a total of 8 values.
so I want it to add the new value on top of the other value
create trigger T_STOK_BIU for STOK_HAREKET before insert or update
AS
BEGIN
if (NEW.giris_cikis= 2) then
BEGIN
update STOK set MINIMUM = + new.MINIMUM where ID = new.STOK_ID;
END
end
i tried this i got an error
create trigger T_STOK_BIU for STOK_HAREKET before insert or update
AS
BEGIN
if (NEW.giris_cikis= 2) then
BEGIN
merge into STOK
using (
select STOK_ID, sum(GIRIS) as toplm
from STOK_HAREKET
where STOK_ID = new.STOK_ID
group by STOK_ID
) as src
on STOK.ID = src.STOK_ID
when matched then update set STOK.MAKSIMUM = src.toplm + new.MAKSIMUM;
END
end
I tried this, it works, but it doesn't show the newly updated process, it comes from behind, so if the record is 8, I add 2, it shows 8, and when I add 2, it makes 12

Related

Avoid update trigger loop sql anywhere 12

Looks like in previous versions you could use ##nestlevel, but in ver 12 it always returns -1.
So how do you avoid update loops?
I'm trying to sync multiple connected contacts in the same table.
ALTER TRIGGER "Sync" AFTER UPDATE
ORDER 1 ON "Contact"
REFERENCING OLD AS oldRec NEW AS newRec
FOR EACH ROW
BEGIN
IF ##nestlevel = 1 THEN
UPDATE Contact set Title = newRec.Title
WHERE newRec.syncid = Contact.syncid;
END IF
END
This results

Query to update row value in next blank column

I have a table called StencilStorage and it has fields that look like this.
What I am trying to accomplish is creating a stored procedure that will update the table and set PCB_ID equal to some irrelevant number wherever the next available slot is, sorting the table from low->high by UNIQID. I have the following so far. The next available slot is labeled as 0 as shown in row 5 in the table above.
CREATE PROCEDURE sp_insert_to_storage
(
#PCB_ID integer
)
AS
BEGIN
UPDATE Tooling.StencilStorage
SET PCB_ID = #PCB_ID
WHERE
GO;
Any help is very much appreciated.
CREATE PROCEDURE sp_insert_to_storage
(
#PCB_ID integer
)
AS
BEGIN
UPDATE Tooling.StencilStorage
SET PCB_ID = #PCB_ID
WHERE UNIQID = (SELECT TOP 1 UNIQID FROM Tooling.StencilStorage WHERE PCB_ID = 0 ORDER BY UNIQID)
GO;

with sql how can i insert data but if it's already exists update data

I want to add it - if exists update else insert!
so that insert is working but i want to only insert new value and if thing already here i want to update it. my code here only inserts data no metter what
BEGIN
DECLARE das int;
SELECT dasaxeleba.id INTO das FROM dasaxeleba WHERE dasaxeleba.barcode = NEW.barcode;
INSERT INTO sawyobi(das_id,raodenoba,tvitgirebuleba,gasayidi1,gasayidi2)
VALUES(das,new.raodenoba,new.fasi,new.gasayidi1,new.gasayidi2);
END
if you are using oracle then you can use something like that:
merge into sawyobi tgt
using (select id
from dasaxeleba
where barcode = NEW.barcode) src on (tgt.das_id = src.id)
when matched then update set
raodenoba = new.raodenoba,
tvitgirebuleba = new.fasi,
gasayidi1 = new.gasayidi1,
gasayidi2 = new.gasayidi2
when not matched then insert
(das_id,
raodenoba,
tvitgirebuleba,
gasayidi1,
gasayidi2)
values (src.id,
new.raodenoba,
new.fasi,
ew.gasayidi1,
new.gasayidi2)
not quite sure where your "new"-values come from... but i hope you get the idea

SQL Trigger Using Variables

I need help creating this trigger, any help will be appreciate it, I can't save this script, it's saying 'Invalid column name OfferId'. But I checked all the columns names and they are correct
CREATE TRIGGER dbo.OrderOffer_UpdatedUnits
ON dbo.OrderOffer
FOR UPDATE
AS
BEGIN
DECLARE #OfferId char(5) SET #OfferId = (Select OfferId From INSERTED i, OrderOffer a Where i.OrderOfferid = a.OrderOfferid);
DECLARE #UnitsAvailable int SET #UnitsAvailable = (Select SUM(UnitsAvailable) From dbo.Offer Where OfferId=#OfferId);
UPDATE dbo.OrderOffer SET dbo.UnitsAvailable = #UnitsAvailable
FROM INSERTED i, OrderOffer a
WHERE i.OrderOfferid = a.OrderOfferid
END
Try this:
CREATE TRIGGER dbo.OrderOffer_UpdatedUnits
ON dbo.OrderOffer
FOR UPDATE
AS
BEGIN
UPDATE a
SET dbo.UnitsAvailable =
(
Select SUM(b.UnitsAvailable)
From dbo.Offer b
Where b.OfferId = a.OfferId
)
FROM OrderOffer a
INNER JOIN INSERTED i
ON i.OrderOfferid = a.OrderOfferid
END
NB: I removed the variables since that would assume you only updated one row at a time. Even if the update runs over multiple rows, the above should still work.

How to insert a value in a trigger

Im trying to insert a value when the condition occurs but it give me the error ambiguos column name
the code is:
create trigger B
on ALUNOS
after update, insert
as
begin
update ALUNOS
set média_final = 9 from inserted where média_final < 9
end
create trigger B
on ALUNOS
after update, insert
as
begin
update ALUNOS
set média_final = 9
from inserted i where i.média_final < 9
end
The ambiguous column is média_final which is in both tables. Specifying which table it came from is what you need to do.
try to follow this
update table set column = value where column = value
change value for the value that you want, you can add null or change null, don`t ever forget '' for string and lose it for numbers