2012-08-07 72 views
-1

如何将此T-SQL查询转换为Oracle?选择case语句时出错

if((select case 
     when (select top 1 AlertMessage 
      from ims.dbo.alertlist 
      where [email protected] 
      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 
+3

你有什么试过?当你把它放到Oracle中时你有错误吗?什么是错误? – 2012-08-07 14:02:58

+1

另外,您正在使用哪个版本的Oracle? – Diego 2012-08-07 14:20:13

+0

我使用oracle 11。 – 2012-08-07 14:29:57

回答

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的非常好,但我从来没有使用甲骨文,所以我的解决方案可能不完美(甚至根本运行,因为我没有的环境中进行测试)。这也意味着您可以像我一样,通过简单的搜索找到您可能遇到的其他问题的答案。 :)