update table - multi-part identifier could not be bound - sql

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

Related

UPDATE with join referencing same table - Redshift SQL

Assume I have a table named tab. Tab has a field called "version". Version is sequential.
In Redshift, I would like to update rows in Tab with the value from a previous row. Please tell me how to do this. Essentially:
UPDATE tab
SET tab.fieldA = tabPrior.fieldA
FROM tab tabPrior
WHERE tab.version = tabPrior.version + 1;
I would love to use a JOIN, but this does not seem to work in Redshift.
To Update with a JOIN statement you are better off writing it in a subquery.
UPDATE table
SET
col1 = TT.col1
col2 = TT.col2
FROM (SELECT T1.id, T1.col1, T2.col2
FROM T1
JOIN T2 ON T1. id ON T2. id) AS TT
WHERE table.id = TT.id
Also, I noticed that your syntax is the SET is not correct:
SET fieldA = tabPrior.fieldA
Redshift does not allow for you to pass the table when selecting the column since it goes under the assumption that you are updating 1 table at a time.
SET column = One or more columns that you want to modify. Columns that
aren't listed retain their current values. Do not include the table
name in the specification of a target column. For example, UPDATE tab
SET tab.col = 1 is invalid.
Link: https://docs.aws.amazon.com/redshift/latest/dg/r_UPDATE.html

Combine update statement and select statement

I'm using Oracle SQL Developer and I'm trying combine an update and a select statment into one. I know that Oracle dosen't support FROM or JOINS directly in the update statement and I therefor put the select in a subquery but it still don't work.
I have got two tables; MASTERTABLE and TESTTABLE.
MASTERTABLE contain an ID_NUMBER column and a TESTTABLE_ID column.
TESTTABLE contains a TESTTABLE_ID column and a TEST_COLUMN column.
What I want to do is to update the TEST_COLUMN value while only knowing the ID_NUMBER.
What my statement looks like:
UPDATE TESTTABLE
SET TEST_COLUMN= 'Testvalue'
WHERE TESTTABLE.TESTTABLE_ID IN (SELECT MASTERTABLE.TESTTABLE_ID
FROM MASTERTABLE
WHERE ID_NUMBER=11);
But I get stuck in some kind of loop. Where did I go wrong?
I have faced the same problem. The solution is usage of MERGE operation instead of UPDATE.
MERGE INTO TESTTABLE t
USING
(
SELECT m.ID_NUMBER num,
m.TESTTABLE_ID id
FROM MASTERTABLE m
) newnum ON (t.TESTTABLE_ID = newnum.id)
WHEN MATCHED THEN UPDATE
SET t.TEST_COLUMN = newnum.num;
You can try any one of this in Oracle
Normal Update
UPDATE
TESTTABLE
SET
TEST_COLUMN= 'Testvalue'
WHERE
EXISTS
(SELECT MASTERTABLE.TESTTABLE_ID
FROM MASTERTABLE
WHERE ID_NUMBER=11);
Using Inline View (If it is considered updateable by Oracle)
Note: If you face a non key preserved row error add an index to resolve the same to make it update-able
UPDATE
(SELECT
TESTTABLE.TEST_COLUMN AS OLD,
'Testvalue' AS NEW
FROM
TESTTABLE
INNER JOIN
MASTERTABLE
ON TESTTABLE.TESTTABLE_ID = MASTERTABLE.TESTTABLE_ID
WHERE ID_NUMBER=11) T
SET
T.OLD = T.NEW;
Using Merge
MERGE INTO
TESTTABLE
USING
(SELECT
T1.ROWID AS RID,
T2.TESTTABLE_ID
FROM
TESTTABLE T1
INNER JOIN
MASTERTABLE T2
ON TESTTABLE.TESTTABLE_ID = MASTERTABLE.TESTTABLE_ID
WHERE ID_NUMBER=11)
ON
( ROWID = RID )
WHEN MATCHED
THEN
UPDATE SET TEST_COLUMN= 'Testvalue';
The problem was as Tom H wrote a blocking problem. When I started working on the project today all of the solutions worked.

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.

SQL update query issue

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

TSQL Multi-Part Identifier Could Not Be Found

We are attempting to update the LastUpdateDateTime column to the value of the Stamp column in another database, on another server via this query:
update [NEWTON-DB].NEWTON.dbo.vital_signs
set lastupdatedatetime =
coalesce ((select top 1 stamp
from [SERVER2].NEWTON.dbo.vital_sign
where rowguid in (select oldrowguid
from [NEWTON-DB].NEWTON.dbo.import_log
where tablename = 'vital_sign'
and newid = [NEWTON-DB].NEWTON.dbo.vital_signs.id)),
coalesce(lastupdatedatetime, getutcdate()))
The import_log table is simply the inner join to fetch the new id based off the old rowguid if that makes any sense.
When they were on the same server this query worked fine, but after migrating I get the following error:
The multi-part identifier "NEWTON-DB.NEWTON.dbo.vital_signs.id" could not be bound.
Is there something blindingly obvious we're missing. Thanks so much in advance!
5 Part identifers like this [NEWTON-DB].NEWTON.dbo.vital_signs.id don't work
You need to alias the table and then use the alias
e.g.
Select top 1 stamp
from [SERVER2].NEWTON.dbo.vital_sign t
where rowguid in (select oldrowguid
from [NEWTON-DB].NEWTON.dbo.import_log
where tablename = 'vital_sign'
and newid = t.id)