简体   繁体   中英

Stored procedure with more than 1 select statements

I have 2 tables, Table A and Table B. I have to display values from A as follows:

select * from A
where EntityId = 26039 and EntityTypeId = 'REQUEST'

Then, from BI want to retrieve values as follows:

select OrderRequestTaskId from B
where OrderRequestId = 26039

This might give 0 or more OrderRequestTaskId. If this query returns OrderRequestTaskId values of 1,2,3 then I want to display them from table A as follows:

select * from A
where  (EntityId = 1 and EntityTypeId = 'TASK')
or  (EntityId = 2 and EntityTypeId = 'TASK')
or (EntityId = 3 and EntityTypeId = 'TASK')

In short the select statement will now be as follows:

select * from A
where (EntityId = 26039 and EntityTypeId = 'REQUEST')
 or (EntityId = 1 and EntityTypeId = 'TASK' )
or ( EntityId = 2 and EntityTypeId = 'TASK')
or( EntityId = 3 and EntityTypeId = 'TASK')

I am not sure how to do this in a stored procedure so that I get all the 4 rows as an output. Can I store the output from the statement

select OrderRequestTaskId from B
where OrderRequestId = 26039

and then use it to build the final select statement?

You just need to use InnerJoin it would look like

select a.* from TableA a
Inner Join TableB b 
on a.EntityID=b.OrderRequestId 

--where conditions if any

where OrderRequestId = 26039

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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