-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
4

1 回答 1

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

于 2012-08-07T14:35:05.447 回答