Update table from another table SQL - sql

Ive hit a wall with what seems a simple procedure.
Update a table from another table, heres where i am:
DECLARE #oldcode varchar(50)
DECLARE #newcode varchar(50)
SET #oldcode = table1.OLDCODE
SET #newcode = table1.NEWCODE
UPDATE table2 SET [CODE] = #NEWCODE WHERE [CODE] = #OLDCODE
this results in:
The multi-part identifier "table1.OLDCODE" could not be bound.
cheers :-
the data:
Table1
record OLDCODE NEWCODE
1 ZZZALF38 ALF38
2 ZZZALF38.1 ALF38.1
3 ZZZALF38.2 ALF38.2
table2
record CODE
1 ZZZALF38
2 ZZZALF38.1
3 ZZZALF38.2
wish to change table 2 to:
record CODE
1 ALF38
2 ALF38.1
3 ALF38.2

I am guessing you are using SQL Server and this is what you want:
update t2
set code = t1.newcode
from table2 t2 join
table1 t1
on t2.code = t1.oldcode;

update table2 set code=table1.newcode from table1 on table2.code=table1.code

Related

How to UPDATE a RowCount using multiple tables and INNER JOIN in SQL Server 2014

I don't know if this can be done is a single statement, but I thought I'd ask.
There are 3 tables involved in a SQL Server 2014 stored procedure:
CREATE TABLE #TempTable
(
EntityID int,
PrefType smallint,
Col1 int,
Col2 int
)
Table1
(
EntityID int,
PrefType smallint
)
Table2
(
EntityID int,
MyRowCount int
)
The #TempTable is used to process other stuff, and now contains just a few rows. I want to use those rows to UPDATE Table2.MyRowCount for the corresponding EntityID using a COUNT(*) from the Table1 WHERE #TempTable.EntityID = Table1.EntityID AND #TempTable.PrefType = Table1.PrefType.
I tried this:
UPDATE Table2 T2
INNER JOIN (SELECT T1.EntityID,
COUNT(*) T1RowCount
FROM Table1 T1, #TempTable2 TT
WHERE T1.EntityID = TT.EntityID
AND T1.PrefType = TT.PrefType
GROUP BY T1.EntityID) AS T1Temp ON T1Temp.EntityID = T2.EntityID
SET T2.MyRowCount = T1Temp.T1RowCount
But I get a syntax error (squiggly line under T2 in "UPDATE Table2 T2" above) telling me that:
Incorrect Syntax near 'T2'. Expecting SET.
And another squiggly line under INNER and AS with 'Incorrect Syntax' also.
Could this be a problem with using the older version of SQL Server (2014)? or is my SQL wrong/not possible?
As #DaleK already commented - have a look at the official documentation when in doubt! That should be your #1 stop - always.
Reading the docs, you'd see you have to use this SQL statement:
-- UPDATE, then SET
UPDATE Table2
SET MyRowCount = T1Temp.T1RowCount
-- only then FROM and INNER JOIN
FROM Table2 T2
INNER JOIN (SELECT T1.EntityID,
COUNT(*) T1RowCount
FROM Table1 T1, #TempTable2 TT
WHERE T1.EntityID = TT.EntityID
AND T1.PrefType = TT.PrefType
GROUP BY T1.EntityID) AS T1Temp ON T1Temp.EntityID = T2.EntityID

Update table field from a different table with if statement

I have two tables as;
Table 1
[SKU], [QTY_on_hand]
Table 2
[XREF] , [QTY_on_hand]
I want to update Table 2 's [QTY_on_hand] with Table 1 's [QTY_on_hand]
where [SKU]=[XREF] and if Table1's [QTY_on_hand] is greater than "30" else table2 's [QTY_on_hand] = '0'.
So the T-SQL is:
update Table 2
set [QTY_on_hand] = t.[QTY_on_hand]
from Table 1 t
where t.[SKU] = [XREF]
But the if statement is missing here and I couldn't manage to how to integrate if statement with 'case when' or 'if-else'.
Follows is how to join a second table to the table being updated in an update statement and use a conditional expression for updating a value:
update T2 set
qty_on_hand = case when T1.qty_on_hand > 30 then T1.qty_on_hand else 0 end
from Table2 T2
inner join Table1 T1 on T1.SKU = T2.XREF
where ?? -- surely you don't want to update the entire table?
Have you tried a case expression like this
UPDATE Table2
SET Qty_on_Hand = CASE WHEN SKU=XREF AND Qty_on_Hand > 30 THEN Table1.Qty_on_Hand ELSE Table2.Qty_on_Hand END

How to update multiple tables with single query

I have 2 tables that I need to update:
Table A consists of: ID, personName, Date, status
Table B consist of: PersonID, Date, status
For every row in A there can be multiple rows in B with the same personID
I need to "loop" over all results from A that the status=2 and update the date and status to 1.
Also, for every row in A that status=2 I need to update all the rows in B that has the same personID (i.e, A.ID==B.PersonID) – I need to update date and status to 1 as well.
So basically, if I was to do this programmatically (or algorithmically) its's something like that:
Foreach(var itemA in A)
If (itemA.status = 2)
itemA.status to 1
itemA.date = GetDate()
foreach(var itemB in B)
if(itemB.PersonID == itemA.ID && itemB.status != 2 )
Change itemB.status to 1
Change itemB.date = GetDate()
i know how to update all the rows in B using the following sql statement:
UPDATE
B
SET
status = 1,
date = GETDATE()
FROM
B
INNER JOIN
A
ON
B.PersonID = A.ID
the problem is that i don't know how to also update table A since there can't be multiple tables in an update statement
thanks for any help
Here is an example using the output clause:
declare #ids table (id int);
update table1
set status = 1
output inserted.id into #ids
where status = 2;
update table2
set status = 1,
date = getdate()
where personid in (select id from #ids);
Put everything inside a transaction and commit if succeeds
DECLARE #err int
BEGIN TRANSACTION
UPDATE B
SET status = 1, date = GETDATE()
FROM B INNER JOIN A ON B.PersonID = A.ID
WHERE A.status = 2
SET #err = ##ERROR
IF #err = 0
BEGIN
UPDATE A
SET status = 1,
date = GETDATE()
WHERE status = 2
SET #err = ##ERROR
END
IF #err = 0
COMMIT
ELSE ROLLBACK
Question has been asked before:
How to update two tables in one statement in SQL Server 2005?
it is not possible to update multiple tables at once.
Summary answer from that question:
You can't update multiple tables in one statement, however, you can use a transaction to make sure that two UPDATE statements are treated atomically. You can also batch them to avoid a round trip.
BEGIN TRANSACTION;
UPDATE Table1
SET Table1.LastName = 'DR. XXXXXX'
FROM Table1 T1, Table2 T2
WHERE T1.id = T2.id
and T1.id = '011008';
UPDATE Table2
SET Table2.WAprrs = 'start,stop'
FROM Table1 T1, Table2 T2
WHERE T1.id = T2.id
and T1.id = '011008';
COMMIT;
For your question something like this would work:
BEGIN TRANSACTION;
UPDATE B
SET status = 1
, date = GETDATE()
WHERE B.PersonId IN ( SELECT ID
FROM A
WHERE A.status = 2
);
UPDATE A
SET status = 1
, date = GETDATE()
WHERE A.status = 2;
COMMIT;

SQL update a table

TABLE 1
id name uf ibge
1 GOIANIA 'GO' null
2 BRASILIA 'DF' null
3 TOCANTINS 'TO' null
TABLE 2
id name uf ibge
1 GOI**Â**NIA 'GO' 5208707
2 BRAS**Í**LIA 'DF' 5300108
3 TOCANTINOPOLIS 'TO' 1721208
I need to update in table 1, the field ibge using some kind of like in the where clause to relate the description of the city with ibge code.
Could anyone help? Very grateful!
Try this...
UPDATE T1
SET T1.ibge = T2.ibge
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.id = T2.id
Assuming you can join both tables on the uf field, like so:
UPDATE Table1
SET ibge = Table2.ibge
FROM
Table2
WHERE
Table1.uf = Table2.uf;
Fiddle here
UPDATE table1 tab1 SET ibge = (SELECT ibge FROM table2 tab2 WHERE tab1.name like (substring(tab2.name, 1, 3) || '%') LIMIT 1);
Assuming that the first 3 characters are the same in field name for table 1 and table 2

update query for multiple rows with different where condition?

How to write update query in sql to update multiple rows with different where condition ?For example if i want to change the name of 100 rows of a particular table with different id's?
update table set name = 'value1' where id=1,
set name ='value2' where id=2;
But like this i can't write for 100 entries. . Any help?
You can create a temp table with these 100 entries then update the table by joining this new table, something like:
CREATE TABLE Temp(
Id int NOT NULL,
Name Varchar(50)
) ;
UPDATE YourTable t1
INNER JOIN Temp t2 ON t1.Id = t2.Id
SET t1.Name = t2.Name;