SQL update query issue - sql

I just accidentally noticed that a Query like
Update tableA tableA set id = '5'
Works fine. Should this give error as I am using table name twice here.
Any thoughts why is this working fine ?

Because the second tableA is seen as an alias. There is no constraint in regards to the name of the alias, so you can have the same name on the alias as the table name.

your code exactly equal
Update tableA as 'tableA' set id = "5"
or
Update tableA as "tableA" set id = "5"
this is simple alias as Sql Alias Tutorial

Related

updating sql query value with select statement

I am trying to execute a query which is something like:
update table set column=(select column1 from table1);
I just want to store the value from other table to my column
but when i try my sql query it says
ERROR 1242 (21000): Subquery returns more than 1 row
definitely this means my table1 contains more than 1 row so i want to know that is there any way to store data into column from other table with multiple row.
or basically saving content of other table as a text something like
update table set column='Data in text from other table';
You probably need a correlation clause:
update table
set column = (select column1 from table1 where table.col = table1.col);
You need to decide what column(s) are used for the correlation.
it will work as i am getting your requirement.Please let me know if your requirement is other i ll make changes in query
update table_name t1
inner join table1 t2 on t1.id =t2.id
set column =column1
Your nested query returns multiple rows so you encountered this error.
Try in this way
UPDATE FirstTable
SET FirstTable.ColumnName =tbl2.ColumnName
FROM SecondTable tbl2 WHERE tbl2.Id = FirstTable.Id
There should be a common id or something that will help to find exact row.

update table - multi-part identifier could not be bound

I need to update a single column from a backup version of a database. I've restored the backup to a different db location and am running the following:
update table_name
Set
column = restoredDB.dbo.table_name.column
From restoredDB.dbo.table_name
Where restoredDB.dbo.table.ID = table_name.ID
The following result is returned:
The multi-part identifier "table_name.ID" could not be bound
The ID field is a primary key, bigint. I've found many postings on the topic of "multi-part identifier could not be bound" however they don't seem to apply to my situation. Could someone explain why this is happening and how to correct the error?
Thx.
All you need for column refernces is the TableName.ColumnName:
update table_name
Set
column = table_name.column
From restoredDB.dbo.table_name
Where table.ID = table_name.ID
Also, in your Where table.id = expression, you do not have any table name table in your example query.
You have to join the tables. To this:
from restoredDB.dbo.table_name
add this
join table_name on restoredDB.dbo.table.ID = table_name.ID
which means that you can get rid of this:
where restoredDB.dbo.table.ID = table_name.ID
This should work:
UPDATE table_name
SET table_name.column = restoredDB.dbo.table_name.column
FROM table_name
JOIN
restoredDB.dbo.table_name.column ON table_name.ID = table_name.ID
Based on SQL update query using joins
I think this should resolve the issue.
USE [<table_name_DB_Name>]
GO
UPDATE table_name
SET
column = t1.column
From restoredDB.dbo.table_name t1, table_name t2
Where t1.ID = t2.ID

I want to use select query in the update query statement

I have changed three columns by using a replace command in sql server with following query
select name ,replace (name,'...','.') as names from dbo.emp_2 where name like '%..%'
After replacing the replaced values has been entered in a new row .
Now i just wants to update the replaced values into original table . SO, I was thinking to use select query in the update query .
Is that possible , Is it so kindly provide a sample query for that
Try something like this
Update dbo.emp_2
set name =replace (name,'...','.') where name like '%..%'
Update dbo.emp_2
Set name = replace (name,'...','.')
Where name like '%..%'
You can do:
UPDATE TableA
SET Column1 = 'Test' + Column1
You can even make joins in your update query
UPDATE A
SET A.Column1 = 'Test' + B.Column2
FROM TableA A
INNER JOIN TableB B
ON B.B_Id = A.A_Id

Oracle SQL update

I've tried searching for this particular topic here, but haven't found the answer... Anyway, my aim is to update table (let's call it t_item), specifically column owner_id with values depending on another table (t_item_geo which is in turn linked to t_geo).
I'm not entirely sure whether the syntax below is actually valid for update statements.
UPDATE t_item SET owner_id= 6993 WHERE t_item.owner_id in
(SELECT t_item.owner_id FROM
t_item,
t_item_geo,
t_geo
WHERE
t_item.id = t_item_geo.item_id and
t_item_geo.geo_id = t_geo.id and
t_item.owner_id in (SELECT id FROM t_user WHERE network_id='fffffff') and
t_geo.id in (SELECT id FROM t_geo WHERE full_name = 'yyyyyyy')
);
Anyway, my problem with this query is that it updates far more rows than it should - if I separate just the select statement Oracle returns ~750 rows but the udpate itself updates more than 4000 rows. It's almost as if the condition was completely ignored - which would point me to perhaps incorrect syntax.
I need to update specific value in the table based on the select from few other 'joined' tables. Hope it makes sense.
Thanks for any contribution!
UPDATE: sorry - maybe it wasn't clear from the question itself, but the correct number of edited items should be ~750 and not ~4000. Thanks!
try this
MERGE INTO t_item
USING
(
SELECT t_item.owner_id FROM
t_item,
t_item_geo,
t_geo,
t_item.rowid rowid_sub
WHERE
t_item.id = t_item_geo.item_id and
t_item_geo.geo_id = t_geo.id and
t_item.owner_id in (SELECT id FROM t_user WHERE network_id='fffffff') and
t_geo.id in (SELECT id FROM t_geo WHERE full_name = 'yyyyyyy')
) on (rowid = rowid_sub)
WHEN MATCHED THEN
UPDATE SET owner_id= 6993;

Writing a single UPDATE statement that prevents duplicates

I've been trying for a few hours (probably more than I needed to) to figure out the best way to write an update sql query that will dissallow duplicates on the column I am updating.
Meaning, if TableA.ColA already has a name 'TEST1', then when I'm changing another record, then I simply can't pick a value for ColA to be 'TEST1'.
It's pretty easy to simply just separate the query into a select, and use a server layer code that would allow conditional logic:
SELECT ID, NAME FROM TABLEA WHERE NAME = 'TEST1'
IF TableA.recordcount > 0 then
UPDATE SET NAME = 'TEST1' WHERE ID = 1234
END IF
But I'm more interested to see if these two queries can be combined into a single query.
I am using Oracle to figure things out, but I'd love to see a SQL Server query as well. I figured a MERGE statement can work, but for obvious reasons you can't have the clause:
..etc.. WHEN NOT MATCHED UPDATE SET ..etc.. WHERE ID = 1234
AND you can't update a column if it's mentioned in the join (oracle limitation but not limited to SQL Server)
ALSO, I know you can put a constraint on a column that prevents duplicate values, but I'd be interested to see if there is such a query that can do this without using constraint.
Here is an example start-up attempt on my end just to see what I can come up with (explanations on it failed is not necessary):
ERROR: ORA-01732: data manipulation operation not legal on this view
UPDATE (
SELECT d.NAME, ch.NAME FROM (
SELECT 'test1' AS NAME, '2722' AS ID
FROM DUAL
) d
LEFT JOIN TABLEA a
ON UPPER(a.name) = UPPER(d.name)
)
SET a.name = 'test2'
WHERE a.name is null and a.id = d.id
I have tried merge, but just gave up thinking it's not possible. I've also considered not exists (but I'd have to be careful since I might accidentally update every other record that doesn't match a criteria)
It should be straightforward:
update personnel
set personnel_number = 'xyz'
where person_id = 1001
and not exists (select * from personnel where personnel_number = 'xyz');
If I understand correctly, you want to conditionally update a field, assuming the value is not found. The following query does this. It should work in both SQL Server and Oracle:
update table1
set name = 'Test1'
where (select count(*) from table1 where name = 'Test1') > 0 and
id = 1234