这是使用 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
;
但我无法检索字段,所以它无法正常工作。真的不知道为什么。连接设置正确。当我尝试检索我得到的字段时: