简体   繁体   中英

Can I use 'in' to update field instead of '=' in SQL oracle?

I'm working on a project where I have to update multiple records in the same column. My values are stored in the list same as ID's that I have to use in my WHERE clause. I was wondering if I can use 'IN' instead of '=' and update all values from my list? Here is my code for lists:

<cfset listUserNum = ArrayToList(userNum)>
<cfset listUserCode = ArrayToList(userCode)>

They look like this:

listUserNum = "72,15,71,27,16,14,22";
listUserCode = "B,B,C,T,R,M,Y";

Here is my update statement:

<cfquery name="UpdateUsers" datasource="test">
    Update Users
    Set UserCode in <cfqueryparam value="#listUserCode#" cfsqltype="cf_sql_varchar" list="yes" /> 
    Where UserNumber in <cfqueryparam value="#listUserNum#" cfsqltype="cf_sql_integer" list="yes" /> 
</cfquery>

I used 'IN' for my WHERE clause in the past but I never used in my SET. Let me know if I'm doing something wrong here or if there is better way to do this. I still haven't tried this because I have to update around 20k records and I want to make sure this is the best way before I run my update. Thank you!

I would create a table to house that mapping and join to it in your UPDATE :

Update u
Set UserCode = l.UserCode
FROM Users u
INNER JOIN Lookup l
  ON u.UserNumber = l.UserNumber
Where UserNumber in <cfqueryparam value="#listUserNum#" cfsqltype="cf_sql_integer" list="yes" /> 

If a temporary table is no option, I would just run 20K updates instead of trying to compress it into one statement. Any decent database should have no problem processing that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM