帆软报表 js 调用sql_SQL Server中的报表–创建由先前创建的主报表调用的基于矩阵的子报表

本文介绍如何在帆软报表中通过js调用SQL,创建一个子报表,当用户点击图表上的特定月份时,显示相应的矩阵数据。作者详细讲解了从创建存储过程到构建子报表和矩阵,以及参数传递的过程,最后展示了如何在主报表中调用子报表并显示实际月份名称。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

帆软报表 js 调用sql

Introduction

介绍

As you will remember from our last “get together” we created an application that permitted us to report upon financial data based upon an unorthodox financial year. In fact, our fiscal year started in July and ended in June. We created a chart to display the data.

您可能会记得,从上次“ 聚会 ”开始,我们创建了一个应用程序,该应用程序使我们能够基于非正统的财务年度报告财务数据。 实际上,我们的会计年度从7月开始,到6月结束。 我们创建了一个图表来显示数据。

In today’s “get together” we are going to push our application a bit further and build in a subreport which will bring up the underlying data when the end user clicks upon the chart for any particular month. Thus should the user click on February 2015, then all of February’s data (for the selected funds) is shown in a matrix. If the user chooses March, then March’s data is shown.

在今天的“聚在一起”中,我们将进一步推动我们的应用程序并构建一个子报表,当最终用户单击任何特定月份的图表时,该子报表将显示基础数据。 因此,如果用户单击2015年2月,则矩阵中将显示2月份的所有数据(针对选定的资金)。 如果用户选择三月,则显示三月的数据。

Without further ado, let’s get started!

事不宜迟,让我们开始吧!

入门 ( Getting Started )

We begin by opening SQL Server Data Tools and opening the project that we created in the article entitled “Setting run dates on the fly”.

我们首先打开SQL Server数据工具,然后打开在标题为“即时设置运行日期”的文章中创建的项目。

Our report surface is brought into view. We note that at this stage in the game, that all we have is the chart.

我们的报告界面已成为现实。 我们注意到在游戏的这个阶段,我们所拥有的只是图表。

In SQL Server Management Studio, we open the stored procedure that we created for this chart.

在SQL Server Management Studio中,我们打开为此图表创建的存储过程。

We are now going to clone an additional stored procedure that will utilize an additional parameter and render the raw data behind the graph; the contents of which will be dependent upon which value of year month that is passed through via the main report. The code listing may be seen in Addenda 1.

现在,我们将克隆一个附加的存储过程,该过程将使用附加的参数并将原始数据呈现在图形后面。 其内容将取决于通过主报表传递的年份月份的值。 代码清单可以在附录1中看到。

The reader will note that we have added the parameter @YearMth to the stored procedure header (see above). Additionally, we have declared a variable @YearMth and set this variable to ‘201502’. This will be commented out once we are ready to create the new stored procedure.

读者会注意到,我们已将参数@YearMth添加到存储过程标头中(请参见上文)。 此外,我们已声明变量@YearMth并将此变量设置为'201502'。 一旦我们准备创建新的存储过程,这将被注释掉。

In the screen dump above, we are able to view the result set generated by the query.

在上面的屏幕转储中,我们可以查看查询生成的结果集。

返回报告服务 ( Back in Reporting Services )

We right click upon the “Reports” folder and select “Add” and “New Item”.

我们右键单击“ Reports”文件夹,然后选择“ Add”和“ New Item”。

We select “Report” and give our report a name “DatesOnTheFlySubReport”. We click “Add” and we are brought into the “DatesOnTheFlySubReport” subreport drawing surface (see below).

我们选择“报告”,并将报告名称命名为“ DatesOnTheFlySubReport”。 单击“添加”,然后将其带到“ DatesOnTheFlySubReport”子报表工程图表面(请参见下文)。

We add a “Matrix” report item from the tool box (see above).

我们从工具箱中添加了一个“矩阵”报告项(见上文)。

We now must create dataset. Should you be unfamiliar with the concept of “data sources” and “data sets” please do yourself a favour and have a look at my SQL Shack article entitled “Now you see it, now you don’t”.

现在,我们必须创建数据集。 如果您不熟悉“数据源”和“数据集”的概念,请帮自己一个忙,看一下我SQL Shack文章,标题为“现在您看到了,现在您不知道了”。

/now-see-now-dont/

/ now-see-now-dont /

By right-clicking on the “Dataset” folder, we bring up the context menu. We click “Add Dataset” (see above).

通过右键单击“ Dataset”文件夹,我们弹出上下文菜单。 我们点击“添加数据集”(见上文)。

The “Dataset Properties” dialog box is brought up. We click the “Use a dataset embedded in my report” option. The thing that is still missing is a local or embedded data source. To create one, we click the “New” button (see above).

出现“数据集属性”对话框。 我们单击“使用报表中嵌入的数据集”选项。 仍然缺少的是本地或嵌入式数据源。 要创建一个,我们点击“ New”按钮(见上文)。

The “Data Source Properties” dialogue box is brought up (see above).

出现“数据源属性”对话框(请参见上文)。

We give our local data source a name “DatesOnTheFlyMatrix” and link it to the “DatesOnTheFly” shared data source which was created at our last “get together”. We click OK to create this local data source.

我们为本地数据源命名为“ DatesOnTheFlyMatrix”,并将其链接到在上一次“聚会”中创建的“ DatesOnTheFly”共享数据源。 我们单击“确定”以创建此本地数据源。

We are brought back to the “Dataset Properties” page. We give our dataset a name “DatesOnTheFlyMatrix”. We also select the “SQLShackFiscalFiscalYearSubReport” stored procedure to pull our data from the table to the report.

我们回到“数据集属性”页面。 我们给数据集命名为“ DatesOnTheFlyMatrix”。 我们还选择“ SQLShackFiscalFiscalYearSubReport”存储过程,将我们的数据从表中拉到报告中。

Clicking on the “Fields” tab (see below and to the left) we see the fields that will be pulled.

单击“字段”选项卡(请参见下面和左侧),我们将看到要拉出的字段。

This looks fine. Our last task is to configure our parameters.

看起来不错。 我们的最后一个任务是配置参数。

As we recall from our previous discussion, as the currency codes and funds have been configured for multi-select, we cannot utilize the normal “=Parameters!CurrCode.Value” declaration. If we recall correctly, what is passed to the stored procedure is a comma delimited string.

正如我们在前面的讨论中所回忆的那样,由于已将货币代码和资金配置为多选,因此我们无法使用常规的“ = Parameters!CurrCode.Value”声明。 如果我们没有记错的话,传递给存储过程的是逗号分隔的字符串。

This implies that we must once again utilize “=JOIN(Parameters!CurrCode.Value,",")” (see above).

这意味着我们必须再次利用“ = JOIN(Parameters!CurrCode.Value,“,”)”(请参见上文)。

We do the same for the fund list (see below).

我们对基金清单也做同样的事情(见下文)。

The remaining Parameter “YearMth” may remain as is for the sole reason that only one particular value of “YearMth” is being passed to the stored procedure.

剩余的参数“ YearMth”可以原样保留,原因是仅将“ YearMth”的一个特定值传递给存储过程。

We click OK to leave the “Dataset Properties” dialog box and find ourselves (once again) on our drawing surface.

我们单击“确定”以离开“数据集属性”对话框,并再次在绘图表面上找到自己。

This achieved, we now link the dataset (that we just created) to the “DataSetName” property of the Matrix (see above and to the bottom right).

为此,我们现在将刚刚创建的数据集链接到Matrix的“ DataSetName”属性(请参见上方和右下方)。

As we do not require the “ColumnGroups” we are going to delete them. We right-click on the “ColumnGroups” and from the context menu, we select “Delete Group”.

由于我们不需要“ ColumnGroups”,因此我们将其删除。 我们右键单击“ ColumnGroups”,然后从上下文菜单中选择“ Delete Group”。

We are asked if we wish to “Delete group and related row and columns” OR “Delete group only”. We select “Delete group only”.

询问我们是否要“删除组以及相关的行和列”或“仅删除组”。 我们选择“仅删除组”。

Clicking on the matrix we are able to bring up our list of fields (5 in total see above).

单击矩阵,我们可以调出字段列表(总共5个,见上文)。

As the reader will note, each of the five fields has its own column. We have also placed a text box on the top of our report to inform the user that this is the raw data behind the chart for the given “YearMth” (see above).

正如读者会注意到的那样,五个字段中的每个字段都有其自己的列。 我们还在报告的顶部放置了一个文本框,以告知用户这是给定“ YearMth”(参见上文)在图表后面的原始数据。

Further, we have formatted the “Market Value” field, right-oriented the figures (see above).

此外,我们对“市场价值”字段进行了格式设置,使数字正确定向(请参见上文)。

Last, but not least, we must modify the data grouping, to group not only on the fund but on the currency code as well (see below).

最后但并非最不重要的一点是,我们必须修改数据分组,不仅要对基金进行分组,还要对货币代码进行分组(请参见下文)。

回到我们的原始图表 ( Back on our original chart )

Having completed our matrix based subreport, we are now in a position to connect the pieces and complete our report.

完成基于矩阵的子报表后,我们现在可以连接各个部分并完成我们的报告。

By right-clicking on the “bars” of our vertical bar chart, we see the “Series Properties” option on the context menu (see below).

通过右键单击垂直条形图的“条形图”,我们可以在上下文菜单中看到“系列属性”选项(见下文)。

We select the “Series Properties” option.

我们选择“系列属性”选项。

With the “Series Properties” dialog box open (see above) we select the “Action” tab (see above).

打开“系列属性”对话框(参见上文)后,我们选择“操作”选项卡(参见上文)。

When the action tab opens we click the “Go to report” option and select the matrix based sub-report that we just created. Additionally we pass our three parameters “Funds”, “CurrCode” and the CHOSEN VALUE OF THE MONTH SELECTED generated by clicking on one of the vertical bars of the chart.

当“操作”选项卡打开时,我们单击“转到报告”选项,然后选择我们刚刚创建的基于矩阵的子报告。 此外,我们通过单击图表的垂直条之一 ,传递三个参数“基金”,“ CurrCode”和“选定月份的选择值”

通过引用传递参数 ( Parameter Passing By Reference )

In order to understand what is actually transpiring, let us put our reports under a microscope and walk through what is actually transpiring.

为了了解实际发生的情况,让我们将报告放在显微镜下,逐步了解实际发生的情况。

Running our “Mother” or main report, we choose funds “FDR1” and “PAT2”. We also tell the system that we wish to see transactions performed in US and Canadian dollars. We then click the “Feb” vertical bar (see above).

在运行“母亲”或主报告时,我们选择“ FDR1”和“ PAT2”资金。 我们还告诉系统,我们希望看到以美元和加元进行的交易。 然后,我们单击“ Feb”竖线 (参见上文)

The sub-report is then passed the “Funds”, the “CurrCode” string PLUS THE VALUE OF YEARMTH FOR THAT VERTICAL BAR!

然后,子报告将传递 “资金”,“ CurrCode”字符串以及该垂直栏的生命值!

Once all three parameters are passed through, they are “given” to the dataset (see below).

一旦所有三个参数都通过,它们就会被“赋予”数据集(见下文)。

Now that the dataset has acceptable arguments to pass through to our sub report stored procedure (see below), it accepts the data from the stored procedure when the stored procedure has completed processing (see the stored procedure below).

现在,该数据集具有可接受的参数通过,从而使我们的子报告的存储过程(见下文), 它接受 从当存储的过程完成 处理(见下面的存储过程) 的存储过程中的数据

Let us see how this works in practice

让我们看看这在实践中如何运作

The reader will note that within the sub-report in the parameters dropdowns, a crumb trail is shown. These are the arguments that we passed from the “mother” report. Normally these dropdowns are hidden from the user.

读者会注意到,在参数下拉列表的子报表中,显示了碎屑轨迹。 这些是我们从“母亲”报告中传递的论据。 通常,这些下拉菜单对用户是隐藏的。

In fact when we hide the parameters, then our sub-report resembles the screenshot shown above. Also, note that the title of the report shows the “YearMth” selected (see above).

实际上,当我们隐藏参数时,子报告类似于上面显示的屏幕截图。 另外,请注意,报告的标题显示了所选的“年份”(请参见上文)。

One last change that we perhaps would like to make, is to the title of sub-report. It currently says “Raw data behind the chart for 201502”. Let us make the title a bit more user-friendly and pass the name of the actual month.

我们也许要进行的最后一项更改是更改子报表的标题。 当前显示为“ 201502图表背后的原始数据”。 让我们使标题更易于使用,并传递实际月份的名称。

To achieve this, we add the @monthee parameter to the call to the sub-report (see below).

为此,我们将@monthee参数添加到对子报表的调用中(请参见下文)。

We add the parameter “@monthee” to the parameters to be passed through to the sub-report stored procedure (see below).

我们将参数“ @monthee”添加到要传递到子报表存储过程的参数中(请参见下文)。

and we refresh the sub-report dataset (see below).

然后刷新子报表数据集(请参见下文)。

We now change the “Title Box” expression to reflect the following (see below).

现在,我们更改“标题框”表达式以反映以下内容(请参见下文)。

Note that the value that will now be displayed is the actual month name.

请注意,现在将显示的值是实际的月份名称。

When tidied up and a bit of eye candy is added, our final subreport may be seen below:

整理后添加一点点糖果,下面可以看到我们的最终子报告:

结论 ( Conclusions )

As we once again come to the end of our “get together”, let us think back for a minute on what we have seen.

当我们再次走到“聚在一起”的结尾时,让我们回顾一下我们所看到的。

  • We have created a matrix based sub-report which is called by the report that we created within a previous “get together”.

    我们创建了一个基于矩阵的子报表,该报表由我们在先前的“聚在一起”中创建的报表调用。
  • We have seen how the parameters within the original report may be passed to the sub-report along with any values that the user may have selected from the main report.

    我们已经看到了原始报告中的参数如何与用户可能已从主报告中选择的任何值一起传递到子报告。
  • We have seen that these parameters and their related arguments may be passed to a sub-report based stored procedure and that the resulting dataset may be utilized for the matrix within the sub-report.

    我们已经看到,这些参数及其相关参数可以传递给基于子报表的存储过程,并且所得数据集可用于子报表中的矩阵。
  • Finally we have seen how to utilize parameter values within the “Report Title” along with being able to add the actual month name to the “Report Title” by utilizing the First() function.

    最后,我们了解了如何利用“报告标题”中的参数值以及如何通过使用First()函数将实际的月份名称添加到“报告标题”中。

As always, should you have any questions, please do contact the SQL Shack editor or me directly.

与往常一样,如果您有任何疑问,请直接与SQL Shack编辑器或我联系。

In the interim, happy programming!!

在此期间,编程愉快!!

附录1 ( Addenda 1 )

 
USE [SQLShackFinancial]
GO
 
/****** Object:  StoredProcedure [dbo].[SQLShackFiscalFiscalYearSubReport]    Script Date: 2/15/2015 7:12:37 AM ******/
SET ANSI_NULLS ON
GO
 
SET QUOTED_IDENTIFIER ON
GO
 
 
 
--IF OBJECT_ID(N'tempdb..#Fund') IS NOT NULL
--BEGIN
--     DROP TABLE #Fund
--END
--IF OBJECT_ID(N'tempdb..#Currency') IS NOT NULL
--BEGIN
--     DROP TABLE #Currency
--END
--Go
CREATE procedure [dbo].[SQLShackFiscalFiscalYearSubReport]
(
@CurrCode varchar(2000),
@Funds varchar(2000),
@YearMth varchar(6)
,@Monthee varchar(3)
)
as
 
--Declare @CurrCode varchar(2000)
--Declare @Funds varchar(2000)
--Declare @YearMth varchar(6)
--set @CurrCode = 'USD, CAD'
--Set @Funds = 'Pat2, FDR1'
--Set @YearMth = '201502'
declare @beginFiscal date 
declare @endFiscal date 
declare @Yearr varchar(4)
declare @decider int 
-- The guts of the code
set @decider = datepart(Month,convert(date,getdate()))
set @Yearr   = datepart(YEAR,Convert(date,Getdate()))
set @Yearr    = case when @decider >= 7 then datepart(YEAR,Convert(date,Getdate())) else @Yearr -1 end
set @Beginfiscal = convert(varchar(4),@Yearr) + '0701'
set @Endfiscal   = dateadd(d,-1,dateadd(Year,1,@beginFiscal))
 
 
declare @Comma CHAR(1)
declare @Fund table  
( 
fundid varchar(8) NOT NULL 
)  
   
      Set @Comma = ','
 BEGIN
 DECLARE @Position INT
 DECLARE @Substringg VARCHAR(8000)
 SELECT @Position =1
 IF (LEN(@Funds)<1) OR @Funds IS NULL RETURN
 WHILE @Position!=0
 BEGIN
 SET @Position=CHARINDEX(@Comma,@Funds)
 IF @Position<>0
 SET @Substringg=LEFT(@Funds,@Position-1)
 ELSE
 SET @Substringg=@Funds
 IF(LEN(@Substringg)>0)
 INSERT INTO @Fund(fundid) VALUES(RTRIM(LTRIM(@Substringg)))
 SET @Funds=RIGHT(@Funds,LEN(@Funds)-@Position)
 IF LEN(@Funds)=0 BREAK
 END 
 select fundid as [fundid] Into #fund from @fund
 END
 
 Set @Position = 0
 Set @Substringg = ''
declare @CurrCode1 table  
( 
Currency varchar(8) NOT NULL 
)  
   
 Set @Comma = ','
 BEGIN
 --DECLARE @Position INT
 --DECLARE @Substringg VARCHAR(8000)
 SELECT @Position =1
 IF (LEN(@CurrCode)<1) OR @CurrCode IS NULL RETURN
 WHILE @Position!=0
 BEGIN
 SET @Position=CHARINDEX(@Comma,@CurrCode)
 IF @Position<>0
 SET @Substringg=LEFT(@CurrCode,@Position-1)
 ELSE
 SET @Substringg=@CurrCode
 IF(LEN(@Substringg)>0)
 INSERT INTO @CurrCode1 (currency) VALUES(RTRIM(LTRIM(@Substringg)))
 SET @CurrCode =RIGHT(@CurrCode,LEN(@CurrCode)-@Position)
 IF LEN(@CurrCode)=0 BREAK
 END 
 select currency as [currency] Into #currency from @CurrCode1
 END
  
 
 
 Select fund_ID, FND_TOT_CURR_CD, sum(MKT_VAL_TOT_AMT) as [Market Value] , YearMth,
 @monthee,
 case 
      when right(YearMth,2) = '01' then 'Jan'
	  when right(YearMth,2) = '02' then 'Feb'
      when right(YearMth,2) = '03' then 'Mar'
	  when right(YearMth,2) = '04' then 'Apr'
	  when right(YearMth,2) = '05' then 'May'
	  when right(YearMth,2) = '06' then 'Jun'
	  when right(YearMth,2) = '07' then 'Jul'
	  when right(YearMth,2) = '08' then 'Aug'
      when right(YearMth,2) = '09' then 'Sep'
	  when right(YearMth,2) = '10' then 'Oct'
      when right(YearMth,2) = '11' then 'Nov'
	  when right(YearMth,2) = '12' then 'Dec' else '99' end as Monthee
	  from (
 select FUND_ID, CAL_DT, FND_TOT_TYP_CD, FND_TOT_CURR_CD, MKT_VAL_TOT_AMT   
,convert(varchar(4),datepart(year,Convert(date,Cal_dt)))  +
Convert(varchar(2),case when len(datepart(month,Convert(date,Cal_dt))) = 1 then  
'0' + convert(varchar(2),datepart(month,Convert(date,Cal_dt)))
else  convert(varchar(2),datepart(month,Convert(date,Cal_dt))) end)  as YearMth
from [dbo].[DateOnTheFly]  DOTF
inner join #Fund fund
on DOTF.Fund_ID = fund.fundid
Inner join #Currency currency
on currency.currency = DOTF.FND_TOT_CURR_CD
where cal_dt between @beginFiscal and @endFiscal 
)a
where @YearMth = rtrim(ltrim(YearMth))
Group by
FUND_ID,  FND_TOT_TYP_CD, FND_TOT_CURR_CD,YearMth,
 case 
      when right(YearMth,2) = '01' then 'Jan'
	  when right(YearMth,2) = '02' then 'Feb'
      when right(YearMth,2) = '03' then 'Mar'
	  when right(YearMth,2) = '04' then 'Apr'
	  when right(YearMth,2) = '05' then 'May'
	  when right(YearMth,2) = '06' then 'Jun'
	  when right(YearMth,2) = '07' then 'Jul'
	  when right(YearMth,2) = '08' then 'Aug'
      when right(YearMth,2) = '09' then 'Sep'
	  when right(YearMth,2) = '10' then 'Oct'
      when right(YearMth,2) = '11' then 'Nov'
	  when right(YearMth,2) = '12' then 'Dec' else '99' end 
 order by YearMth desc
 
 
 
GO
 

翻译自: https://www.sqlshack.com/reporting-sql-server-create-matrix-based-subreport-called-by-previously-created-main-report/

帆软报表 js 调用sql

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值