简体   繁体   English

使用SQL Server中的数据在Access中生成报告?

[英]Using data from SQL Server to generate report in Access?

I have a legacy VB6 application that's connected to SQL Server where all data is stored. 我有一个旧的VB6应用程序,该应用程序已连接到存储所有数据的SQL Server。 I'm creating a report for the user where I'll need to pass 2 dates (FROM and TO). 我正在为用户创建报告,我需要传递两个日期(从和到)。 In SQL Server, I have created a few views that fulfill all my criteria and use the data from there to populate a dbo.tblTempTable (which is not actually #tempTable , but a normal table). 在SQL Server中,我创建了一些满足我所有条件的视图,并使用那里的数据填充dbo.tblTempTable (实际上不是#tempTable ,而是普通表)。 For now the dates are hardcoded, and I'm struggling with finding a way of passing those dates. 目前,日期已经过硬编码,而我正努力寻找一种传递日期的方法。 I'm pretty sure I am unable to pass parameters to a view. 我很确定我无法将参数传递给视图。 To populate the table I have a simple stored procedure that goes like this.... 为了填充表,我有一个简单的存储过程,如下所示。

TRUNCATE TABLE dbo.tbl_TTableReport

INSERT INTO tbl_TTableReport (UserID, CompanyID, CompanyName, Sold, Voided, Returned, Subtotal)
    SELECT        
        1234 AS USERID, CompanyID, CompanyName, 
        Sold, Voided, Returned, Subtotal
    FROM dbo.vCompanyInfo

So in this select statement, every piece of data comes from my final view ..vCompanyInfo . 因此,在此select语句中,每条数据都来自我的最终视图..vCompanyInfo Sold, Voided, and Returned need to be filtered by the FROM and TO dates. 出售,作废和退货都需要按FROM和TO日期进行过滤。 How would I do something like that? 我该怎么做?

EDITED. 已编辑。 I'm trying to create a stored procedure but I'm getting some weird errors...I have never create a complex multi SELECT statement SP before so I'm still trying to work out the kinks. 我正在尝试创建一个存储过程,但是却遇到了一些奇怪的错误……我之前从未创建过复杂的多重SELECT语句SP,因此我仍在尝试解决问题。

This is me trynig to build a SP with select statement, but I keep seeing the same error : Subquery returned more than 1 value. 这是我尝试用select语句构建SP的尝试,但我一直看到相同的错误: 子查询返回了多个值。 This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。

TRUNCATE TABLE dbo.tbl_TTableReport

INSERT INTO tbl_TTableReport
 (UserID
, CompanyID)
, CompanyName)
SELECT 1234 AS USERID
,(Select CompanyID from dbo.tblMainInv)
,(select CompanyName from dbo.tblCompanies)

For both the CompanyID and companyName there are about 30 values. 对于CompanyID和companyName,大约有30个值。 I'm trying to have them get loaded into the table, I don't know where my error is. 我正在尝试将它们加载到表中,但我不知道我的错误在哪里。

I recommend you create an ADP Access project to connect to your SQL Server database . 我建议您创建一个ADP Access项目以连接到SQL Server数据库 This allows you more control over the queries that you send to the Sql Server for reports and data entry forms. 这使您可以更好地控制发送到Sql Server的报表和数据输入表单的查询。 When creating the project file you designate the server and database with which you need to connect to. 创建项目文件时,请指定需要连接的服务器和数据库。 Then setting up the data source for your report is as simple as including the select statement to issue against the database into the data source of the report. 然后,为报告设置数据源非常简单,只需将要针对数据库发出的select语句包含到报告的数据源中即可。 You can also refer to text boxes on forms or have automatic prompts for parameters. 您还可以引用表单上的文本框,或者具有自动提示输入参数的功能。

As for the error you are getting in your second query in your edit... you are using the sub-query as an expression. 至于错误,您将在编辑中进入第二个查询...您正在使用子查询作为表达式。 SQL Server is being nice in letting you use a sub-query as one of the values to output in your select statement, but it's expecting just that... one value. SQL Server非常好用,它允许您将子查询用作要在select语句中输出的值之一,但它只是期望...一个值。 So you could do something like: 因此,您可以执行以下操作:

Select
    (select count(*) from MyTableValues) as CountTotal,
    (select sum(*) from MyTableValues) as SumTotal

However, what you are trying to do is return several rows when SQL Server is expecting you to return just one. 但是,您试图做的是在SQL Server期望您仅返回一行时返回几行。 If you want to join the data of two tables, you use a join ... something like: 如果要联接两个表的数据,请使用join ...,例如:

SELECT 1234 AS USERID, CompanyID, CompanyName
from dbo.tblMainInv
join dbo.tblCompanies
on tblMainInv.CompanyID = tblCompanies.CompanyID

Which is probably something that your view dbo.vCompanyInfo probably already looks like. 您的视图dbo.vCompanyInfo可能已经看起来像这样。

If your determined to use your temp table approach (although I highly recommend you reconsider as your just making more work for future maintenance)... this is something that your procedure might look like: 如果您决定使用临时表方法(尽管我强烈建议您重新考虑一下,因为您只是为将来的维护做更多的工作)...这可能是您的过程如下所示:

GO
CREATE PROCEDURE sp_LoadReportData
(
    @UserID as int,
    @StartDate as datetime,
    @EndDate as datetime
) AS

delete from tbl_TTableReport
where USERID = @UserID

INSERT INTO tbl_TTableReport 
    (UserID, CompanyID, CompanyName, Sold, Voided, Returned, Subtotal)
SELECT        
    @UserID AS USERID, 
    CompanyID, 
    CompanyName, 
    Sold, 
    Voided, 
    Returned, 
    Subtotal
FROM vCompanyInfo
where 
    Sold between @StartDate and @EndDate
    and Voided between @StartDate and @EndDate
    and Returned between @StartDate and @EndDate

GO

You would then call the procedure with the filters you want and send the UserID as a parameter like this: 然后,您将使用所需的过滤器调用该过程,并将UserID作为参数发送,如下所示:

exec sp_LoadReportData 1234, '1/1/2016', '1/20/2016'

But this still doesn't solve your problem... because you'll still have to send the UserID as a parameter in your report's data source like: 但这仍然不能解决您的问题...因为您仍然必须将UserID作为参数发送到报表数据源中,例如:

select * from tbl_TTableReport where UserID=1234

Unless you want to create a table and procedure for every user, you should really rethink your strategy and learn how to do it the standard way. 除非您要为每个用户创建一个表和过程,否则您应该真正地重新考虑您的策略并学习如何以标准方式进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM