Update Multiple records in single query MS SQL - sql-server

I am trying to update multiple records in the table using the temporary table using the below query, but doesn't working. Please tell me the proper way to update multiple records.
UPDATE sarufoo
SET sarufoo.mobile = (SELECT mobile_no FROM logan)
WHERE sarufoo.homep IN (SELECT homep FROM logan);

One neater way to achieve that be would to join the two tables:
UPDATE sf
SET sf.mobile = l.mobile_no
From
sarufoo sf
JOIN logan l ON sf.homep = l.homep

You have to correlate the row you are updating with the row you are selecting from. Otherwise your subselect (the SET one) will return every row it has.
UPDATE sarufoo
SET sarufoo.mobile = (SELECT mobile_no FROM logan WHERE sarufoo.homep = logan.homep)
WHERE sarufoo.homep IN (SELECT homep FROM logan);

Related

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 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...Set...From...Where query is affecting all rows and not specific ones

The below query should only affect the row titled 'F0085'. However, it has affected all the rows. Setting the address to 'test' on every Job_No.
UPDATE TBTest.dbo.Site
SET Address ='FDAN'
FROM Jobs
WHERE Job_No = 'F0085'
This is the first table:
Your syntax is wrong. Since your update depends on other table, you need to join them. Try:
UPDATE t
SET
t.Address = 'FDAN'
FROM
TBTest.dbo.Site t
INNER JOIN JOBS j
ON t.Job_No = j.Job_No
WHERE j.Job_No = 'F0085'
Thank you all. I needed to find the common column (the ID column).
UPDATE t
SET
t.Address = 'FDAN'
FROM
TBTest.dbo.Site t
INNER JOIN Jobs j
ON t.Site_ID = j.Job_ID
WHERE j.Job_No = 'F0085'
I don't fully understand how the aliases work. But it appears to be working fine.
UPDATE TBTest.dbo.Site ST
INNER JOIN Jobs JB ON JB.Job_No=ST.JOB_NO
SET ST.Address =JB.Address
Try above query.

Transfer data from one table to another in sql server

I have two tables, one of which I don't need anymore. I want to transfer the piece of data i need from the obsolete table, into the table I'm going to keep. There are bookingid columns in both tables, which I can use to match the rows up. Its a 1 to 0 or 1 relationship. I've looked around and built up this query to accomplish the transfer, but I'm getting a could not be bound error on bookingtoupdate.bookingid
WITH bookingtoupdate (bookingid) AS
(
SELECT bookingid
FROM bookings
)
UPDATE bookings
SET meetinglocation = (SELECT business.name
FROM abk_Locations
INNER JOIN business ON dbo.abk_Locations.IP_Number = business.businessid
WHERE
(dbo.abk_Locations.Booking_Number = bookingtoupdate.bookingid)
)
WHERE
bookingid = bookingtoupdate.bookingid
Are there any obvious issues with my code?
I referred the following pages...
http://msdn.microsoft.com/en-us/library/ms175972.aspx
SQL Server FOR EACH Loop
You declare bookingtoupdate but you don't select anything from it. That's why it can't be bound.
Here is a simplified query to do what you need without CTE
UPDATE bookings
SET meetinglocation = business.name
FROM bookings
INNER JOIN abk_Locations ON abk_Locations.Booking_Number = bookings.bookingid
INNER JOIN business ON dbo.abk_Locations.IP_Number = business.businessid

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