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'\?')
Related
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%';
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.
In an already existing table I would like to create a new column containing the name of the query I am using.
For example:
ID Name
-----------
1 Max
2 Jack
The desired output is:
ID Name Query
-------------------
1 Max QueryName
2 Jack QueryName
I think you can use a static value field in your query, like this:
SELECT ID, NAME, "QueryName" AS Query
FROM yourTable;
When you just want to add a column with a static value in result of querying a table, above is the solution, If you want to create a Query with a special name then use it, just use its name in its query in creation time.
I mean when you are changing name of a query; also edit its query with using that name.
But, I don't think this is a good idea to store a query then change its name and use its name as a result of its query or other queries, It seems you want to store a string value somewhere then use it by adding it in another query, So:
Create another table(QueryTable) like:
QueryId | QueryName
--------+--------------
1 | Query Name 1
2 | Query Name 2
Use it in your other queries:
SELECT t.*, q.QueryName
FROM yourTable t CROSS JOIN QueryTable q
WHERE q.QueryId = 1;
And I suggest this because it's better to use a tool in its using zone, when you want to store data and use it result some information; use a table for that. HTH ;).
I need a like statement to find two strings in 1 column, ie.
Table name is CustomerNumber
Column name is ID
Sample strings in column ID would be:
12345678909876543210
98765432109876543210
I want to find the row that contains 456 and 654.
My SQL statement is:
Select *
from CustomerNumber
where ID like all (values ('%456%'), (%654%'))
But I am getting expression errors.
You can use like:
select cn.*
from customernumber cn
where cn.id like '%456%' and cn.id like '%654%';
If you want them in a particular order, then use one like pattern:
select cn.*
from customernumber cn
where cn.id like '%456%654%';
I have never seen the construct like all nor like with a subquery that returns multiple rows. I imagine that generates a syntax error.
SELECT *
FROM CustomerNumber
WHERE ID like '%456%' AND ID like %654%'
I have a table CustPurchase (name, purchase) and another table CustID (id, name).
I altered the CustPurchase table to have an id field. Now, I want to populate this newly created field by referencing the customer ids from the CustID table, using:
UPDATE CustPurchase
SET CustPurchase.id = CustID.id
WHERE CustPurchase.name = CustID.name;
I keep getting syntax errors!
I believe you are after the useful UPDATE FROM syntax.
UPDATE CustPurchase SET id = CI.id
FROM
CustPurchase CP
inner join CustID CI on (CI.name = CP.name)
This might have to be the following:
UPDATE CustPurchase SET id = CI.id
FROM
CustID CI
WHERE
CI.name = CustPurchase.name
Sorry, I'm away from my Postgres machine; however, based upon the reference, it looks like this is allowable. The trouble is whether or not to include the source table in the from_list.
Joining by name is not an ideal choice, but this should work:
UPDATE custpurchase
SET id = (SELECT c.id
FROM CUSTID c
WHERE c.name = custpurchase.name)
The caveat is that if there's no match, the value attempting to be inserted would be NULL. Assuming the id column won't allow NULL but will allow duplicate values:
UPDATE custpurchase
SET id = (SELECT COALESCE(c.id, -99)
FROM CUSTID c
WHERE c.name = custpurchase.name)
COALESCE will return the first non-NULL value. Making this a value outside of what you'd normally expect will make it easier to isolate such records & deal with appropriately.
Otherwise, you'll have to do the updating "by hand", on a name by name basis, to correct instances that SQL could not.