分享方式:


第 3 課:定義資料表報表的資料集 - SQL Server Reporting Services

當您為分頁報表定義資料來源之後,就需要定義資料集。 在 Reporting Services 中,報表所用的資料是包含在資料集中。 資料集含有指向資料來源的指標和報表要使用的查詢、導出欄位和變數。

使用報表設計師中的查詢設計工具來定義資料集。 在本教學課程中,您將建立一項查詢,可擷取 AdventureWorks2022 資料庫中的銷售訂單資訊。

定義報表資料的 Transact-SQL 查詢

  1. 在 [報表資料] 窗格中,選取 [新增]>[資料集...]。[資料集屬性] 對話方塊隨即開啟,並顯示 [查詢] 區段。

    Screenshot of the Dataset Properties dialog box showing the query section.

  2. 在 [名稱] 文字方塊中,輸入 "AdventureWorksDataset"。

  3. 然後選取 [使用內嵌在我的報表中的資料集] 圓形按鈕。

  4. 從 [資料來源] 下拉式方塊中,選取 AdventureWorks2022。

  5. 針對 [查詢類型],選取 [文字] 選項按鈕。

  6. 在 [查詢] 文字方塊中,輸入 (或複製並貼上) 下列 Transact-SQL 查詢。

    SELECT
       soh.OrderDate AS [Date],
       soh.SalesOrderNumber AS [Order],
       pps.Name AS [Subcat],
       pp.Name as [Product],
       SUM(sd.OrderQty) AS [Qty],
       SUM(sd.LineTotal) AS [LineTotal]
    FROM Sales.SalesPerson sp
    INNER JOIN Sales.SalesOrderHeader AS soh
          ON sp.BusinessEntityID = soh.SalesPersonID
       INNER JOIN Sales.SalesOrderDetail AS sd
          ON sd.SalesOrderID = soh.SalesOrderID
       INNER JOIN Production.Product AS pp
          ON sd.ProductID = pp.ProductID
       INNER JOIN Production.ProductSubcategory AS pps
          ON pp.ProductSubcategoryID = pps.ProductSubcategoryID
       INNER JOIN Production.ProductCategory AS ppc
          ON ppc.ProductCategoryID = pps.ProductCategoryID
    GROUP BY ppc.Name, soh.OrderDate, soh.SalesOrderNumber, pps.Name, pp.Name,soh.SalesPersonID  
    HAVING ppc.Name = 'Clothing'
    
  7. (選擇性) 選取 [查詢設計工具] 按鈕。 查詢會顯示在以文字為基礎的 [查詢設計工具] 中。 在 [查詢設計工具] 工具列上,選取 [執行] 按鈕,以檢視查詢的結果。 在 AdventureWorks2022 資料庫中,顯示的資料集包含 4 個資料表中的 6 個欄位。 查詢會使用別名之類的 Transact-SQL 功能。 例如,SalesOrderHeader 資料表稱為 soh

  8. 選取 [確定] 結束 [查詢設計工具]。

  9. 選取 [確定] 結束 [資料集屬性] 對話方塊。

[報表資料] 窗格會顯示 AdventureWorksDataset 資料集和欄位。

Screenshot of the Datasets folder showing the AdventureWorksDataset and its fields.

後續步驟

您已經成功指定一項擷取報表資料的查詢。 之後,您將要建立報表版面配置。 繼續第 4 課:將資料表新增至報表 (Reporting Services)