Reputation: 423
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 B I 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?
Upvotes: 0
Views: 566
Reputation: 906
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
Upvotes: 1