Updating Field Based on Another Table - sql

As someone new to SQL can you please point me in the right direction. I know the following is wrong but I am not sure why.
UPDATE cus
SET cus.leg_no = new.leg_no
WHERE cus.c_no = new.c_no
The cus table currently has null in the leg_no. I want to update it from the new table. I will be joining on c_no which is in both tables.
I have tried searching the web but I am getting further confused. This has lead me to think I need FROM but something is telling me that is when using SELECT rather than UPDATE.

UPDATE cus,new
SET cus.leg_no = new.leg_no
WHERE cus.c_no = new.c_no

Here's the standard way to do it:
UPDATE cus
SET cus.leg_no = (select new.leg_no from new WHERE cus.c_no = new.c_no)
Here's the SQL Server/MS access dialect way to do it:
UPDATE cus
SET cus.leg_no = new.leg_no
FROM cus
INNER JOIN new
ON cus.c_no = new.c_no
Note that, with the standard method, if there are multiple rows in new that match a particular row from cus, you'll get an error message. Whereas with the SQL Server/MS access dialect form, the system will arbitrarily select one of the rows, and issue no warning or error.

Related

Updating a set of values using criteria from multiple tables

Given a specific set of rows that I've queried I want to update the "ifSpeed" value on each of them to a static value of 1000000000.
I have created a query to find all the specific rows that I need to update that value in, but had to join two tables to get that information, due to the the tag I'm using being in a separate table.
I have created a query to find all the specific rows that I need to update that value in, but had to join two tables to get that information, due to the the tag I'm using being in a separate table. That query is the first block of code. The second is one of my attempts at updating the values. I've also looked into subqueries, but my SQL is very rusty as it has been about 8 years since I've worked with it.
SELECT ifSpeed
FROM master_dev.device_interfaces AS MDDI
JOIN master_dev.device_interface_tags_map AS MDDITM
ON MDDI.if_id = MDDITM.if_id
WHERE ifSpeed=10000000
AND MDDITM.tag_id=13
UPDATE master_dev.device_interfaces
SET ifSpeed = '1000000000'
FROM master_dev.device_interfaces AS MDDI
JOIN master_dev.device_interface_tags_map AS MDDITM
ON MDDI.if_id = MDDITM.if_id
WHERE ifSpeed=10000000
AND MDDITM.tag_id=13
My expectation is that all rows that exist in my SELECT query have their ifSpeed updated to 1000000000.
What actually happens is simply an error. I'm using a restricted interface to query the DB and it only provides rows affected, the data from the rows or simply "ERROR". Very helpful... I know.
in SSMS try Below
---- Updated ---
UPDATE MDDI
SET ifSpeed = '1000000000'
FROM master_dev.dbo.device_interfaces AS MDDI
JOIN master_dev.dbo.device_interface_tags_map AS MDDITM
ON MDDI.if_id = MDDITM.if_id
WHERE ifSpeed = 10000000
AND MDDITM.tag_id = 13
The syntax of UPDATE that has a FROM clause is rather cumbersome. Luckily in SQL Server there is a better way.
I usually use CTE (common-table expression) in this case, which makes the query very easy to write and read and verify that it works as intended.
So, you have your complex SELECT statement that returns all the rows that you want to update. Great.
SELECT ifSpeed
FROM
master_dev.device_interfaces AS MDDI
JOIN master_dev.device_interface_tags_map AS MDDITM
ON MDDI.if_id = MDDITM.if_id
WHERE
ifSpeed=10000000
AND MDDITM.tag_id=13
;
Now, wrap it into CTE and update the CTE:
WITH
CTE
AS
(
SELECT ifSpeed
FROM
master_dev.device_interfaces AS MDDI
JOIN master_dev.device_interface_tags_map AS MDDITM
ON MDDI.if_id = MDDITM.if_id
WHERE
ifSpeed=10000000
AND MDDITM.tag_id=13
)
UPDATE CTE
SET ifSpeed = '1000000000'
;
You can write SELECT * FROM CTE instead of the line UPDATE CTE SET ... before performing an actual update to verify that you are selecting correct rows.
Thanks for all the input on this! Unfortunately I had two different teams give me two different answers. While I supposedly was able to do this after escalating further up the chain the engineering team on the backend put up a brick wall saying don't risk the DB tables. That decision baffles me based on what's affected, but not my call to make I suppose.
That being said they did let me know that the original code was fine and likewise the variation that maSTAShuFu provided.
#Vladimir - Sorry didn't get to try your's out, but I'm definitely keeping it in my back pocket. Thank you!

Why am I asked for "Enter parameter value" when running update query in MS Access?

I've been struggling with my query and don't know what is wrong with it. I am not an advanced SQL user, but I have some understanding of SQL.
I have two tables and I am trying to update one field with the field from the other table. They do not have a unique identifier in each, so they need to be matched with three fields (HoleID, From, To), especially because table LEAPFROG_Lithology has more records that do not have the same From/To than in the Lithology table.
I have looked for questions asked on similar subject, and tried different variant of my query, but none work and it always asks me to "Enter parameter value".
I tried with an inner joint like in this one and one, but it did not work.
Here is what I tried:
Version 1
Update LEAPFROG_Lithology
SET LEAPFROG_Lithology.Primary_litho = (
SELECT Lithology.Primary_litho
FROM Lithology
RIGHT JOIN LEAPFROG_Lithology ON (
Lithology.[HoleID] = LEAPFROG_Lithology.[HoleID] and
Lithology.[From_m] = LEAPFROG_Lithology.[From_m] and
Lithology.[To_m] = LEAPFROG_Lithology.[To_m]))
Where LEAPFROG_Lithology.Primary_litho is null ;
Version 2 (tried with an INNER JOIN as well)
Update LEAPFROG_Lithology
LEFT JOIN Lithology ON (
LEAPFROG_Lithology.[HoleID] = Lithology.[HoleID] and
LEAPFROG_Lithology.[From_m] = Lithology.[From_m] and
LEAPFROG_Lithology.[To_m] = Lithology.[To_m])
SET LEAPFROG_Lithology.Primary_litho = Lithology.Primary_litho
Where LEAPFROG_Lithology.Primary_litho is null ;
I tried with [ ] everywhere, with none. It doesn't make a difference.
Can someone tell me what am I doing wrong?
Thanks!
Make sure both your tables can form a relationship before using JOIN, you need to make sure your PRIMARY KEY in one table corresponds to the FOREIGN KEY in the other table to join them.
Access lets you use a design view to create queries which can be easy if you're not fluent with SQL. Once you design it visually you can switch to SQL view to find the code.
Please verify that the names of the tables and fields are correct. If so, the second query should work.
But I suggest to use a slightly different bracketing, so you can display the graphical design view of the query. Also, there's no need to use a LEFT JOIN, because for records that have no Primary_litho and don't have a matching Lithology, the NULL value would be updated with a NULL value. So this should work without any problem:
UPDATE LEAPFROG_Lithology
INNER JOIN Lithology ON
(LEAPFROG_Lithology.[HoleID] = Lithology.[HoleID]) AND
(LEAPFROG_Lithology.[From_m] = Lithology.[From_m]) AND
(LEAPFROG_Lithology.[To_m] = Lithology.[To_m])
SET LEAPFROG_Lithology.Primary_litho = Lithology.Primary_litho
WHERE LEAPFROG_Lithology.Primary_litho Is Null;

Update query based on two conditions

I have the following table based on this query:
SELECT
repName.repID, repName.Rep_Name, repName.Job_Code, GenItems.Item_Name,
repName.Entered
FROM
GenItems
INNER JOIN repName
ON GenItems.Job_Code = repName.Job_Code
ORDER BY
repName.Rep_Name
I want to add an update routine to it. I want to update the entered field if the user entry matches the rep.ID and the Item Name. and finally return the Max value for the Entered field. Can I add this to this query or is it better to write another.
I just started working with sql, so if my questions seems basic, please forgive me. I am self taught and stumbling greatly.
Thank you
I don't understand fully your question.
You are showing us a SELECT statement. It can only be used to return a table-like result. If you want to upate a table you must use an UPDATE query. For the SQL-Server (and SQL CE) the query looks like this:
UPDATE repName
SET repName.Entered = x
FROM
GenItems
INNER JOIN repName
ON GenItems.Job_Code = repName.Job_Code
WHERE
repName.repID = x AND GenItems.Item_Name = 'y'
The difficulty is that tables have to be joined in the UPDATE statement. This not supported in Oracle for instance, where you have to do it with sub-selects.

Trying SQL table update matching on string field

Could really use some help with an update query...(SQL Serer 2008 R2 Express)
I have two tables, tblJP and tblMaster.
I only have a string field that matches between the two tables.
tblJP AND tblMaster
I need to update tblJP.LangString with tblMaster.Long_text when
tblJP.short_text = tblMaster.short_text AND tblMaster.Lang = 'jp'
Any help would be greatly appreciated. I am spinning my wheels trying all sorts of logic and syntax from creating temp tables to other types of joins all with no luck.
A simple update with an INNER JOIN should do the trick.
UPDATE tblJP
SET tblJP.LangString = tblMaster.Long_Text
FROM tblJP
INNER JOIN tblMaster ON tblMaster.alt_text = tblJP.short_text
WHERE tblMaster.Lang = 'jp'
WARNING: Never run an update statement against your production server without first testing it against a development server - especially when someone else wrote the SQL.
You could also use MERGE
MERGE INTO tblJP
USING (SELECT *
FROM tblMaster
WHERE Lang = 'jp') AS SOURCE
ON SOURCE.alt_text = tblJP.short_text
WHEN MATCHED THEN
UPDATE SET LangString = SOURCE.Long_Text;
In the event that the JOIN returns multiple rows you will be alerted to the problem with an error The MERGE statement attempted to UPDATE or DELETE the same row more than once.

Update Query from a Lookup Query

I have a spreadsheet that I am converting to an Access DB. I have a column of typed out customer names that I want to replace with the appropriate customer number from our accounting system.
I have created a table with the customer info, and a query that shows what ID needs to be inserted into the source data. What I'm looking for is:
UPDATE tblStarting_Data
SET CustomerID=x
WHERE TEMPCustomer=y
Where X and Y come from qryIDPerCustomer.
Can I use a loop? How do I reference another query?
Another possibility in MS Access (object names borrowed from Tomalak answer):
UPDATE tblStarting_Data, qryIDPerCustomer
SET tblStarting_Data.CustomerID=qryIDPerCustomer.CustomerID
WHERE tblStarting_Data.TEMPCustomer=qryIDPerCustomer.CustomerName
I think a JOIN will help you:
UPDATE
tblStarting_Data AS sd
INNER JOIN qryIDPerCustomer AS qc ON sd.TEMPCustomer = qc.CustomerName
SET
sd.CustomerID = qc.CustomerID;
This can be expressed as a correlated sub-query as well (though the join syntax is preferable):
UPDATE
tblStarting_Data
SET
CustomerID = (
SELECT CustomerID
FROM qryIDPerCustomer
WHERE CustomerName = tblStarting_Data.TEMPCustomer
)
No need for a loop, both statements will update all records in tblStarting_Data in one step.