【问题标题】:incorrect syntax error near 'IN' keyword'IN' 关键字附近的语法错误
【发布时间】:2015-08-17 16:20:54
【问题描述】:

如何在 sql server 中使用这个 mysql 查询?

如果我在 sql server 中使用相同的语法,则会收到以下错误。

“IN”关键字附近的语法错误

select count(1) as total_record 
from sms_allocation 
left join sms_api_definition on sms_allocation.sms_api_definition_id = sms_api_definition.sms_api_definition_id 
where if(('1' = 1 || '4,26,28,31,32,33,37,41,53,56,58,62,63,66,71,72,73,75,76,77,81' = 81 || '4,26,28,31,32,33,37,41,53,56,58,62,63,66,71,72,73,75,76,77,81' = 82), 
      if('0'!='0',reseller_id in (
        select reseller_id 
        from sms_allocation 
        where reseller_id in (0)),reseller_id in (
          select reseller_id 
          from sms_allocation)
        ), reseller_id in (
          select reseller_id 
          from sms_allocation 
          where reseller_id in (0)) and sms_allocate_to='Company');

【问题讨论】:

  • 我正在将 mysql 代码迁移到 MS sql

标签: mysql sql-server


【解决方案1】:

只需将 mysql if 语句替换为标准 sql case 语句即可解决您的问题。

    select
        count(1) as total_record
    from
        sms_allocation left join sms_api_definition on sms_allocation.sms_api_definition_id = sms_api_definition.sms_api_definition_id
    where
        case when 
            '1' = 1 or '4,26,28,31,32,33,37,41,53,56,58,62,63,66,71,72,73,75,76,77,81' = 81 or '4,26,28,31,32,33,37,41,53,56,58,62,63,66,71,72,73,75,76,77,81' = 82
        then
            case when 
                '0' != '0'
            then
                reseller_id in (
                    select
                        reseller_id
                    from
                        sms_allocation
                    where
                        reseller_id in (0)
                 )
             else
                 reseller_id in (
                     select
                         reseller_id
                     from
                         sms_allocation
                 )
             end
       else    
           reseller_id in (
               select
                   reseller_id
               from
                   sms_allocation
               where
                   reseller_id in (0)
       end
       and 
          sms_allocate_to = 'Company'
);

更新:将|| 替换为or,抱歉我错过了。

【讨论】:

  • 我收到以下错误:关键字“in”附近的语法不正确。关键字“else”附近的语法不正确。关键字“end”附近的语法不正确。
  • 更新了答案,请试一试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-10
  • 2016-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-07
相关资源
最近更新 更多