How to upload an SQL query to SSRS that returns 5 tables, ensuring that when the report is exported to Excel, these tables appears on separate sheets

Our reports, written in MS SQL, need to be uploaded to SSRS, allowing coworkers to generate them with different parameters and export SSRS reports to Excel files. We typically create stored procedures with parameters and use them in SSRS reports.

Currently, I have an issue: if the stored procedure returns more than one table, SSRS only recognizes the first one and ignores the others. However, I need to display all 5 tables when the customer exports the report to Excel. The first table should be on the first sheet, the second table on the second sheet, and so on. How can this be achieved in SSRS?”

The problem is that all these tables depend on each other, so I don’t want to create 5 different stored procedures for each table. Below, I will paste my SQL query. Don’t pay attention with tables and columns, I just wanted to show that we first need to get data from several tables, perform some calculations, and then use this result to generate the other 4 tables.

-- All Debit/Credit Transactions for '20240229' date
            
    
SELECT FT_ID, TRANSACTION_TYPE, DEBIT_ACCT_NO, CREDIT_ACCT_NO, f.DEBIT_CUSTOMER, f.CREDIT_CUSTOMER
    , ABS(DEBIT_AMOUNT) AMOUNT
    , DEBIT_THEIR_REF, CREDIT_THEIR_REF, RECORD_STATUS, DATE_TIME, INPUTTER, f.DEBIT_VALUE_DATE
INTO #All
FROM T24DATA.dbo.FUNDS_TRANSFER f
WHERE (f.DEBIT_ACCT_NO='GEL164589800' OR f.CREDIT_ACCT_NO='GEL164589800') AND
(f.DEBIT_VALUE_DATE=20240229 OR (f.DATE_TIME>=2402290000 AND f.DATE_TIME<=2402292359))

            

-- TransferOut Transactions

SELECT * , LEFT(Purpose,11) PID
INTO #DBTrOut FROM (
SELECT *            
FROM TransfersHub.dbo.TransfersHis WHERE SenderAccCode IN ('GE48CD1450000045010039') AND ValueDate='2024-02-29'
UNION ALL
SELECT * FROM TransfersHub.dbo.Transfers WHERE SenderAccCode IN ('GE48CD1450000045010039') AND ValueDate='2024-02-29'
             )m



-- TransferIn Transactions
             
SELECT 
*
INTO #CDTrIn
FROM TransfersHub.dbo.TransfersHis WHERE ReceiverAccCode IN ('GE48CD1450000045010039') AND ValueDate='2024-02-29'

UNION ALL

              SELECT
              * 
              FROM TransfersHub.dbo.Transfers WHERE ReceiverAccCode IN ('GE48CD1450000045010039') AND ValueDate='2024-02-29'




-- Debits 

            SELECT f.* , i.Pid, o.PID TrOutPID
            INTO #DEBIT
            FROM #All f
            LEFT JOIN ListOfBalance.dbo.InsLoan i ON i.LoanId=f.DEBIT_THEIR_REF
            LEFT JOIN #DBTrOut o ON f.DEBIT_THEIR_REF=o.Reference
            WHERE f.DEBIT_ACCT_NO='GEL164589800'


    

-- Credits
            
            SELECT f.* , i.Pid, o.PID TrOutPid
            INTO #CREDIT
            FROM #All f
            LEFT JOIN ListOfBalance.dbo.InsLoan i ON i.LoanId=f.DEBIT_THEIR_REF
            LEFT JOIN #DBTrOut o ON i.Pid=o.PID
            WHERE f.CREDIT_ACCT_NO='GEL164589800'






-- All Customer IDs for this date


    
            SELECT DISTINCT CREDIT_CUSTOMER AS CUSTOMER 
            INTO #Customers
            FROM #All
            WHERE CREDIT_CUSTOMER <> 0
            UNION   
            SELECT DISTINCT DEBIT_CUSTOMER 
            FROM #All
            WHERE DEBIT_CUSTOMER <> 0






-- Customers with their Credit Amounts Summed and Debit Amounts Summed
            

            SELECT cst.CUSTOMER
            , SUM(IIF(db.DEBIT_CUSTOMER = cst.CUSTOMER, db.AMOUNT, 0)) CreditAMOUNT
            , SUM(IIF(db.CREDIT_CUSTOMER = cst.CUSTOMER, db.AMOUNT, 0)) DebitAMOUNT
            INTO #AMOUNTsSummedCUST
            FROM #Customers cst
            LEFT JOIN #All db ON cst.CUSTOMER=IIF(db.CREDIT_CUSTOMER=0, db.DEBIT_CUSTOMER, db.CREDIT_CUSTOMER)
            GROUP BY  cst.CUSTOMER


        



`your text`

-- Find differences


SELECT m.CUSTOMER, m.LEGAL_ID, m.CU_NAME1, m.CU_NAME2, SUM(m.DebitAMOUNT) AS DebitAMOUNT,  SUM(m.CreditAMOUNT) AS CreditAMOUNT
INTO #Different
FROM (
            SELECT ams.CUSTOMER, cst.LEGAL_ID, cst.CU_NAME1, cst.CU_NAME2 
            , ams.DebitAMOUNT
            , ams.CreditAMOUNT
            FROM #AMOUNTsSummedCUST ams
            LEFT JOIN T24DATA.dbo.CUSTOMERS cst ON ams.CUSTOMER=cst.CUSTOMER_CODE
            ) m
            GROUP BY m.CUSTOMER, m.LEGAL_ID, m.CU_NAME1, m.CU_NAME2
            HAVING SUM(m.DebitAMOUNT) <> SUM(m.CreditAMOUNT)

            


-- Sheet1 in Excel Should be the customers which have different Credit and Debit Amounts


      SELECT DISTINCT ams.* , acc.ACCOUNT_NUMBER 
      , tot.ReceiverAccCode
      , CreditAMOUNT - DebitAMOUNT AS Difference
      , ABS(CreditAMOUNT - DebitAMOUNT) DifferenceABS
      FROM #Different ams
      LEFT JOIN T24DATA.dbo.ACCOUNTS AS ACC WITH (NOLOCK) ON ACC.CUSTOMER = ams.CUSTOMER AND ACC.CATEGORY=3100
      LEFT JOIN #DBTrOut tot ON ams.LEGAL_ID=tot.ReceiverTaxCode
      ORDER BY DifferenceABS
            
            

-- Sheet2 (Debits) Should contain the debit transactions which does not match with credit transactions


      SELECT * FROM  (
      SELECT db.*  
      FROM #DEBIT db
      INNER JOIN #Different df ON df.CUSTOMER=db.CREDIT_CUSTOMER
     ) db
     LEFT JOIN (  
      SELECT cd.* 
      FROM #CREDIT cd
      LEFT JOIN #Different df ON df.CUSTOMER=cd.DEBIT_CUSTOMER
      WHERE (df.CUSTOMER IS NOT NULL OR (cd.DEBIT_CUSTOMER=0 AND cd.CREDIT_CUSTOMER=0))
      ) cd ON db.AMOUNT=cd.AMOUNT AND ISNULL(db.TrOutPID, db.Pid) = cd.Pid

      WHERE cd.AMOUNT IS NULL





-- Sheet3 (Credits) Should contain the credit transactions which does not match with debit transactions


      SELECT * FROM  (

      SELECT cd.* 
      FROM #CREDIT cd
      LEFT JOIN #Different df ON df.CUSTOMER=cd.DEBIT_CUSTOMER
      WHERE (df.CUSTOMER IS NOT NULL OR (cd.DEBIT_CUSTOMER=0 AND cd.CREDIT_CUSTOMER=0))
      ) cd
      LEFT JOIN (
                    SELECT db.*   
                    FROM #DEBIT db
                    INNER JOIN #Different df ON df.CUSTOMER=db.CREDIT_CUSTOMER
                )    db
      ON db.AMOUNT=cd.AMOUNT AND ISNULL(db.TrOutPID, db.Pid) = cd.Pid

      WHERE db.AMOUNT IS NULL





-- SHeet4 (TransferOut) should contain this information


      SELECT * FROM #DBTrOut


-- Sheet5 (TransferIn) should contain this information

      SELECT * FROM #CDTrIn

New contributor

Monika Eliashvili is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật