sql select update multiple column and check not null before update - sql

I have two tables.
Table1 structure NOT NULL.
Table2 some value NULL.
I want update table2 to table1 ,
I want when select value if null don't update and skip next column have value to update.
My Table
table1 value(table1) table2 value(table2)
t1ID 1234 t2ID 1234
t1Name Bear t2Name null
t1Adress 87/25 t2Adress 99/77
t1Tel 01254798535 t2Tel null
My Code
UPDATE table1
SET t1Name = (SELECT t2Name
FROM table2
WHERE t2Name IS NOT NULL
),
t1Adress = (SELECT t2Adress
FROM table2
WHERE t2Adress IS NOT NULL
),
t1Tel = (SELECT t2Tel
FROM table2
WHERE t2Tel IS NOT NULL
)
FROM table1,table2
WHERE t1ID = '1234' AND t2ID ='1234'
When I execute I get error:
SQL error : Msg 512, Level 16, State 1, Line 1 Subquery returned more
than 1 value. This is not permitted when the subquery follows =, !=,
<, <= , >, >= or when the subquery is used as an expression. The
statement has been terminated.
How can I fix it?

I think you intend:
UPDATE t1
SET t1Name = COALESCE(t2.t2Name, t1.t1Name),
t1Adress = COALESCE(t2.t2Adress, t1.t1Adress),
t1Tel = COALESCE(t2.t2Tel, t1.t2Tel)
FROM table1 t1 JOIN
table2 t2
ON t1.t1id = t2.t2id
WHERE t1.t1ID = 1234;
Note that I removed the single quotes on '1234'. Ids are usually numbers so they should be compared to numbers.
Your code fails because you are using subqueries instead of the values from the join. You seem to have multiple rows in table2, so you are getting the subquery returned more than one row error.

The following query replaces the null values with table1 values and updates the rest from table2 values
UPDATE T1 SET T1.t1Name = IsNull(T2.t2Name,T1.t1Name),
T1.t1Adress = IsNull(T2.t2Adress,T1.t1Adress),
T1.t1Tel = IsNull(T2.t2Tel,T1.t1Tel)
FROM table1 TI
INNER JOIN table2 T2 ON T1.t1ID = T2.t2ID
Try the following select query to check your result
SELECT T1.t1Name,IsNull(T2.t2Name,T1.t1Name),T1.t1Adress,IsNull(T2.t2Adress,T1.t1Adress),T1.t1Tel,IsNull(T2.t2Tel,T1.t1Tel)
FROM table1 TI
INNER JOIN table2 T2 ON T1.t1ID = T2.t2ID

Related

Update row in a table based on multiple rows in another table

I have two tables: table1 and table2:
table1 has columns id and integer
table2 has columns id and boolean
table2 can have multiple rows with the same id
I want to update the integer column of table1 by looking at all rows with the same id in table2 and seeing if any of the boolean values are true. If so I want table1.integer to be 1, else I want it to be 0.
I have tried something like this:
UPDATE table1,
(
SELECT table2.id, Sum(table2.boolean) > 0
) AS 'condition'
from table2
WHERE 1
GROUP BY table2.id) table3
SET table1.integer =IF(table3.condition, 1, 0) where table1.id = table3.id
And it seems to work, but I wanted to ask if there is a nicer/cleaner/more succinct way of updating the rows of table1 according to multiple rows of table2.
I would recommend EXISTS:
UPDATE table1 t1
SET t1.integer = (EXISTS (SELECT 1
FROM table2 t2
WHERE t2.id = t.id AND
t2.boolean
)
);
This can take advantage of an index on table2(id, boolean). With such an index, it should be faster than an approach that uses JOIN and AGGREGATION.
The syntax of your query is MySql like, so you can do a join like this:
UPDATE table1 t1 INNER JOIN (
SELECT id, MAX(boolean) maxboolean
FROM table2
GROUP BY id
) t2 ON t2.id = t1.id
SET t1.integer = t2.maxboolean
If there are ids in table1 without a corresponding id in table2 and you want the integer column for them to be updated to 0 then use a LEFT join:
UPDATE table1 t1 LEFT JOIN (
SELECT id, MAX(boolean) maxboolean
FROM table2
GROUP BY id
) t2 ON t2.id = t1.id
SET t1.integer = COALESCE(t2.maxboolean, 0)

specifying count in WHERE clause

select *
from table1 t1,
table2 t2,
table3 t3
where t2.parent_id = t1.row_id
and t2.xyz is not null
and (
select count(*)
from table3
where xyz = t2.row_id
) = 0;
Will it work?
I am using the alias t2 within my subquery.
My requirement is to check is to specify condition in where clause such that there is no record present in table3 where column xyz of table3 is stored as row_id of table2.
You can use NOT EXISTS to assert that there is no row returned from the subquery. Use modern explicit join syntax instead of comma based legacy syntax. No need to join table3 outside (you were making a cross join effectively).
select *
from table1 t1
join table2 t2 on t2.parent_id = t1.row_id
where t2.xyz is not null
and not exists (
select 1
from table3
where xyz = t2.row_id
);

t-sql update multiple rows using inner join

i trying to update a column in my table for several rows at the same time.
i want get the value to update from an another table which has a foreignkey.
Table Table1:
Id Primary
UserNumber INT
Table Table2
Id Primary
Id_T1 ForeignKey
UserId INT
OrderNumber INT
can someone help with this pls?
Note: In the Table T2 i need the UserId Value Where the ordernumber has the maximum value.
for example:
Id_T1 UserId OrderNumber
15 24 1
15 55 2
15 72 3
the value i want to receive is the 72.
i have try this:
update T1 set T1.UserNumber = T2.UserId
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.Id = T2.IdMain
WHERE T1.Id = T2.IdMain
AND T2.OrderNumber = (SELECT MAX(T2.OrderNumber) FROM Table2)
but i get this error:
Msg 147, Level 15, State 1, Line 6 An aggregate may not appear in the
WHERE clause unless it is in a subquery contained in a HAVING clause
or a select list, and the column being aggregated is an outer
reference.
You don't need to use alias name in inner query.
Try this:
UPDATE T1
SET T1.UserNumber = T2.UserId
FROM Table1 AS T1 INNER JOIN
Table2 AS T2 ON T1.Id = T2.IdMain
WHERE T1.Id = T2.IdMain
AND T2.OrderNumber = (SELECT MAX(OrderNumber) FROM Table2)

Update statement for multiple ids

I have 3 tables, i need to update 3rd table's column by calculating data from other two tables.
update table3 set column3=
(
select t2.column3+t1.column3
from table2 t2 with (nolock) join table1 t1
on table2.id=t1.id
where table2.id= 100
)
where id= 100;
This query works fine, it updates the 3rd table column, however if i supply IN operators like this:
update table3 set column3=
(
select t2.column3+t1.column3
from table2 t2 with (nolock) join table1 t1
on table2.id=t1.id
where table2.id IN (100,101)
)
where id IN (100,101);
this fails and i get this message
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
& i know this is because subquery is returning more than 1 row, how can i handle this scenario? Any hint/thought will be helpful.
How do i update for multiple ids? ie. the select query value returned by ID 100 should be updated against ID 100 in 3rd table & similarly for ID 101.
Also, I need to do a sum like this sum(t2.column3)- (t1.column3 + t1.column2)
update table3 set column3=
(
select sum(t2.column3)- (t1.column3 + t1.column2)
from table2 t2 with (nolock) join table1 t1
on table2.id=t1.id
where table2.id IN (100,101)
)
where id IN (100,101);
This is because you are trying to set column3 to a returned result, and SQL expects that to be one value only (scalar). The SQL engine gets confused when you pass it more than one return value (which one should it use?...it does not assume to iterate through the results). So, if you want to update an entire result set, then you need to create a subtable from you query and join on that. Your query should look more like this
UPDATE Table3
SET Column3 = subtable.value
FROM Table3
JOIN (
select t2.column3+t1.column3 as value, t1.id
from table2 t2 with (nolock) join table1 t1
on table2.id=t1.id
where table2.id IN (100,101)
) AS subtable
ON subtable.id = Table3.id
WHERE table3.id IN (100, 101)
Under this assumption that table3.id matches the other id's, you also really do not need the inner where table2.id IN ...
You should also join table3 in your UPDATE. Try this:
UPDATE t3
SET column3 = t2.column3+t1.column3
FROM table3 t3
INNER JOIN table2 t2 WITH(NOLOCK)
ON t3.id = t2.id
INNER JOIN table1 t1
ON t3.id=t1.id
WHERE t3.id IN (100,101)

SQL Query: select all the records from table1 which exist in table2 + all the records from table2 which don't exist in table1

consider the following example.
I have to select all the records from table1 which exist in table2 also + all the records from table2 which don't exist in table1 but exist in table2 and have IsActive=1 and status not null.
I initially tried it with a join but how to do the later part where I have to select the records which don't exist in table 1 ? I have to do it inside a single query presumably with a SQL view.
Edit
I need to combine the results like a UNION of 2 tables, so incase of rows absent in table1 but present in table2, the columns of belonging to table1 would be blank.
Here's an example query:
select *
from Table2 t2
left join
Table1 t1
on t1.id = t2.id
where t1.id is not null
or (isActive = 1 and status is not null)
The first line of the where clause takes care of "all the records from table1 which exist in table2". The second line is for "don't exist in table1 but exist in table2 and have IsActive=1 and status not null".
You will need an outer join here.
http://msdn.microsoft.com/en-us/library/ms187518.aspx
Is this it? Not sure if I got right what you want to do.
SELECT
*
FROM
Table1 t1
JOIN
Table2 t2 ON (t1.ID = t2.ID OR (t1.ID IS NULL AND t2.isActive = 1 AND t2.Status IS NOT NULL))