简体   繁体   English

SQL DB2将查询结果存储到变量中

[英]SQL DB2 store query result into variable

Usind a DB2 tables: This is what is done manually in a table Usind DB2表:这是在表中手动完成的操作

select app_id from table_app where APP_NAME='App_Temp';

gets me an ID, say it's 234 给我一个ID,说是234

I copy than and do: 我比做:

select * form table_roles where role_app_id=234;

gets me a row , which is my desired end result. 让我连续行,这是我想要的最终结果。

Is there a way save the result of the first one into a variable and do the second query without the manual step in between using local variables? 有没有一种方法可以将第一个查询的结果保存到变量中,并且在不使用局部变量之间进行手动操作的情况下进行第二个查询呢?

I know, you can query out the information with a very simple join between two tables, but I'd like to know, how it works with variables. 我知道,您可以通过两个表之间的非常简单的连接来查询信息,但是我想知道它如何与变量一起使用。 Is there a way? 有办法吗?

Just can just plug it in: 只需插入即可:

select *
from table_roles
where role_app_id = (select app_id from table_app where APP_NAME = 'App_Temp');

If there can be more than one match, use in instead of = . 如果可以有多个匹配项,请使用in代替=

You can also phrase this as a join: 您也可以将此短语表述为联接:

select r.*
from table_roles r join
     table_app
     on r.role_app_id = a.app_id and APP_NAME = 'App_Temp';

However, this might return duplicates, if two apps have the same name. 但是,如果两个应用程序具有相同的名称,则这可能会返回重复项。 In that case, use select distinct : 在这种情况下,请使用select distinct

select distinct r.*
from table_roles r join
     table_app
     on r.role_app_id = a.app_id and APP_NAME = 'App_Temp';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM