I'm looking for an ANSI-SQL method to do a Select query without returning any record, but fill a TDataSet's Fields structure.

我正在寻找ANSI-SQL方法来执行Select查询而不返回任何记录,但填充了TDataSet的Fields结构。

The method I found is by adding a "where 1=0" in any query, for example:

我找到的方法是在任何查询中添加“where 1 = 0”,例如:

Select Id, name, province
from customers
where 1=0

This is a fairly trivial example, it turns a little more complicated when I have to work with queries entered by the user, then parse them, remove the where clause if it already has one, and replace by "1=0".

这是一个相当简单的例子,当我必须处理用户输入的查询,然后解析它们,删除where子句(如果已有)时,它会变得更复杂,并替换为“1 = 0”。

If the last clause in the user-entered query is the where clause, then there's no problem at all, but what about more complicated queries like this:

如果用户输入的查询中的最后一个子句是where子句,那么根本没有问题,但是更复杂的查询如下:

select
  c.lastname,
  sum(cs.amount)
from customersales cs
join customers c on c.idcustomer=cs.idcustomer
/* where 1=0 */
group by c.idcustomer, c.lastname

By using the "where 1=0" method, the only way to insert it in the previous example is by having a rather powerful SQL parser (remember the user can enter complex queries, including Subqueries, and all that), who can understand where to include this string.

通过使用“where 1 = 0”方法,在前一个示例中插入它的唯一方法是使用一个相当强大的SQL解析器(记住用户可以输入复杂查询,包括子查询,以及所有这些),谁能理解哪里包括这个字符串。

Does anyone knows a better way to do this? I cannot use "limit 1" because it must be in an ANSI way.

有谁知道更好的方法吗?我不能使用“限制1”,因为它必须采用ANSI方式。

5 个解决方案

#1


11

What about adding your own SELECT around the user's SELECT?

如何在用户的SELECT周围添加自己的SELECT?

SELECT * FROM (
select
  c.lastname,
  sum(cs.amount)
from customersales cs
join customers c on c.idcustomer=cs.idcustomer
/* where 1=0 */
group by c.idcustomer, c.lastname
) x
WHERE 0=1

EDIT: ORDER BY would not work with that solution, but since you get no rows, you could try to remove that from the query when necessary.

编辑:ORDER BY不适用于该解决方案,但由于没有行,您可以尝试在必要时从查询中删除它。

更多相关文章

  1. mysql 批量更新与批量更新多条记录的不同值实现方法
  2. 远程连接Linux虚拟机上的mysql失败的解决方法
  3. PHP OOP和MySQLi连接=致命错误:调用未定义的方法sqmyli::arrayQu
  4. MySQL忘记root密码??重置密码方法
  5. hibernate(*.hbm.xml)中新添加的字段被标记为红色(找不到)的解决方法
  6. Linux安装MySQL的两种方法 先卸载之前版本
  7. linux安装apache/mysql/php的最新完整方法(cents和ubuntu都实用)
  8. 获取项目列表的更好方法:缓存序列化数据与数据库查询或其他?
  9. cpanel导入大数据库(mysql)的方法

随机推荐

  1. android 中定时器的几种用法总结
  2. android > WebView > 布局
  3. virtualbox 安装android设置分辨率(自定
  4. android中MediaPlayer的用法
  5. 原:Android(安卓)获取屏幕高度、标题高度
  6. Android使用AchartEngine绘制曲线图
  7. android:TextAppearance.Material.Widget
  8. Android(安卓)Weak Handler:可以避免内存
  9. Android自定义样式
  10. android情景模式切换的实现