Update SQL query in SQL Server - sql

I have an issue where I need to update a field in a number of SQL Server tables. The customer Order ID needs to be updated on all records that do not follow the standard format "C000001" (a letter followed by numbers). There are approximately 300 records that need changed (carry over records from a previous database version), as well as the corresponding linked tables.
I am a bit rusty on my SQL, so would like to verify the commands.
My proposed commands
UPDATE Customer_Order
SET ID = CONCAT('X', ID)
WHERE ID not like 'c%';
UPDATE Customer_Order_Line
SET Cust_Order_ID = CONCAT('X', Cust_Order_ID)
WHERE Cust_Order_ID not like 'c%';
UPDATE Quote_Order
SET Cust_Order_ID = CONCAT('X', Cust_Order_ID)
WHERE Cust_Order_ID not like 'c%';
etc... (I have approx 12 additional tables to update same as above)
Assistance is appreciated, thanks in advance.

More of a formatted comment here..
Any time you want to do this, just make your update a select to verify your update:
SELECT ID, CONCAT('X', ID) as NewID
FROM Customer_Order
WHERE ID not like 'c%'
Make sure the NewID column is what you want to replace ID with.

Related

Replace "?" with "Ñ" in bigquery table

I need to perform an update in 21 rows. The way I came up with is using a query to get the new values and the id where to update the table. However, I'm getting UPDATE/MERGE must match at most one source row for each target row.
The data looks like this:
new_address address id
ÑANDU 123 ?ANDU 123 17-18
OTOÑAL 12 OTO?AL 12 10-16
The query I'm using looks like this:
UPDATE table
SET direccion = new_address
FROM (SELECT new_address FROM(SELECT REGEXP_REPLACE(address,r'\?',"Ñ") AS new_address,address,id
FROM table) WHERE address LIKE "%?%")
WHERE id IN (SELECT id FROM (SELECT REGEXP_REPLACE(address,r'\?',"Ñ") AS new_address,address,id
FROM table) WHERE address LIKE "%?%")
Why is this error and how can I achieve this?
There is no "join condition" in the WHERE clause (e.g. on the id column) to say which value generated by the FROM clause applies to a given target row.
But in this case you don't need a "joined update"; a simpler "searched update" would do:
UPDATE table
SET address = REGEXP_REPLACE(address,r'\?',"Ñ")
WHERE address LIKE "%?%"
If you did want to use joined update it would look something like:
UPDATE table
SET address = new_address
FROM (SELECT REGEXP_REPLACE(address,r'\?',"Ñ") AS new_address, id as upd_id
FROM table WHERE address LIKE "%?%")
WHERE id=upd_id
Not sure why you are trying some heavy workaround instead of just going as simple as below
UPDATE table
SET address = REGEXP_REPLACE(address, r'\?', "Ñ")
WHERE REGEXP_CONTAINS(address, r'\?')

How to update column based on filter of another column?

I'm trying to basically update a subset of rows in a table using a filter but currently my query is updating all values instead of the intended subset. I'm not really sure what I'm missing.
Here's what the current (non working) update looks like.
UPDATE account_type
SET type_id = 3
FROM my_filter;
I have been able to successfully select the data I want, I just can't figure out how to get the update to work. I am able to access the fields correctly with the following select statement.
SELECT account_type FROM my_filter;
But when I attempt to update, it updates every field. Here's what the (working) join I have looks like, wrapped in a CTE.
WITH my_filter AS
(
SELECT account_type.type_id, username
FROM account_type
INNER JOIN user ON user.account_id = account_type.account_id
WHERE username LIKE 'filter%'
)
You need a join condition. I would guess:
UPDATE account_type
SET type_id = 3
FROM my_filter
WHERE account_type.type_id = my_filter.type_id;
Alternative, you can write this as:
UPDATE account_type
SET type_id = 3
FROM user
WHERE user.account_id = account_type.account_id AND
user.username LIKE 'filter%';

Perform multiple updates in a single query in an access database table

In my access 2016 database, I have a table tblCustomerInfo with a field customer_code. There are some old customer_code values that need to be updated to newer values (for example, all rows with customer_code = 103 should be updated to customer_code = 122).
I can achieve something like this going one customer_code at a time using queries like:
UPDATE tblCustomerInfo set customer_code = 122 Where customer_code = 103;
UPDATE tblCustomerInfo set customer_code = 433 Where customer_code = 106;
...
however, I would like to avoid having to run a separate query for each customer_code. Is there any way to update all the codes, each to a different new value, in a single query?
Create a table eg CustomerCodes with two fields oldcode and newcode, and add in all the values. Then run an update query like this:
UPDATE CustomerCodes INNER JOIN tblCustomerInfo
ON CustomerCodes.OldCode = tblCustomerInfo.Customer_Code
SET Customers.Customer_Code = [CustomerCodes].[NewCode];
Alternative
If there aren't too many to change you can use a switch statement like this:
UPDATE tblCustomerInfo
SET Customer_Code =
SWITCH(Customer_Code=103,126,
Customer_Code = 106,130,
Customer_Code = 107,133);
There is, in my experience, a limit on the number of pairs you can have in a switch statement, although I have never bothered to find out exactly what the limit is - it doesn't seem to be documented.

Updating value in a column SQL Server

This should be a really easy question, but I wanted to make sure it was done right where I'm not a SQL guy. We need to turn all of the patients with a security level of 1 to 2. All patients who are not at 1 are currently at 2. Thanks and sorry for asking such a simple question. SQL Server 2005.
select
patient_id,
security_level
from patient
where security_level = '1'
SQLFIDDLE
update patient set security_level = '2'
where security_level = '1';
Above will update the entire patient table, and set the security level to 2 (where it initially was 1).
A simple update should work. Something like:
UPDATE patient
SET sercurity_level='2'
WHERE security_level='1';
Edit: changed correct table name and added quotes

Using SQL to determine if all records of a related table have the same value

I'm trying to create a stored procedure that will update a record on one table depending on certain values in a related table:
MainTable contains a status field and is related to SubTable which also has a status field. I need to update the status of each record in MainTable where ALL of the related records in SubTable has the same status x. I've tried several queries but think I am going about it the wrong way. Any assistance would greatly be appreciated. Thanks.
for example having following tables :
Producer ( Code,Name,Status)
Goods ( GCode,PCode( Producer Code),Name,Status)
and Query :
Update Producer set Status=F
where not exists (select * from Goods where Status <> X and Goods.PCode= Producer.Code)
Try this :
Update schema.yourtable s set s.fieldtoupdate = (
Select e.Relationalfield from schema.RealtionalTable e where e.STATUSFIELD = s.STATUSFIELD);
Hope it helps