1

I have to select two tables data, One is PackageData and another is PackageDataDetails. Here is PackageData is Parent table and PackageDataDetails is child table.

See below code:

SELECT 
    PD.Id, PD.MemberId, PD.Merchant, PD.Weight, PD.Remarks
FROM 
    [dbo].[PackageData] PD
INNER JOIN 
    Account A ON A.MemberId = PD.MemberId
INNER JOIN 
    Users U ON U.Id = A.UserId AND (@UserId IS NULL OR U.Id = @UserId)
WHERE 
    (@Status IS NULL OR @Status = '' OR  PD.SystemStatus = @Status)
    AND (@Id IS NULL OR PD.Id = @Id)
ORDER BY 
    MD.Id
    OFFSET @PageSize * (@PageNo+1 - 1) ROWS  
    FETCH NEXT @PageSize ROWS ONLY 
OPTION (RECOMPILE);

SELECT 
    PDD.Id, PDD.PackageDataId, PDD.Description, PDD.Quantity, PDD.Status
FROM 
    [dbo].[PackageDataDetails] PDD
INNER JOIN 
    [dbo].[PackageData] PD ON PD.Id = PDD.PackageDataId
WHERE 
    PDD.PackageDataId IN ({PD.Id of Above Select query})

This code block is written in a stored procedure. Here, I didn't want to rewrite the first query in where a condition of the second query.

I have to use this stored procedure in one API response to do things in one DB call only. Please share your idea to resolve this issue.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

4 Answers4

1

You can insert your first query data in table variable and then fetch the id from table variable in second query.Something like below.

Code:

 Declare @tblData TABLE
    ( 
        Id          Int,
        MemberId                    Int,
        Merchant    NVARCHAR(Max),
        Weight  NVARCHAR(Max),
        Remarks               Nvarchar(Max)

    );

   Insert Into @tblData
(
    Id,
    MemberId,
    Merchant,
    Weight,
    Remarks
)

SELECT PD.Id ,PD.MemberId, PD.Merchant,PD.Weight,PD.Remarks
FROM [dbo].[PackageData] PD
INNER JOIN Account A ON A.MemberId = PD.MemberId
INNER JOIN Users U ON U.Id = A.UserId AND (@UserId IS NULL OR U.Id = @UserId)
WHERE (@Status IS NULL OR @Status = '' OR  PD.SystemStatus = @Status)
AND (@Id IS NULL OR PD.Id = @Id)
ORDER BY MD.Id
OFFSET @PageSize * (@PageNo+1 - 1) ROWS  FETCH NEXT @PageSize ROWS ONLY OPTION (RECOMPILE);

SELECT PDD.Id, PDD.PackageDataId, PDD.Description, PDD.Quantity, PDD.Status
FROM [dbo].[PackageDataDetails] PDD
INNER JOIN [dbo].[PackageData] PD ON PD.Id = PDD.PackageDataId
WHERE PDD.PackageDataId IN (select Id from @tblData )
Techgeeks1
  • 556
  • 1
  • 4
  • 18
  • Yes, this way will works now for me. but here we have to create temp table, please provide if you have any other way for this without temp table. Thanks – Akhatarali Ansari Apr 04 '19 at 08:53
0

Please try below query.

First Query

SELECT 
    PD.Id ,PD.MemberId, PD.Merchant,PD.Weight,PD.Remarks
    INTO #temp 
    FROM [dbo].[PackageData] PD
    INNER JOIN Account A ON A.MemberId = PD.MemberId
    INNER JOIN Users U ON U.Id = A.UserId AND (@UserId IS NULL OR U.Id = @UserId)
    WHERE (@Status IS NULL OR @Status = '' OR  PD.SystemStatus = @Status)
    AND (@Id IS NULL OR PD.Id = @Id)
    ORDER BY MD.Id
    OFFSET @PageSize * (@PageNo+1 - 1) ROWS  FETCH NEXT @PageSize ROWS ONLY OPTION (RECOMPILE);

get output first query

 SELECT * FROM #temp 

Second query with the first query result.

SELECT 
     PDD.Id, PDD.PackageDataId, PDD.Description, PDD.Quantity, PDD.Status
FROM [dbo].[PackageDataDetails] PDD
    INNER JOIN [dbo].[PackageData] PD ON PD.Id = PDD.PackageDataId
    WHERE PDD.PackageDataId IN (SELECT PD.Id FROM #temp)

DROP TABLE #temp
Hemang A
  • 1,012
  • 1
  • 5
  • 16
0

Using a CTE(Common Table Expression):

i.e:

    ;with CTE1 as (
 select 1 as Col1,
 2 as Col2,
 3 as Col3
), CTE2 as (
select 1 as Col1,
        5 as Col2,
        6 as Col3
)select * from CTE1 where CTE1.Col1 IN (select Col1 from CTE2)
mrHLand
  • 43
  • 2
  • 10
0

If you only need the first query to feed the second, wrap the first in a CTE and refer to in running the second query:

;WITH PackageDataFiltered AS
(
    SELECT PD.Id ,PD.MemberId, PD.Merchant,PD.Weight,PD.Remarks
    FROM [dbo].[PackageData] PD
    INNER JOIN Account A ON A.MemberId = PD.MemberId
    INNER JOIN Users U ON U.Id = A.UserId AND (@UserId IS NULL OR U.Id = @UserId)
    WHERE (@Status IS NULL OR @Status = '' OR  PD.SystemStatus = @Status)
    AND (@Id IS NULL OR PD.Id = @Id)
    ORDER BY MD.Id
    OFFSET @PageSize * (@PageNo+1 - 1) ROWS  FETCH NEXT @PageSize ROWS ONLY 
)
SELECT PDD.Id, PDD.PackageDataId, PDD.Description, PDD.Quantity, PDD.Status
-- You can refer to fields from the CTE here too
-- like select the PDF.Merchant or PDF.Remarks
FROM [dbo].[PackageDataDetails] PDD
INNER JOIN [dbo].[PackageData] PD ON PD.Id = PDD.PackageDataId
INNER JOIN PackageDataFiltered PDF ON PDF.Id = PDD.PackageDataId
MarcinJ
  • 3,471
  • 2
  • 14
  • 18
  • possibly it will work, I need both select query results, but in this case, I can only select One table, means only either CTE result selected or use it in where the condition of a second query. So for this, I can use the answer of @Techgeeks1. – Akhatarali Ansari Apr 04 '19 at 08:50