Вопрос

I have the following @temp table :

------------------------------------
|Condition_ID   Operator   Order    |
|     1            ==        1      |
|     2            <>        3      |
|     3            ==        2      |
------------------------------------

I want to implement a SELECT CASE that will output a certain message depending on the Operator

However this throws me an Incorrect syntax near the keyword 'PRINT'. error.

SELECT CASE
    WHEN Operator LIKE '=='
        THEN PRINT 'EQUALS'
    WHEN Operator LIKE '<>'
        THEN PRINT 'NOT EQUALS'
END AS Operator
FROM @temp

What am I doing wrong?

Это было полезно?

Решение

You just need to remove the PRINT

SELECT CASE
    WHEN Operator LIKE '=='
        THEN 'EQUALS'
    WHEN Operator LIKE '<>'
        THEN 'NOT EQUALS'
END AS Operator
FROM @temp

You can't use a PRINT inside a SELECT, and you don't need to - the SELECT will retrieve the appropriate value for you.

Другие советы

You cannot mix PRINT and SELECT

SELECT CASE WHEN Operator LIKE '=='
            THEN 'EQUALS'
            WHEN Operator LIKE '<>'
            THEN 'NOT EQUALS'
       END AS Operator
FROM @temp

this works ::

   SELECT CASE
    WHEN opr LIKE '=='
        THEN 'EQUALS'
    WHEN opr LIKE '<>'
        then 'NOT EQUALS'
    else
        'wrong i/p'
    end
as operator
FROM tbl1;

Try this ,there is no print option

SELECT CASE 
        WHEN Operator LIKE '=='
        THEN 'EQUALS'
        WHEN Operator LIKE '<>'
        THEN 'NOT EQUALS'
   END AS Operator
   FROM @temp
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top