2013-04-08 29 views
-1

這是使用Unidac組件的SQL Server工作的原代碼:該uniQuery的 在SQL(字符串列表)我有:轉換SQL絕對數據庫

use HIS 
    DECLARE @StartDate DATETIME, 
      @EndDate DATETIME, 
      @RoomType VARCHAR(6) 
    ; 

    SELECT @StartDate = '2011-04-15', 
      @EndDate = '2011-04-26', 
      @RoomType = 'DBLMS' 
    ; 
    WITH 
    cteStayDates AS 
    ( 
    SELECT RoomType = Room_Type, 
      StartDate = CASE WHEN Rate_Start_Date < @StartDate THEN @StartDate ELSE Rate_Start_Date END, 
      EndDate = CASE WHEN Rate_End_Date > @EndDate THEN @EndDate ELSE Rate_End_Date END, 
      Rate 
     FROM dbo.Room_Rates 
     WHERE @RoomType = Room_Type 
     AND @StartDate < Rate_End_Date 
     AND @EndDate >= Rate_Start_Date 
    ) 
    SELECT RoomType, StartDate, EndDate, Rate, 
      Days = DATEDIFF(dd,StartDate,EndDate) 
       + CASE WHEN EndDate = @EndDate THEN 0 ELSE 0 END 
     FROM cteStayDates 
     ORDER BY StartDate 
    ; 

在按鈕點擊我有:

procedure TForm1.Button1Click(Sender: TObject); 
//uniQuery1.SQL.Add('SET DATEFORMAT DMY'); 
begin 
uniQuery1.Close; 
uniQuery1.SQL.Clear; 
uniQuery1.SQL.Add('DECLARE @StartDate DATETIME,'); 
uniQuery1.SQL.Add('@EndDate DATETIME,'); 
uniQuery1.SQL.Add('@RoomType VARCHAR(6);'); 
uniQuery1.SQL.Add('SELECT @StartDate = :a2,'); 
uniQuery1.SQL.Add('@EndDate = :a3,'); 
uniQuery1.SQL.Add('@RoomType = :a1;'); 
uniQuery1.SQL.Add('WITH'); 
uniQuery1.SQL.Add('cteStayDates AS'); 
uniQuery1.SQL.Add('(SELECT RoomType = Room_Type,'); 
uniQuery1.SQL.Add('StartDate = CASE WHEN Rate_Start_Date < @StartDate THEN @StartDate ELSE Rate_Start_Date END,'); 
uniQuery1.SQL.Add('EndDate = CASE WHEN Rate_End_Date > @EndDate THEN @EndDate ELSE Rate_End_Date END,'); 
uniQuery1.SQL.Add('Rate'); 
uniQuery1.SQL.Add('FROM dbo.Room_Rates'); 
uniQuery1.SQL.Add('WHERE @RoomType = Room_Type'); 
uniQuery1.SQL.Add('AND @StartDate < Rate_End_Date'); 
uniQuery1.SQL.Add('AND @EndDate >= Rate_Start_Date)'); 
uniQuery1.SQL.Add('SELECT RoomType, StartDate, EndDate, Rate,'); 
uniQuery1.SQL.Add('Days = DATEDIFF(dd,StartDate,EndDate)'); 
//uniQuery1.SQL.Add('+ CASE WHEN EndDate = @EndDate THEN 0 ELSE 0 END'); 
uniQuery1.SQL.Add('FROM cteStayDates ORDER BY StartDate;'); 
uniQuery1.Params.ParamByName('a1').AsString := cxTextEdit1.Text; 
uniQuery1.Params.ParamByName('a3').AsDate := cxDateEdit2.Date; // end date 
uniQuery1.Params.ParamByName('a2').AsDate := cxDateEdit1.Date; // start date 
uniQuery1.Open; 


end; 

現在,我想這個端口的代碼絕對數據庫(表中的字段是相同的),但不能讓它開始工作。有人可以幫我弄這個嗎 ?

我也試圖把這個查詢在ABSQuery1的SQL文本:

SELECT RoomType, StartDate, EndDate, Rate, 
      Days = DATEDIFF(dd,StartDate,EndDate) 
       + CASE WHEN EndDate = EndDate THEN 0 ELSE 0 END 
    RoomType = Room_Type, 
      StartDate = CASE WHEN Rate_Start_Date < StartDate THEN StartDate ELSE Rate_Start_Date END, 
      EndDate = CASE WHEN Rate_End_Date > EndDate THEN EndDate ELSE Rate_End_Date END, 
      Rate 
     FROM Room_Rates 
     WHERE RoomType = Room_Type 
     AND StartDate < Rate_End_Date 
     AND EndDate >= Rate_Start_Date  ORDER BY StartDate 
    ; 

但因此它不會鐵鍋我不能檢索的字段。真的不知道爲什麼。連接設置正確。當我嘗試檢索我得到的字段時: enter image description here

+0

大概它會產生某種錯誤。那是什麼錯誤?一個猜測是它與CTE和/或DATEDIFF有問題。可以將CTE重寫爲子查詢,如果DATEIFF是問題,則必須檢查絕對數據庫的文檔以查看您可以改爲執行的操作。 – 2013-04-08 12:43:22

+0

絕對數據庫不支持'Common Table Expressions'('CTEs')。有關適用的語法,請參閱絕對文檔。 – 2013-04-08 12:47:01

+0

文檔是面向專業人士的,不適合初學者。 – user763539 2013-04-08 13:01:04

回答

0

公用表表達式(with cteStayDates as (..))是可能不是絕對的SQL Server功能。你可以嘗試這樣的:

use HIS 
    DECLARE @StartDate DATETIME, 
      @EndDate DATETIME, 
      @RoomType VARCHAR(6) 
    ; 

    SELECT @StartDate = '2011-04-15', 
      @EndDate = '2011-04-26', 
      @RoomType = 'DBLMS' 
    ; 
    SELECT RoomType, StartDate, EndDate, Rate, 
      Days = DATEDIFF(dd,StartDate,EndDate) 
       + CASE WHEN EndDate = @EndDate THEN 0 ELSE 0 END 
    RoomType = Room_Type, 
      StartDate = CASE WHEN Rate_Start_Date < @StartDate THEN @StartDate ELSE Rate_Start_Date END, 
      EndDate = CASE WHEN Rate_End_Date > @EndDate THEN @EndDate ELSE Rate_End_Date END, 
      Rate 
     FROM dbo.Room_Rates 
     WHERE @RoomType = Room_Type 
     AND @StartDate < Rate_End_Date 
     AND @EndDate >= Rate_Start_Date  ORDER BY StartDate 
    ; 
+0

絕對數據庫似乎不支持:USE或DECLARE。 – user763539 2013-04-08 12:55:04