SQL Server 2008 - Ambiguous column name - sql-server

I am trying to do a join between two tables but I am receiving this error
Ambiguous column name 'NurseWard'
Code:
select
#WardID = NurseWard
from
dbo.NurseTbl as o
inner join
inserted as i on o.NurseID = i.NurseID

This looks like the inserted table in a trigger. The inserted table has the same column names as the original table. From the linked article:
The inserted table stores copies of the affected rows during INSERT
and UPDATE statements. During an insert or update transaction, new
rows are added to both the inserted table and the trigger table. The
rows in the inserted table are copies of the new rows in the trigger
table.
The query has an alias on both the NurseTbl and inserted tables, and the inner join uses the aliases, but the select does not. You will need to add an alias to the NurseWard column in the select:
select #WardID = o.NurseWard -- or i.NurseWard depending on what you need
from dbo.NurseTbl as o
inner join inserted as i on
o.NurseID = i.NurseID

Related

Merge vs left join to insert missing data

I want to bring in data from table a into table b every day.
Both tables have same schema.
I can either use MERGE (WHEN NOT MATCHED THEN INSERT) or I can do an INSERT of the result obtained by doing a left join of table a and table b on id where b.id is null.
Are these both approaches effectively the same?

Why trigger is doing unnecessary subtraction after a query in sql?

I have two tables:
Order and
Product.
I want a specific column(OnShelfQuantity) in the Product table to be updated as a new row is added in the Order table. I have used the below query to implement a trigger which will do that. But the problem is that when I insert a row in the Order table and then later check the Product table to see the changes, I notice that the Product table has been updated 3 times. For e.g: Order quantity inserted = 10, then only 10 should be subtracted from Product_TAB.OnShelfQuantity. But 30 gets subtracted. Please help!
create trigger dbo.Trigge
ON dbo.Ordertable
AFTER INSERT
AS
BEGIN
update Product_TAB set OnShelfQuantity= Product_TAB.OnShelfQuantity - Ordertable.Quantity
FROM dbo.Product_TAB
INNER JOIN Ordertable
ON Ordertable.ProductID = Product_TAB.ProductID;
END;
I think, you can use INSERTED table to resolve this Issue.
Inserted table is a table which is used by triggers to store the frequently inserted records in the tables.
So, you can use the same in your update statement to avoid this.
update Product_TAB set OnShelfQuantity= Product_TAB.OnShelfQuantity -
Ordertable.Quantity
FROM dbo.Product_TAB
INNER JOIN Ordertable ON Ordertable.ProductID = Product_TAB.ProductID
INNER JOIN inserted INS ON INS.Order_ID=Ordertable.Order_ID
You can have multiple rows in the inserted table. And, these rows could have the same product. A row in the target table is only updated once in an update statement. Hence, you want to aggregate the data before the update:
create trigger dbo.Trigge
ON dbo.Ordertable
AFTER INSERT
AS
BEGIN
update p
set OnShelfQuantity= p.OnShelfQuantity - i.total_quantity
from dbo.Product_TAB p JOIN
(SELECT i.ProductId, SUM(i.Quantity) as total_quantity
FROM inserted i
GROUP BY i.ProductId
) i
on i.ProductID = p.ProductID;
END;
Note that this only uses inserted and not the original table.
So the issue was that I was inserting new rows but with the same Order ID. This is why it was doing additional subtraction which I didn't require. So now I have to just insert a new row but with unique OrderID. Thanks to everyone who replied above!

Join inserted record with latest record target table

i'm setting up a trigger that, in case of a new record being added to my source table, inserts a record into my target table with information based on the data in the first inserted record.
Since the new data in the target table are basically calculated fields based on the original latest record in the target table and the inserted record in the source table, i basically need a way to join the last record of my target table and my INSERTED table.
i've tried selecting the top row of my target table and join this with my INSERTED table, but so far it has not worked
CREATE TRIGGER dbo.NewData ON dbo.t_stats_lijn1 AFTER INSERT AS
IF Exists(select top 1 Categorie from INSERTED where Categorie = 'geen stop' order by p_timestamp desc)
begin
declare #joincause int = 0
insert into dbo.T_VALUE_LIJN1 ([Event ID]
,[p_timestamp]
,[Model]
,[Modelnaam]
,[running_time]
)
select
i.event_id,
i.p_timestamp
i.model
t.running_time + i.delta
from inserted i left join (select top 1 * from t_value_lijn1 order by p_timestamp) t on #joincause = 0
end
go

Trigger in SQL Server 2008 R2

I am trying to create a trigger in SQL Server 2008 R2. I have three tables and they have PK-FK -FK relationship.
I have a column in first table where column name is STATUS that column has value 'inactive' or 'active' and in second table I have column FKNEWLoanreviewfieldid which has multiple values C2,C4,C6 and in third table I have FKWarningtypeID has different value like 4,3,2,1.
I have to create a trigger so that if FKwarningtypeID is updated to value 4 then in first table (where I have status column) the column value of status will be modified to 'active'.
Can any body help me please.
You need something like this:
CREATE TRIGGER trg_UpdateYourColumn
FOR UPDATE ON dbo.YourThirdTableHere
AS
-- update the first table
UPDATE
dbo.YourFirstTableHere
SET
-- set [Status] to 'Active'
-- (I sure hope this isn't *REALLY* stored as a *STRING* value!)
[Status] = 'Active'
FROM
dbo.YourFirstTableHere t1
INNER JOIN
-- join first and second table on some common column
dbo.YourSecondTableHere t2 ON t1.XX = t2.XX
INNER JOIN
-- join second and third table on some common column
dbo.YourThirdTableHere t3 ON t2.YY = t3.YY
INNER JOIN
-- join third table with the "Inserted" pseudo table
Inserted i ON t3.ID = i.ID
INNER JOIN
-- join third table with the "Deleted" pseudo table
Deleted d ON t3.ID = d.ID
WHERE
i.FKwarningtypeID = 4 -- new value is 4
AND d.FKwarningtypeID <> 4 -- old value was *NOT* 4

How do I make a trigger that only affects the row that was updated/inserted?

I have a table with two columns where I need one (columnB) to be a copy of the other one (columnA). So, if a row is inserted or updated, I want the value from columnA to be copied to columnB.
Here's what I have now:
CREATE TRIGGER tUpdateColB
ON products
FOR INSERT, UPDATE AS
BEGIN
UPDATE table
SET columnB = columnA
END
The problem now is that the query affects all rows, not just the one that was updated or inserted. How would I go about fixing that?
Assuming you have a primary key column, id, (and you should have a primary key), join to the inserted table (making the trigger capable of handling multiple rows):
CREATE TRIGGER tUpdateColB
ON products
FOR INSERT, UPDATE AS
BEGIN
UPDATE table
SET t.columnB = i.columnA
FROM table t INNER JOIN inserted i ON t.id = i.id
END
But if ColumnB is always a copy of ColumnA, why not create a Computed column instead?
Using the inserted and deleted Tables
There is a special inserted table available in triggers that will contain the "after" version of rows impacted by an INSERT or UPDATE operation. Similarly, there is a deleted table that will contain the "before" version of rows impacted by an UPDATE or DELETE operation.
So, for your specific case:
UPDATE t
SET t.columnB = t.columnA
FROM inserted i
INNER JOIN table t
ON i.PrimaryKeyColumn = t.PrimaryKeyColumn

Resources