将sql从服务器语法转换为绝对数据库

时间:2013-04-08 12:36:31

标签: delphi delphi-xe2 delphi-xe unidac

这是使用Unidac组件的SQL Server的原始代码: 在uniQuery的SQL(TStrings)中我有:

 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

1 个答案:

答案 0 :(得分: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
    ;