一尘不染

在 CASE 语句中赋值 TSQL 给出错误

sql

我是 TSQL 的新手,试图将 MySQL 迁移到 TSQL,但出现错误:

SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near '='.

带绑定参数的 SQL 查询:

SELECT TOP(1)
         content.content_id       AS content_id ,
         content.content_type     AS content_type ,
         content.content_name     AS content_name ,
         content.content_version  AS content_version ,
         content.effective_date   AS effective_date      
FROM     test_content content
WHERE    content.vendor_id = :vendor_id
AND      content.content_type = :content_type
AND
   CASE
       WHEN :content_type2 <> 'T' 
       THEN content.content_name = :content_name
       ELSE 1=1
   END
ORDER BY content_version DESC  

查询语句:

SELECT TOP(1)
         content.content_id       AS content_id ,
         content.content_type     AS content_type ,
         content.content_name     AS content_name ,
         content.content_version  AS content_version ,
         content.effective_date   AS effective_date      
FROM     test_content content
WHERE    content.vendor_id = 127
AND      content.content_type = 'E'
AND
   CASE
       WHEN 'E' <> 'T' 
       THEN content.content_name = 'ABC'
       ELSE 1=1
   END
ORDER BY content_version DESC

THENcontent.content_name = ‘ABC’似乎有问题。有没有办法在 CASE 表达式中分配动态数据?

任何帮助将被appriciated。


阅读 123

收藏
2021-05-30

共1个答案

一尘不染

这是你期望的吗?

    SELECT TOP(1)
     content.content_id       AS content_id ,
     content.content_type     AS content_type ,
     content.content_name     AS content_name ,
     content.content_version  AS content_version ,
     content.effective_date   AS effective_date      
   FROM     test_content content
   WHERE    content.vendor_id = :vendor_id
   AND      content.content_type = :content_type
   AND      content.content_name = 
               CASE
                WHEN :content_type2 <> 'T' 
                THEN :content_name
                ELSE content.content_name
               END
   ORDER BY content_version DESC 
2021-05-30