向 SQL 查询的结果添加静态值 [英] Adding a static value to the results of an SQL query

查看:23
本文介绍了向 SQL 查询的结果添加静态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法通过 SQL 查询来完成此操作.

I'm wondering if there is a way to accomplish this with an SQL query.

我有一个表,我们称之为LISTOFTHINGS",它有两个感兴趣的字段ID"和NAMEOFTHING"

I have a table, lets call it "LISTOFTHINGS" that has two fields of interest "ID" and "NAMEOFTHING"

我想要做的是构造一个查询,以便返回的是这个查询的结果:

What I want to do is construct a query such that what gets returned is the results of this query:

SELECT ID, NAMEOFTHING FROM LISTOFTHINGS ORDER BY NAMEOFTHING

并在上述查询的第一行之前添加一行,其值为-1, 'ALL THINGs'".

and adds a row before the first row of the above query that has " -1, 'ALL THINGs' " as the values.

所以如果表有以下三个条目:

So if the table has the following three entries:

1, 'THING 1'
3, 'THING 3'
2, 'THING 2'

那么我想要的结果是这样的:

Then the result that I want looks like this:

-1, 'ALL THINGS'
1, 'THING 1'
2, 'THING 2'
3, 'THING 3'

我知道我可以进行查询并使用代码创建列表,但是在我使用它的 VB6 程序中,我有一个接受查询的 3rd 方应用程序(我没有代码)用结果填充 ACTIVEX 表控件.我没有添加静态值的钩子.

I know that I can do the query and create the list with code, but inside the VB6 program where I am using this, I have a 3rd party app (which I don't have the code for) that takes the query to populate an ACTIVEX table control with the results. I don't have the hooks to go in to add the static value.

我也知道我可以在表中为-1, 'ALL THINGS'"添加一条记录,但问题是,如果我这样做,我需要更改程序中的很多地方以忽略处理时的记录.

I also know that I could just put a record in the table for " -1, 'ALL THINGS' " but the problem is, if I do that, I will need to change a lot of places in the program to ignore that record when doing processing.

ALL THINGS"值是一种伪记录,它处理程序某一部分的特殊情况.

The 'ALL THINGS' value is sort of a pseudo record that handles a special case for one part of the program.

推荐答案

你能在查询中做一个联合吗?

Could you do a union in the query?

SELECT -1 AS ID , 'ALL THINGS' AS NAMEOFTHING FROM DUAL /*'FROM DUAL' is an Oracle thing,
                                                       not sure if you need to do 
                                                       something like that in DB2*/
UNION 
SELECT ID, NAMEOFTHING FROM LISTOFTHINGS ORDER BY NAMEOFTHING

<小时>

显然,这就是 DB2 应该这样做的方式


Apparently, this is how it should be done for DB2

SELECT -1 AS ID , 'ALL THINGS' AS NAMEOFTHING FROM SYSIBM.SYSDUMMY1
UNION 
SELECT ID, NAMEOFTHING FROM LISTOFTHINGS ORDER BY NAMEOFTHING

这篇关于向 SQL 查询的结果添加静态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆