Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CASE expression syntax error SQL

I have researched everywhere but still can't seem to fix a simple error: Running Microsoft SQL server:

UPDATE copyprogmaster
       SET active =
                 CASE
                   WHEN active = 1 THEN active = 0
                   WHEN active = 0 THEN active = 1
                  ELSE active
                 END
WHERE source = 'Mass_Mail'

my error is :

Line 4: Incorrect syntax near '='.

like image 566
JDV590 Avatar asked Dec 06 '22 21:12

JDV590


2 Answers

Remove the = after the THEN, so:

  UPDATE copyprogmaster
       SET active =
                 CASE
                   WHEN active = 1 THEN 0
                   WHEN active = 0 THEN 1
                  ELSE active
                 END
  WHERE source = 'Mass_Mail'

You already have active = after the SET on the second line.

like image 143
JNK Avatar answered Dec 20 '22 19:12

JNK


You do not need to repeat "active =" after THEN

UPDATE copyprogmaster
       SET active =
                 CASE
                   WHEN active = 1 THEN 0
                   WHEN active = 0 THEN 1
                  ELSE active
                 END
WHERE source = 'Mass_Mail'

Here's an example from the documentation at http://msdn.microsoft.com/en-us/library/ms181765.aspx

USE AdventureWorks2008R2;
GO
SELECT   ProductNumber, Category =
      CASE ProductLine
         WHEN 'R' THEN 'Road'
         WHEN 'M' THEN 'Mountain'
         WHEN 'T' THEN 'Touring'
         WHEN 'S' THEN 'Other sale items'
         ELSE 'Not for sale'
      END,
   Name
FROM Production.Product
ORDER BY ProductNumber;
GO
like image 35
tofutim Avatar answered Dec 20 '22 19:12

tofutim