将 SQL Server 查询转换为 Delphi 语法

发布于 2024-11-17 00:21:43 字数 1132 浏览 3 评论 0原文

这是有效的原始 SQL 服务器查询:

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 不是一个列。

This is the original SQL server query which works:

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'

I converted it to 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;

However I am getting 'Invalid column NAME 'DBLMS''

What am I missing here ? DBLMS is not a column.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴意少年 2024-11-24 00:21:43

您没有引用 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 注入的侵害。查看这篇文章在查询中使用参数

You are no quoting the strings inside of your sql sentence try rewriting your code to this :

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;

As addtional advice , try using parameters instead of string literals, in this way you will protect your code against sql injection beside others advantages. check this article Using Parameters in Queries

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文