select case语句出错

时间:2012-08-07 13:51:08

标签: 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 
       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

1 个答案:

答案 0 :(得分:0)

正如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,因此我的解决方案可能并不完美(甚至根本不运行,因为我没有环境来测试它)。这也意味着您可以像我一样通过简单的搜索找到您可能遇到的其余问题的答案。 :)