I need to update two table in single query - sql-server

I need to update two table in single query.
Please find the below query.
Update
m_student_moreinfo
INNER JOIN
m_student
ON
m_student_moreinfo.studentID = m_student.id
SET m_student_moreinfo.MIAStartdate=GETDATE(),m_student.status='Clinical MIA'
where
studentID IN
(
Select
smi.studentID
FROM
dbo.m_student st
INNER JOIN
dbo.m_student_course sc
on
sc.studentID=st.id
INNER JOIN
dbo.m_student_classClinical scl
on
scl.studentcourseID=sc.id
INNER JOIN
dbo.m_student_moreinfo smi
on
smi.studentID=st.id
where
scl.startDate<=GETDATE() and scl.endDate >=GETDATE()
and MIAStartdate IS NULL
)
I am getting Incorrect syntax near Inner.

You can't update two tables at once, but you can link an update into an insert using OUTPUT INTO, and you can use this output as a join for the second update
please see this and that for more info
So basically you can wrap this in transaction and commit after all update steps finished.

Simple answer: You can not.
What you can do is two update queries in a transaction:
BEGIN TRANSACTION;
update query 1
update query 2
COMMIT;
That wil do the job for you

Related

Using multiple update or inner join is better for performance

I have the following update statement I use in trigger that have same behavior and both of it is correct but 2nd one have problem if I update both tableA_ID and ValueB at same times. But this not big issue as there is work around but if possible I want to avoid it. So what I want to know is there a big performance between this 2 and it will make me decide which one to go. If 2nd much better in performance than I will use 2nd one. But if the difference not that big I will use 1st one. I not expert in sql server. Anyone can help?
UPDATE tableA
SET tableA.Value = tableA.Value - old.ValueB
FROM tableA
INNER JOIN (
SELECT tableA_ID ,ValueB
FROM deleted
) old ON old.tableA_ID = tableA.ID
UPDATE tableA
SET tableA.Value = tableA.Value + new.ValueB
FROM tableA
INNER JOIN (
SELECT tableA_ID ,ValueB
FROM inserted
) new ON new.tableA_ID = tableA.ID
UPDATE tableA
SET tableA.Value = tableA.Value - old.ValueB + new.ValueB
FROM tableA
INNER JOIN (
SELECT tableA_ID ,ValueB
FROM deleted
) old ON old.tableA_ID = tableA.ID
INNER JOIN (
SELECT tableA_ID ,ValueB
FROM inserted
) new ON new.tableA_ID = tableA.ID
As with anything, test it to be sure.
Whether or not you should be doing this in a trigger is a long discussion.
Performance-wise, you're going to see better performance, in most circumstances, with the second query. The reason I can simply state that is that you're only looking at a single lookup for the UPDATE in the query where you update everything all at once. Whereas, the first query has to find the data to UPDATE, two times.
However, again, test this.
And yes, as #SMor says in the comments, why you need this as a trigger should absolutely be discussed. Triggers can be seriously misused and abused.

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!

wanted to update target coloum with source table and where condition with two coloumn

Not able to update target table.
Using where condition from both table ac_no and part_no column should be match.
I tried this code but when I execute in actionoutput shown only
running
Please help me determine where my code does not work
UPDATE
wo.rr_sec
INNER JOIN
wo.rb_acpt ON
rr_sec.AC_NO = wo.rb_acpt.ac_no
SET
wo.rr_sec.p_name = wo.rb_acparts.p_name
WHERE
wo.rr_sec.AC_NO=wo.rb_acpt.ac_no and
wo.rr_sec.PART_NO=wo.rr_acparts.part_no;
Based on your comment, I assume you want something similar to:
UPDATE
wo.rr_sec
SET
wo.rr_sec.p_name = wo.rb_acparts.p_name
FROM
wo.rr_sec
INNER JOIN
wo.rb_acpt ON
wo.rr_sec.AC_NO = wo.rb_acpt.ac_no
INNER JOIN
wo.rr_acparts ON
wo.rr_sec.PART_NO=wo.rr_acparts.part_no;

Join Multiple result tables into permanent

I have a number of queries that are run at the same time but now I want the result to populate a permanent table that I've created.
Each of the queries will have a column called 'Descript' which is what I want all the results to join to so i want to make sure that if the Descript column is out of order (or null) on one of the queries it will link the figures to the correct Descript.
I performed an INTO after the end of each query being run but this didn't work.
The first level of data went in but the second level just went underneath the first (if that makes sense) creating more rows.
INSERT INTO dbo.RESULTTABLE (Descript, Category, DescriptCount)
SELECT Descript, Category, DescriptCount
FROM #Query1
I have around 15 queries to join into 1 table so any help to understand the logic is appreciated.
Thanks
If I understood your question clearly, you want to insert query results which is not stored in the Temptable and update already existing records in the table.
update R set Category = Q.Category, DescriptCount = Q.DescriptCount,
from #ResultTable R inner join #Query1 Q ON R.Descript = Q.Descript
INSERT INTO dbo.RESULTTABLE (Descript, Category, DescriptCount)
SELECT Descript, Category, DescriptCount FROM #Query1 where Descript NOT IN (select Descript from #ResultTable)
Then you can process the same approach for other queries.

Update Query issue in SQL for multiple rows

I am trying to use an update query but so far it is keep failing on me and i don't understand what am i doing wrong here. I am getting this error 'Update canceled: attempt to update a target row with values from multiple join rows'. I know the table called OTHER_TABLE has duplicate records. Here is my current query:
UPDATE MAINTABLE
SET BLDG_NBR = DM.BLDG_NBR
FROM OTHER_TABLE DM
WHERE MAINTABLE.BLDG_NM = DM.BLDG_NM
You need to join the two tables
UPDATE MAINTABLE
SET BLDG_NBR = DM.BLDG_NBR
FROM MAINTABLE INNER JOIN OTHER_TABLE DM
ON MAINTABLE.BLDG_NM = DM.BLDG_NM
According to your comment, you have no index set on the tables and because of that it performs full table scan. Try adding index on both tables before executing the update statement,
CREATE NONCLUSTERED INDEX MAINTABLE_BLDGNM_idx ON MAINTABLE(BLDG_NM);
CREATE NONCLUSTERED INDEX OTHERTABLE_BLDGNM_idx ON OTHER_TABLE(BLDG_NM);

Resources