check table column name as string in where clause SQL Server 2008 - sql

I have some mapped column values stored in a table. I want an update query like ...
update table1 set table1.value = table2.columnValue
from table2
where table2's columnName = table1.ColumnNameValue

Do a JOIN on both tables like
update t1
set [value] = t2.columnValue
from table1 t1
join table2 t2 on t2.columnName = t1.ColumnName

Related

SQL how to insert from another table based on same row values?

I have two tables with similar column values for columns "name" and "regis". One table is already filled with values, the other has only values for "name". How do i get "regis" values from table1 into table2 based on same column values?
I tried something like this
INSERT INTO table2 (regis)
SELECT table1 (regis)
WHERE table2.name = table1.name
Please try:
update table2 set regis = (select regis from table1 where table1.name=table2.name);
see the whole sample on:
https://www.db-fiddle.com/f/3jC8PGeZZEuty3XVq8gdzz/9
I think, what you are after is insert when name not found in table2 or update if its found in both tables.
INSERT INTO TABLE2 (regis)
SELECT t1.REGIS FROM TABLE1 as t1
LEFT OUTER JOIN TABLE2 as t2 on t2.name = t1.name
WHERE t2.name is null
You can update table2 using the statement like below:
update t2
set t2.regis = t1.regis
from table2 as t2
inner join table1 as t1 on t1.name = t2.name
If you are using SQL Server, then you can use merge statement to either insert or update in one step.
You need to use table2 as join in your select statement.
INSERT INTO table2 (regis)
SELECT table1.regis FROM Table1 INNER JOIN Table2 on table1.name = table2.name

update table from another table in access

I have two tables in access
table1 (name,nat) 16000 records
table2 (v_name,id) 189000 records
I want to update nat in table1 with id from table2. tried the following
UPDATE table1 SET nat = (SELECT table2.ID FROM table2 INNER JOIN table1 ON table2.V_name = table1.Name);
got the following
error: Operation must use an updateable query
You can try to use UPDATE .... JOIN in access
UPDATE table1 t1
INNER JOIN table2 t2
ON t2.V_name = t1.Name
SET t1.nat = t2.ID

Update table column based on another table based on ID

I have 2 tables 'table1' and 'table2'.
table1 has 10,000 records and table2 has 5,000 records.
Both tables have "RECORD_ID" column. All RECORD_ID's that are in table2 can be found in table1.
I want to UPDATE the "PRICE" column of table1 based on the "RECORD_ID" column of table2.
update table1 set PRICE = table2.PRICE where RECORD_ID = table2.RECORD_ID
I got this error message:
SQL0206N "table2.PRICE" is not valid in the context where it is used
SQLSTATE=42703
I am using DB2.
UPDATE table1 SET table1.price = (SELECT table2.price FROM table2 WHERE table2.record_id = table1.record_id)
Try this:
UPDATE table1 f1
SET f1.price =(
SELECT f2.price
FROM table2 f2
WHERE f2.record_id = f1.record_id
)
WHERE exists
(
SELECT f2.price
FROM table2 f2
WHERE f2.record_id = f1.record_id
)
You have to use a join like this:
UPDATE
Table1
SET
Table1.Column = T2.Column
FROM
Table1 T1
INNER JOIN
Table2 T2
ON
T1.PK = T2.FK;

UPDATE with isNull

In Microsoft SQL Server,
if the inner select doesn't have a matching criteria, then I need to update Field1 with blank instead of null.
UPDATE Table1
SET Field1=(
SELECT Field2
FROM Table2
WHERE Table2.Field3 = Table1.Field4
)
Please try like this -
UPDATE a
SET a.Field1 = ISNULL(b.Field2,'')
FROM Table1 a
LEFT JOIN Table2 b ON b.Field3 = a.Field4
You can reference another table using SQL Server's UPDATE ... FROM ... syntax:
UPDATE t1
SET field1 = COALESCE(t2.field2, '')
FROM Table1 t1
LEFT JOIN
Table2 t2
ON t2.Field3 = t1.Field4
The COALESCE() function returns the first non-null expression in a list. You can keep most of your original query, by using COALESCE() to replace any NULL from that query with an empty string.
UPDATE Table1
SET Field1=COALESCE((
SELECT Field2
FROM Table2
WHERE Table2.Field3 = Table1.Field4
), '')
Try this
UPDATE T1
SET T1.Field1=isnull(T2.Field2,'')
from Table1 T1 left join Table2 T2
ON T2.Field3 = T1.Field4

Sql server update multiple columns from another table

I have read lots of post about how to update multiple columns but still can't find right answer.
I have one table and I would like update this table from another table.
Update table1
set (a,b,c,d,e,f,g,h,i,j,k)=(t2.a,t2.b,t2.c,t2.d,t2.e,t2.f,t2.g,t2.h,t2.i,t2.j,t2.k)
from
(
SELECT ..... with join ... where ....
) t2
where table1.id=table2.id
If I running only select statement (between brackets) then script return values but not working with update
TSQL does not support row-value constructor. Use this instead:
UPDATE table1
SET a = t2.a,
b = t2.b,
(...)
FROM
(
SELECT ..... with join ... WHERE ....
) t2
WHERE table1.id = table2.id
You don't need to use a sub-query you can also simply do the following....
Update t1
set t1.a = t2.a
,t1.b = t2.b
,t1.c = t2.c
,t1.d = t2.d
.......
from table1 t1
JOIN table2 t2 ON t1.id = t2.id
WHERE .......
The above solution will work only for MSSQL.
In case of MySql you just need to first declare the tables
UPDATE
table1 t1 ,table2 t2
set
t1.field=t2.field
where
t1.id=t2.id;
In my case this worked..!!
The UPDATE SET commands implicitly apply on the table specified by , and it is not possible to specify the table on the SET operation.
Edit: Specify only the column name you want to update, do not mention the table.