简体   繁体   中英

Error in SQL Server when comparing two columns

I have a query here that selects the greatest value from two columns with dates and I get an error:

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

What can I rewrite the query?

update Product
set date = (select 
                date1, date2,
                case when exists (date1 > date2 
                        then date1
                        else date2
                end as date))
            from Product))
UPDATE Product
    SET date=(SELECT 
           CASE WHEN date1 > date2 THEN date1
                ELSE date2
           END AS date
           FROM Product
            )

It is no need to use other Product table in UPDATE if it is on the same row

UPDATE Product SET
    date = CASE WHEN date1 > date2 THEN date1 ELSE date2 END

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