gpt4 book ai didi

delphi - 将 SQL Server 查询转换为 Delphi 语法

转载 作者:行者123 更新时间:2023-12-03 15:42:32 26 4
gpt4 key购买 nike

这是有效的原始 SQL Server 查询:

use HIS

SELECT
room_type,rate_start_date,rate_end_date,rate
, DATEDIFF(DAY,case when rate_end_date < '2011.08.21'
then '2011.08.19'
else rate_start_date
end,
case when rate_start_date > '2011.08.19'
then '2011.08.21'
else rate_end_date
end
) AS days FROM room_rates
WHERE room_type = 'DBLMS' AND rate_start_date <= '2011.08.21'
AND rate_end_date > '2011.08.19'

我将其转换为 Delphi SQL :

procedure TForm1.Button1Click(Sender: TObject);
begin
uniQuery1.Close;
uniQuery1.SQL.Clear;
uniQuery1.SQL.Add('SELECT room_type,rate_start_date,rate_end_date,rate,');
uniQuery1.SQL.Add('DATEDIFF(DAY,case when rate_end_date < 2011.08.21 then 2011.08.19 else rate_start_date end,');
uniQuery1.SQL.Add('case when rate_start_date > 2011.08.19');
uniQuery1.SQL.Add('then 2011.08.21 else rate_end_date end) AS days FROM room_rates');
uniQuery1.SQL.Add('WHERE room_type = DBLMS AND rate_start_date <= 2011.08.21');
uniQuery1.SQL.Add('AND rate_end_date > 2011.08.19');
uniQuery1.Open;
end;

但是我收到“无效的列名称“DBLMS””

我在这里缺少什么? DBLMS 不是一个列。

最佳答案

您没有引用 sql 语句中的字符串,请尝试将代码重写为:

procedure TForm1.Button1Click(Sender: TObject);
begin
uniQuery1.Close;
uniQuery1.SQL.Clear;
uniQuery1.SQL.Add('SELECT room_type,rate_start_date,rate_end_date,rate,');
uniQuery1.SQL.Add('DATEDIFF(DAY,case when rate_end_date < ''2011.08.21'' then ''2011.08.19'' else rate_start_date end,');
uniQuery1.SQL.Add('case when rate_start_date > ''2011.08.19''');
uniQuery1.SQL.Add('then ''2011.08.21'' else rate_end_date end) AS days FROM room_rates');
uniQuery1.SQL.Add('WHERE room_type = ''DBLMS'' AND rate_start_date <= ''2011.08.21''');
uniQuery1.SQL.Add('AND rate_end_date > ''2011.08.19''');
uniQuery1.Open;
end;

作为附加建议,尝试使用参数而不是字符串文字,这样除了其他优点之外,您还可以保护您的代码免受 SQL 注入(inject)。查看这篇文章Using Parameters in Queries

关于delphi - 将 SQL Server 查询转换为 Delphi 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6475521/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com