2012-08-07 73 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的非常好,但我從來沒有使用甲骨文,所以我的解決方案可能不完美(甚至根本運行,因爲我沒有的環境中進行測試)。這也意味着您可以像我一樣,通過簡單的搜索找到您可能遇到的其他問題的答案。 :)