I am new to SQL Server and this is my first post.
I am getting the message "incorrect syntax near '=' when using the Case Statement. Here is an example of my code:

  Select * , CASE 
    when a > b THEN b = a
    when c > d THEN d = c
    when e > f THEN f = e
    when g > h THEN h = g
    when i > j THEN j = i
    when k > l THEN l = k
    when m > n THEN n = m
    when o > p THEN p = o

END as value
INTO #temptable
From #atemptable

Thanks in advance for your help!

1 Answers

0
Razvan Socol On

It's not clear if you want the THEN part to be an assignment or a comparison. Either way, it's not possible, the THEN part can only be an expression. I'm assuming that you wanted an assignment. It looks that you don't want to update the #atemptable, you just want the new values in #temptable. In this case, you can use separate CASE expressions, like this:

SELECT  A, CASE WHEN A > B THEN A ELSE B END AS B,
        C, CASE WHEN C > D THEN C ELSE D END AS D
END AS value
INTO #temptable
FROM #atemptable