+------------------+
| id1 | id2 | bool |
+------------------+
| 1 | 1 | F |
| 1 | 2 | F |
| 2 | 1 | F |
+------------------+
UPDATE table_name
SET bool = T
WHERE (id1, id2) IN ((1,1),(2,1)) --Need work here
So basically I want to select where the conditions of (id1, id2) = (value1, value2). Similar to the statement below:
WHERE id1 = value1 AND id2 = value2
however in set of values in an array. Is this possible?
Thanks in advance
EDIT: I'm using SQL server 2008. I'm sorry if it wasn't too clear. I'm trying to put this as a stored procedure and call it from a service. The input would be some sort of an array (variable size), and find a match with the two IDs in a row.
Notice the WHERE clause in the UPDATE statement. The WHERE clause specifies which record(s) that should be updated. If you omit the WHERE clause, all records in the table will be updated!
The UPDATE command in SQL is used to modify or change the existing records in a table. If we want to update a particular value, we use the WHERE clause along with the UPDATE clause. If you do not use the WHERE clause, all the rows will be affected.
To do a conditional update depending on whether the current value of a column matches the condition, you can add a WHERE clause which specifies this. The database will first find rows which match the WHERE clause and then only perform updates on those rows.
Here is the way to do it in MSSql. All you need is to make one value (in this example VARCHAR) from Id1 and Id2. In this case you can use IN statement with the values set. Also you should think about NULLs in id1 and id2 if they are allowed in these fields (just add: and id1 is not null and id2 is not null
).
UPDATE table_name
SET bool = T
WHERE convert(varchar(20),id1)+','+convert(varchar(20),id2) in ('1,1','2,1')
One idea to achieve this is make use of temp table
Create Table #Temp
(
id1 int,
id2 int
)
insert into #Temp values(1,1)
insert into #Temp values(1,2)
insert into #Temp values(2,1)
insert into #Temp values(2,2)
--update data
UPDATE
table_name
SET bool = T
from table_name T1
inner join #Temp T2
on T1.Id1= T2.Id1
and T1.Id2= T2.Id2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With