Sql server select case语句出错

Sql server select case语句出错,sql-server,stored-procedures,oracle11g,Sql Server,Stored Procedures,Oracle11g,如何将此T-SQL查询转换为Oracle if((select case when (select top 1 AlertMessage from ims.dbo.alertlist where SystemID=@meter order by TimePeriod desc ) like '%NO_COMMUNICATION_Resolved' then 1

如何将此T-SQL查询转换为Oracle

if((select case 
       when (select  top 1 AlertMessage   
             from ims.dbo.alertlist 
             where SystemID=@meter 
             order by TimePeriod desc
            ) like '%NO_COMMUNICATION_Resolved' then 1 
       else 0 end)=1)
begin
     INSERT INTO [ims].[dbo].[alertlist]
          ([SiteID],[ThresholdNumber],[SystemID],
           [AlertMessage],[TimePeriod],[AlertType],[PollID]) 
     VALUES 
          (1,@thresnumber,@meter,@message,getdate(),1,0)
end

正如Dan Puzey所指出的,你的问题过于宽泛,需要大量的尝试和错误。不过,我会尝试用一些可能解决您问题的第一部分的方法让您走上正确的轨道

DECLARE v_IsMessageResolved int;

-- This query will retrieve the value of the first row returned by the sub-query, mimicking the TOP 1 of SQL Server, and it will store the value of MessageResolved in a Variable
SELECT
  MessageResolved into v_IsMessageResolved
FROM
  (
  -- This query will return 1 for all the Messages that match the LIKE clause, ordered by TimePeriod in descending order.
  SELECT
    CASE
      WHEN AlertMessage LIKE '%NO_COMMUNICATION_Resolved' THEN 1
      ELSE 0
    END AS MessageResolved
    ,RANK() OVER (ORDER BY TimePeriod DESC) AS MessageRank
  FROM
    ims.alertlist 
  WHERE
    (SystemID = :meter)
  )
WHERE
  (MessageRank = 1)

-- At this point, it will be a matter of checking the value of v_IsMessageResolved and, if it's "1", run the INSERT

请注意,我非常了解SQL Server,但我从未使用过Oracle,因此我的解决方案可能并不完美,甚至根本无法运行,因为我没有测试它的环境。这也意味着您可以像我一样,通过简单的搜索找到剩余问题的答案

你试过什么?当您将其放入Oracle时,是否会出现错误?错误是什么?还有,您使用的是哪一版本的Oracle?我试着这样做:选择case when select AlertMessage from ims.alertlist,其中SystemID=v_meter,rownum=1 order by TimePeriod desc,如“%NO_COMMUNICATION_Resolved”,然后从dual将1其他0结束为v_count;如果v_count=1,则将其插入ims.alertlistSiteID、ThresholdNumber、SystemID、AlertMessage、TimePeriod、AlertType、PollID值1、v_thresnumber、v_meter、v_message、getdate、1,0 end if;我得到了缺少的表达式错误。我建议将测试结果放在原始问题的主体中,这样会更具可读性。