简体   繁体   中英

T-SQL update with restrictions in WHERE clause

SOLVED IT

This will do it:

UPDATE myTable
  SET
    URL = 
       ( CASE 
            WHEN (URL <> 'new domain' THEN 'new domain'
            ELSE URL
         END
       )
  WHERE
    NAME = 'any name'

Good Morning,

I have an UPDATE query with a little problem. The query shall update the existing URL column. BUT ... the WHERE clause has two restrictions build up on each other.

First and main restriction is the NAME column. This column is unique. Now I want to check if that URL in the database is equal to the new one. And only if there is a difference I want to replace it.

UPDATE myTable
  SET
    URL = 'new domain'
  WHERE
    NAME = 'any name' AND
    URL <> 'new domain'

This UPDATE query will look for the name but also for every domain that doesn't match this pattern. Not what I want.

If you want to update only if there is really a change, the value for URL in the SET-clause should be the same as in the WHERE-clause. In your example they are different.

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