如何创建SQL Server报告服务(SSRS)报告

本文介绍了如何创建SQL Server Reporting Services (SSRS)报告,包括所需条件、创建报告服务器项目、定义数据源和数据集、设计报告布局、格式化报告、添加分组和总计,以及发布报告到服务器的基本步骤。适合具有SQL基础知识的用户参考。
摘要由CSDN通过智能技术生成

SQL Server Reporting Services (SSRS) is part of Microsoft SQL Server services since version 2000. If you are an ordinary user with a basic knowledge of SQL, this article will help you in creating your first SSRS report.

自2000版以来,SQL Server Reporting Services(SSRS)是Microsoft SQL Server服务的一部分。如果您是具有SQL基本知识的普通用户,则本文将帮助您创建第一个SSRS报告。

创建SSRS报告的要求 (Requirements for creating a SSRS report)

Before you begin using SSRS and creating reports you must have:

在开始使用SSRS和创建报告之前,您必须具有:

创建报告服务器项目 (Create a report server project)

The first thing to do is to create a report server project because we will need this later for saving report definition files and any other files that you need for creating SSRS reports.

要做的第一件事是创建报告服务器项目,因为稍后我们将需要它来保存报告定义文件以及创建SSRS报告所需的任何其他文件。

  • SQL Server Data Tools. Click on the SQL Server数据工具 。 单击File menu, find 文件菜单,找到New and then 新建 ,然后找到Project.项目
  • Business Intelligence. Business Intelligence
  • Reporting Services and then Reporting Services ,然后单击Report Server Project. Report Server Project
  • Report test in 名称”中键入Name “报告测试”
  • OK to finish. 确定”完成。



创建一个新的报告定义文件 (Create a new report definition file)

  • Solution Explorer, and then right-click on the 解决方案资源管理器” ,然后右键单击“ Reports folder. In the 报告”文件夹。 在“ Add section click 添加”部分中,单击“ New Item. 新建项目”
  • Add New Item, click 添加新项 ”窗口中,单击Report. “报告”
  • Design view you define your report layout, and in the 设计视图中,您可以定义报告布局,在Preview view you can run your report. 预览视图中,您可以运行报告。

定义数据源和数据集 (Defining data source and dataset)

Setting up a connection

建立连接

In order to retrieve data from a database or from some other resource, you will need to define the data source. In the following section, you will see how you can define the data source. We will use AdventureWorks2012 database as an example.

为了从数据库或其他资源中检索数据,您将需要定义数据源。 在下一节中,您将看到如何定义数据源。 我们将以AdventureWorks2012数据库为例。

  • View menu find and click Report Data, then 查看”菜单中,找到并单击“报告数据”,然后单击“ New and after that 新建 ”,然后单击该Data Source数据源。
  • Type AdventureWorks2012 in the field Name
  • 名称字段中输入AdventureWorks2012
  • Embedded connection 嵌入式连接
  • Microsoft SQL Server Microsoft SQL Server
  • Connection string field type: 连接字符串”字段中,输入:




Note: If the database is not on the local computer, replace localhost with name of your database server instance.

注意:如果数据库不在本地计算机上,则将localhost替换为数据库服务器实例的名称。

  • Credentials and then 凭据” ,然后Use Windows Authentication. 使用“ Windows身份验证”。
  • OK, and you’re done. OK ,您就完成了。



Define a T-SQL query for report data

定义报告数据的T-SQL查询

In this section you will learn how to specify a dataset. You will need some basic SQL knowledge in order to create a query and define what information you will need from your database. The query you will see is just an example, and for your own purposes and database, you will have to change that step.

在本节中,您将学习如何指定数据集 。 您将需要一些基本SQL知识才能创建查询并定义数据库中需要的信息。 您将看到的查询只是一个示例,对于您自己的目的和数据库,您必须更改该步骤。

  • In the Report Data pane find and click New, and then Dataset.
  • 在“报表数据”窗格中,找到并单击“ 新建” ,然后单击 数据集”
  • Dataset Properties dialog box as 数据集属性”对话框中,作为“ Name type DataSet1 名称”键入DataSet1
  • Use a dataset embedded in my report. 使用报表中嵌入的数据集
  • data source, 数据源
  • Text as a Text作为Type, and type this into the Query input: Type ,然后在Query输入中输入以下内容:

     
    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'
     
    
    Defining a Dataset for the Table Report (Reporting Services)为表格报表定义数据集(Reporting Services)



Note: soh, pps, sd, pp, ppc are just shorter names for tables that we have in AdventureWorks2012 database.

注意: soh,pps,sd,pp,ppc只是AdventureWorks2012数据库中表的简称。

如何将表和字段添加到报表布局 (How to add a table and fields to a report layout)

After finishing with previous technical details, we can start with more interesting things, like designing your first SSRS report. This part is easy because instead of writing code, you can drag-and-drop graphic icons into the report format. This section will show you how to add Table and Fields to your report.

在完成了先前的技术细节之后,我们可以从更有趣的事情开始,例如设计您的第一个SSRS报告。 这部分很容易,因为您无需编写代码,而是可以将图形图标拖放到报告格式中。 本节将向您展示如何将表和字段添加到报告中。

  • In the View menu click Toolbox, then find and click Table and drag the mouse to the design area.
  • 在“ 视图”菜单中,单击“ 工具箱”,然后找到并单击“ 表” 然后将鼠标拖动到设计区域。
  • Dataset1 in order to see all the fields. Dataset1以便查看所有字段。
  • Drag one of the field (e.g. field Date) from Report Data to the column in the table

    将字段之一(例如,日期字段)从报表数据拖到表中的列




Preview report

预览报告

If you want to preview your report to see how it all looks, to correct errors, to correct issues or to verify design and data connection, click tab Preview.

如果要预览报告以查看其外观,更正错误,更正问题或验证设计和数据连接,请单击“ 预览”选项卡

Tips and tricks

技巧和窍门

An easier way to add a table is to right-click on the design surface, click Insert and then click Table.

添加表的一种更简单的方法是右键单击设计图面,单击“ 插入” ,然后单击“ 表”。

如何格式化报告 (How to format your Report)

Format currency and date

格式化货币和日期

If you want to format Date field to show only the date, follow the steps below:

如果要格式化日期字段以仅显示日期,请按照以下步骤操作:

  • Design tab, right-click the desired cell, then click 设计”选项卡中,右键单击所需的单元格,然后单击“ Text Box Properties. 文本框属性”。
  • Number, then in the field数字 ,然后在 Category click 类别字段中单击Date 日期
  • Type select date format you want, and click 类型”中选择所需的日期格式,然后单击“ OK to finish. 确定”完成。



If in your table you have a field that holds information about currency, but you only see ordinary numbers, you can format that field to display a number as currency:

如果表中有一个字段,其中包含有关货币的信息,但您只看到普通数字,则可以格式化该字段以将数字显示为货币:

  • Design tab, right-click the desired cell, then click 在设计选项卡上,右键单击所需的单元格,然后单击Text Box Properties. 文本框属性。
  • Number, then in the field 数字 ,然后在Category click 类别字段中单击Currency 货币
  • In accordance to your need you can change defaults

    根据您的需要,您可以更改默认值
  • OK to finish. 确定完成。



Changing column width and text style

更改列宽和文本样式

You also have an option to change text style (font, size etc.), and to change column width. Column width you can change by simply dragging the columns to the desired size.

您还可以选择更改文本样式(字体,大小等)并更改列宽 。 只需将列拖动到所需的大小,即可更改列宽。

Text style you can change by clicking the Format menu. In Format menu find Font, then click whatever you need (bold, italic, etc.)

您可以通过单击格式菜单来更改文本样式。 在“ 格式”菜单中,找到“ 字体” ,然后单击所需的任何内容(粗体,斜体等)。

添加分组 (Adding Grouping)

If you want to make data set in your SSRS report do the following:

如果要在SSRS报告中设置数据,请执行以下操作:

  • Design tab, and then choose pane 设计选项卡,然后选择窗格Row Groups 行组
  • Row Groups 行组”中
  • From your report pane drag some other field you want to group.

    从报告窗格中拖动要分组的其他字段。
  • Delete the old columns to the double line.

    删除旧列到双行。
  • Text Box Properties, the next steps are the same like in the formatting report section. 文本框属性” ,接下来的步骤与“格式报告”部分中的相同。



Tips and tricks

技巧和窍门

You can do the same by right-clicking on the surface and clicking View, and then Grouping.

您可以通过右键单击表面并单击“ 视图” ,然后单击 分组”来执行相同的操作

加总 (Adding totals)

Total is the sum of numeric, non-null data in the data region, and if you want to add totals for a group, you can do that by clicking Add Total for the group in the Grouping pane, and if you want to add totals for an individual cell just click Add Total for the cell.

总计是数据区域中数字非空数据的总和,如果要添加组的总计,则可以通过在“分组”窗格中单击该组的“ 添加总计”来完成,如果要添加总计对于单个单元格,只需单击该单元格的“ 添加总计 ”即可。

Add a daily total and grand total

添加每日总计和总计

  • and choose 并选择Add total, then click 添加总计 ,然后单击After之后 .
  • Type Daily to format a new name, Daily Total

    键入Daily以设置新名称Daily Total
  • After that, select the new cell [Daily Total], two Sum cells and the empty cell you see between them.

    之后,选择新的单元格[Daily Total],两个Sum单元格以及在它们之间看到的空单元格。
  • Format menu, choose background color. We chose color orange. 格式菜单上,选择背景色。 我们选择了橙色。



  • Add total, and then 添加总计” ,然后选择“ After之后” .
  • Type Grand to format a new name, Grand Total

    键入Grand以格式化新名称Grand Total
  • Select the new cell [Grand Total], the two [Sum] cells and the empty cell you see between them.

    选择新的单元格[总计],两个[总和]单元格以及在它们之间看到的空单元格。
  • Format menu, choose background color. We chose color light-blue. 格式菜单上,选择背景色。 我们选择浅蓝色。



Tips and tricks

技巧和窍门

After you add total, you can change the default function Sum. There is a list of different function you can use (avg, count, etc.).

添加总计后,可以更改默认功能总和。 您可以使用不同功能的列表(平均,计数等)。

将您的报告发布到报告服务器 (Publish your Report to the report server)

Finally, when you finished with creating your first SSRS report, you may want to publish the report:

最后,完成创建第一个SSRS报告后,您可能要发布该报告:

  • Project menu click 项目菜单中单击Test report Properties 测试报告属性
  • Put your report server’s name in the field TargetServerURL, then click OK.

    将报表服务器的名称放在TargetServerURL字段中,然后单击“确定”。
  • Build menu click 生成”菜单中,单击“ Deploy Test report. You will get a message that indicates whether you have successful or unsuccessful deployment. 部署测试报告” 。 您将收到一条消息,指出您是成功还是失败的部署。

Note: A problem will occurred if you don’t have permissions on the report server or if you have been using SSDT with administrator privileges.

注意:如果您对报表服务器没有权限,或者您一直在使用具有管理员权限的SSDT,则会出现问题。

Most common problems

最常见的问题

The most common problem in publishing the report to the report server is configuring the target server URL.

将报表发布到报表服务器中最常见的问题是配置目标服务器URL。

The first step that you need to do in configuration is to launch the SQL Server Reporting Service Configuration Manager and connect to the Reporting Service.

在配置中需要做的第一步是启动SQL Server Reporting Service 配置管理器并连接到Reporting Service

Create SSRS report

After that in section Web Service URL you can find URL to the report server.

之后,在“ Web服务URL”部分中,您可以找到报表服务器的URL。

Creating SSRS report shot

In section Report Manager URL you can see URL for viewing and managing reports.

在“ 报告管理器URL”部分中,您可以查看用于查看和管理报告的URL。

Creating SSRS report shot

In the Project menu open the Test report Properties.

项目菜单中,打开测试报告属性

Note: Prefix “Test report” is name of your report.

注意 :前缀“测试报告”是报告的名称。

Final step in configure target server URL is to fill the Web Service URL in the TargerServerURL property with correct URL that you find in Report Manager URL.

配置目标服务器URL的最后一步是使用在报表管理器URL中找到的正确URL填充TargerServerURL属性中的Web服务URL

Creating SSRS report shots 1

Congratulation, you have successfully created your first SSRS report!

恭喜,您已经成功创建了第一个SSRS报告!

参考资料 (References)

Reporting Services (SSRS)
Reporting Services Concepts (SSRS)
Reporting Services Backward Compatibility
Cause and Resolution of Reporting Services Errors

报告服务(SSRS)
报告服务概念(SSRS)
报表服务向后兼容性
Reporting Services错误的原因和解决方案

翻译自: https://www.sqlshack.com/create-sql-server-reporting-services-ssrs-report/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值