I have to update a column in table_b when a row inserted to table_a.
Query
CREATE TRIGGER trg_Update
ON table_a FOR INSERT
AS
UPDATE table_b
SET Sold_Qty=inserted.qty
WHERE OrdNo=inserted.OrdNo;
I am getting the following error.
Msg 4104, Level 16, State 1, Procedure trg_Update, Line 6
The multi-part identifier "inserted.OrdNo" could not be bound.
Try this in place of the UPDATE query:
UPDATE table_b
SET Sold_Qty=inserted.qty
FROM table_b b
INNER JOIN inserted ON b.OrdNo=inserted.OrdNo;
Related
I'm trying to insert values from table 2 (in the pacp2 column) into table 1 (in the pacp column) where the facility_id from both tables matches and where table1 pacp column is null. I get the following error:
Msg 4104, Level 16, State 1, Line 5 The multi-part identifier
"table1.FACILITYID" could not be bound. Msg 4104, Level 16, State 1,
Line 5 The multi-part identifier "table1.pacp" could not be bound.
Does anyone know why this isn't working? I've tried using alias for both tables but that won't work.
INSERT INTO database.dbo.table1 (pacp)
SELECT pacp2
FROM table2
WHERE database.dbo.table1.facility_id = database.dbo.table2.facility_id
AND database.dbo.table1.pacp IS NULL
The description of your question suggests that you are looking for an update rather than an insert.
Here is one option using an updatable cte:
with cte as (
select t1.pacp, t2.pacp2
from table1 t1
inner join table2 t2 on t1.facility_id = t2.facility_id
where t1.pacp is null
)
update cte set pacp = pacp2
For rows of table1 where pacp is null, the query attempts to retrieve the value of pacp2 from table2, using facility_id for matching. If it succeeds, then it updates the corresponding row in table1 and sets pacp to the value of pacp2.
i am trying to create a column that measures the UPDATE_DATE of a row.
I am building a trigger but i have an ambiguity problem regarding the key.
CREATE TRIGGER ModDate_location_update
ON dim_Location
AFTER UPDATE
AS
BEGIN
UPDATE dbo.dim_Location
SET UPDATE_DATE = GETDATE()
--WHERE INSERT_DATE IN (SELECT INSERT_DATE FROM INSERTED)
FROM dim_Location X
JOIN dim_Location Y ON X.NEW_LOCATION_KEY = Y.NEW_LOCATION_KEY
end
Msg 8154, Level 16, State 1, Procedure ModDate_location_update, Line 6 [Batch Start Line 119]
The table 'dbo.dim_Location' is ambiguous.
Simply update all the rows having a PK that appears in the INSERTED virtual table. EG:
CREATE TRIGGER ModDate_location_update
ON dim_Location
AFTER UPDATE
AS
BEGIN
UPDATE dbo.dim_Location
SET UPDATE_DATE = GETDATE()
WHERE LOCATION_ID IN (SELECT LOCATION_ID FROM INSERTED)
end
I need to create a trigger in SQL Server that triggers every time that the value from column "status" = 'Baja'
CREATE TRIGGER trg_triggerName
ON dbo.table1
AFTER UPDATE
AS
BEGIN
IF status = 'Baja' THEN BEGIN
INSERT INTO dbo.table1 (fechaBaja)
VALUES (CURRENT_TIMESTAMP)
END
END
GO
I got this error message
Msg 207, Level 16, State 1, Procedure trg_FechaBaja, Line 3 [Batch Start Line 34]
Invalid column name 'status'.
IF status = 'Baja' THEN BEGIN
In this line the "status" give me the message "invalid column name 'status'" and I'm 100% sure that my column has that name.
IN RESUME: Got a table named table1 that has a column named 'status' and another column named 'fechaBaja'
Every time that the 'status' value changes to 'Baja', I need to trigger and update the cell 'fechaBaja' with the current_timestamp
All the operations are in the same table1.
First, you want an update, not an insert.
Second, A trigger in SQL Server is fired once per statement, not once per row. This means that if the update statement that fired the trigger have updated multiple rows, your trigger will be fired once, and include data about these rows in the inserted and deleted tables.
Third, You need to make sure that the update statement inside the trigger will not raise it again. Do that by configuring the database.
The code you need is something like this:
CREATE TRIGGER trg_triggerName ON dbo.table1
AFTER UPDATE AS
BEGIN
UPDATE t
SET fechaBaja = CURRENT_TIMESTAMP
FROM dbo.table1 As T
INNER JOIN Inserted As I
ON T.<PrimaryKey> = I.<PrimaryKey>
INNER JOIN Deleted As D
ON T.<PrimaryKey> = D.<PrimaryKey>
WHERE I.[status] = 'Baja'
AND (D.[Status] IS NULL OR D.[Status] <> 'Baja')
END
GO
I believe you want to update fechaBaja field if updated row status field equal'Baja'. You need to capture primary key information as well. I put 'Id' but you need to change it with your actuel field name.
CREATE TRIGGER trg_triggerName ON dbo.table1
AFTER UPDATE AS
BEGIN
UPDATE dbo.table1 SET fechaBaja=CURRENT_TIMESTAMP
WHERE EXISTS (SELECT 1 FROM INSERTED I WHERE dbo.table1.Id = I.Id AND I.Status='Baja')
END
GO
You can do it by joining the three tables like
CREATE TRIGGER trg_triggerName ON dbo.table1 AFTER UPDATE AS
BEGIN
UPDATE T1
SET T1.fechaBaja = CURRENT_TIMESTAMP
FROM dbo.table1 T1 INNER JOIN INSERTED T2 ON T1.ID = T2.ID
INNER JOIN DELETED T3 ON T1.ID = T3.ID
WHERE T2.[status] = 'Baja'
AND
(T3.[status] <> 'Baja' OR T3.[status] IS NULL);
END
GO
There is no need to use IF.
status is a reserved word, enclose it in brackets.
Join your table with INSERTED table to get all the IDs of the rows updated.
Join your table with DELETED table to get all the IDs where the data is not 'Baja'.
Filter by where INSERTED.[status] = 'Baja' AND DELETED.[status] <> 'Baja' to update only those rows.
I'm trying to insert new_file_name column data from document_image_volume1_rename_temp table into the document_image_volume1_rename table. Both tables have a column document_image_id.
USE myDatabase
INSERT INTO document_image_volume1_rename (new_file_name)
SELECT
new_file_name
FROM
document_image_volume1_rename_temp
WHERE
document_image_volume1_rename.document_image_id = document_image_volume1_rename_temp.document_image_id
Here's the error message:
Msg 4104, Level 16, State 1, Line 10
The multi-part identifier "document_image_volume1_rename.document_image_id" could not be bound.
I think what you are actually looking for is an UPDATE query not an INSERT query. If you don't want to add new rows but just modify a column in existing rows, try this:
UPDATE t2
SET t2.new_file_name = t1.new_file_name
FROM document_image_volume1_rename_temp t1 INNER JOIN document_image_volume1_rename t2 ON t1.document_image_id = t2.document_image_id
A handy reference on UPDATE query syntax by DBMS can be found in Eric's answer here.
I can't quite figure out how to update the last inserted row using table triggers.
I tried this:
create TRIGGER [dbo].[t_checkAucEquFields]<br />
ON [dbo].[_aucEquUpdateLog]
AFTER INSERT
as
update a set inserted.[Status] = coalesce(pes.id,'22')
from [_aucEquUpdateLog] a
left join v_pobEquStatus pes on pes.statusDescr = inserted.[Status]
I would like the text of inserted.Status - ex being "In stock" to be replaced with the integer pes.id. If the join cannot be made, I would like it to default to value 22.
I get this error when trying above query:
Msg 4104, Level 16, State 1, Procedure t_checkAucEquFields, Line 7
The multi-part identifier "inserted.Status" could not be bound.
I might be way off, but I am quite lost as of how to do it in a proper way?
You need to join the inserted table, and update the actual table instead of the inserted table
Try this:
CREATE TRIGGER [dbo].[t_checkAucEquFields]
ON [dbo].[_aucEquUpdateLog]
AFTER INSERT
as
begin
UPDATE a
SET
[Status] = coalesce(pes.id,'22')
FROM
[_aucEquUpdateLog] a
JOIN
inserted i
ON
i.[your primary key] = a.[your primary key]
LEFT JOIN
v_pobEquStatus pes
ON
pes.statusDescr = i.[Status]
end