SQL update where in set of data

回眸只為那壹抹淺笑 提交于 2019-12-22 03:25:23

问题


+------------------+
| 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.


回答1:


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')



回答2:


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



回答3:


Try This - SQL Server 2008 Version.

create table mytable
(
id1 int,
id2 int,
bool char(1)
);

insert INTO mytable VALUES(1,1,'F');
insert INTO mytable VALUES(1,2,'F');
insert INTO mytable VALUES(2,1,'F');

SELECT * FROM mytable;

update mytable 
set bool='T'
WHERE exists (SELECT id1,id2 from mytable tb2
where mytable.id1 = 1 AND mytable.id2 = 1
or mytable.id1 = 2 AND mytable.id2 = 1);

SELECT * from mytable;



回答4:


This query works in oracle...

UPDATE table_name 
SET BOOL = T 
WHERE (id1, id2) IN (SELECT 1,1 FROM DUAL UNION SELECT 2,1  FROM DUAL);

Which is your database?




回答5:


What are you fundamentally trying to do? Why is this the way you have chosen to do it? It appears you are a bit fuzzy in understanding set-based logic. Each of the answers provided by other posters are valid and will work, but may not be the most suitable for your purpose. Is this processing against an existing data set? Is it part of a data load or insert process? Each of the ID fields you have listed has its own range of unique values. Based on your example it appears that what you are really wanting to do is update the bool value when ID2 = 1

UPDATE table_name SET Bool = 'T'
WHERE Id2 = 1

More likely you are wanting to develop a logic that sets the Bool value based on some data rule - for example if Id2 is less-than-or-equal-to Id1. A case statement works here:

UPDATE table_name SET Bool = CASE WHEN Id1 > Id2 THEN 'T' ELSE 'F' END

This is far more efficient when you are dealing with large sets of data than writing in AND/OR rules in your WHERE clause for each and every variation of values you insert.

Think of WHERE as a filter rather than as a place to implement if/then type logic.

In small data inserts (you either manually type values into the table fields, are inserting from some sort of web form transaction by transaction) it is probably simplest just to manually set the value as you see fit, or build that into the procedural part of your system and apply a validation rule to the table.

If you want to write a stored procedure that does this you create variables for the ID values and link these up to whatever system transmits the external information into your database system. (I will presume you have created the table structures already);

CREATE PROCEDURE table_name_insert
@Id1 Int NOT NULL,
@Id2 Int NOT NULL

-- If you want to execute the logic outside of the DB environment
-- (perhaps as part of an SSIS package) then you will want to add this
-- variable and pass it in explicitly.

, @Bool bit NOT NULL 


AS

DECLARE @sql nvarchar(4000)


SET @sql = '
INSERT INTO table_name (ID1, ID2, Bool)
SELECT @Id1, @Id2, [Case Logic or Variable Value for Bool]
' EXEC sp_executeSQL @sql

This process can be called by your program and you pass the variables you might generate from an array into it. There are also ways where you can import the values directly to the appropriate column and then execute the Bool logic in post-insert code. "Hard Coding" a WHERE statement to handle every case is inefficient, and a bad habit to get into.




回答6:


if you are using SQL Server try this

     UPDATE table_name
     SET bool = T
     WHERE (convert(varchar 20, id1) + '-' + convert(varchar 20 , id2)) 
      IN (convert(varchar 20 , value1) + '-' + convert(varchar 20 , value2),
        convert(varchar 20 , value3) + '-' + convert(varchar 20 , value4));

try this if you are using oracle

     UPDATE table_name
     SET bool = T
       WHERE (id1 || '-' || id2) IN (value1 || '-' || value2)

usually we use in clause if we have more that one value to match. assuming you (id1 = value1 and id2 =value2) and (id1 = value3 and id2 =value4)

     UPDATE table_name
     SET bool = T
     WHERE (id1 || '-' || id2) IN (value1 || '-' || value2, value3 || '-' || value4)



回答7:


You can send your list of values to the stored procedure as XML. Unpack the XML to table variable and use exists to find the rows that you should update.

create procedure YourSP
  @XMLParam xml
as

declare @T table(id1 int, id2 int)

insert into @T(id1, id2)
select T.N.value('id1[1]', 'int'),
       T.N.value('id2[1]', 'int')
from @XMLParam.nodes('/Row') as T(N)

update table_name
set bool = 'T'
where exists (select *
              from @T as T
              where T.id1 = table_name.id1 and
                    T.id2 = table_name.id2)

Call like this:

exec YourSP '<Row>
               <id1>1</id1>
               <id2>1</id2>
             </Row>
             <Row>
               <id1>2</id1>
               <id2>1</id2>
             </Row>'


来源:https://stackoverflow.com/questions/11625673/sql-update-where-in-set-of-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!