SQL Server - 过滤器

问题描述:

我正在将Access数据库升级到SQL Server(并且

攀登该学习曲线!)。向导愉快地升级了所有表格

我可以使用ODBC链接到它们。


应用程序控制飞机维护修订的分配

航空公司类型操作的手册。


在应用程序中,启动时会加载一个表单,允许用户选择

选择他们目前感兴趣的记录来自4个不同的

下拉列表,这些列表基于4个查询表(飞机类型,飞机

注册,基础和负责人)。用户可以从无或部分或全部列表中进行选择

。在当前系统中,所有表格

都基于查询,其中包含基于

形式的where子句的记录,例如''Where(TableX.BaseId = Form!选择!BaseId或

Form!Selection!BaseId is Null)''。更新选择表单(

用户更改其选择)时,表单会强制重新查询任何当前

打开表单以及该表单上的任何组合/列表框,以便表格没有

专门编码,甚至不知道它。使用jet

可以很好地工作,并且为用户提供了极好的灵活性,并且相对容易实现
程序(几年前,当我认为我的大脑工作得更好时)。


在SQL Server中使用如上所述的查询会减慢速度(超过

一点!)因为查询必须在Access中执行以允许

包含表格!参数东西。我希望能够在SQL中创建

视图,这将传递所需的数据,但如果可能的话,不使用

参数查询。


我有几个关于如何做到这一点的想法,但所有似乎都有某种类型的问题。什么似乎是最简单的(在我看来

无论如何)将在服务器上有一个选择表,每行当前记录

对于具有表中过滤器选择的用户和

然后基于通过此选择表连接的数据表的所有视图。

这将意味着选择表的一次更新每次用户改变他们的选择并且没有太多额外的工作要做(由我设计

视图等)。这个方法我的问题是我无法弄清楚如何在每个当前用户的Selection表中有一行
,注意一个

用户可以登录到多台计算机上的同一个数据库或者

(天堂帮助我们)在一台计算机上多次登录(我知道它不是好的,但是b $ b好​​,但那是事情是这样的)。我查看了使用USER_ID,但是这将会在一个用户2台计算机场景中出现重复,并且

查看了SID(我认为它)是)但这似乎是每个连接和我的

阅读在线书籍意味着一个Access会话可以有多个连接

。我需要的是某种唯一的标识符

运行SQL Server知道的每个访问''会话''我可以找到哪些
然后包含在选择中表格。


我正在寻找的是一种简单的方法,使得视图无需工作,而b / b
必须对每种形式的SQL进行编码(以及组合框和列表框)等等


其他人一定有同样的问题。有没有优雅的

(即最小工作)解决方案?


谢谢


我喜欢这个新闻组 - 在过去的几年里,它已经为我节省了太多时间(并且头发还是b
)。特别感谢常客

贡献者 - 你和其他所有人都知道你是谁。


问候


鲍勃·科林森


鲍勃在colsoft dot com dot au

I am in the process of upgrading an Access database to SQL Server (and
climbing that learning curve!). The wizard happily upgraded all the tables
and I can link to them OK using ODBC.

The application controls allocation of revisions to aircraft maintenance
manuals for an airline type operation.

In the application there is a form loaded at start-up allowing the user/s to
select the records that they are currently interested in from 4 different
pull down lists which are based on 4 lookup tables (Aircraft Type, Aircraft
Registration, Base and Person Responsible). The user can make a selection
from none or some or all of the lists. In the current system all the forms
are based on queries that include records by where clauses based on the
form, eg, ''Where (TableX.BaseId = Form!Selection!BaseId or
Form!Selection!BaseId is Null)''. When the Selection Form is updated (the
user changes their selection) the form forces a requery on any currently
open form and any combo/List boxes on that form so that the forms don''t have
to be specifically coded to even know about it. This works well using jet
and provides excellent flexibility to the user and was relatively easy to
program (some years ago when my brain worked better I think).

In SQL Server using queries like the above slows things down (more than a
little!) because the query has to be executed in Access to allow the
inclusion of the Forms! parameters stuff. I want to be able to create
views in SQL which will pass back the data required but without using
parameter queries if possible.

I have had several ideas about how to do this but all of seem to have
problems of some sort. What would seem to be the simplest (to my mind
anyway) would be to have a ''Selection'' table on the Server with one row for
each currently logged on user with the filter selections in the table and
then base all the views on the data tables joined via this selection table.
This would mean one update of the selection table each time the user varied
their selection and not much extra work to be done (by me in designing the
views etc). My problem with this approach is that I cannot work out how to
have one row in the Selection table for each current user, noting that one
user could be logged on to the same database on more than one computer or
(heaven help us) logged on more than once on one computer (I know it isn''t
good but that is the way it is). I looked into using USER_ID but that would
appear to end up with duplicates in the ''one user 2 computers'' scene, and
looked into SID (I think it is) but that seems to be per connection and my
reading of the On-Line book implies that there can be multiple connections
for one Access session. What I need is some sort of unique identifier for
each Access ''session'' running that SQL Server knows about and which I can
find and then include in the Selection table.

What I am looking for is a simple way of making the views work without
having to code the SQL in each form (and combo box and list box) etc.

Other people must have had the same sort of problem. Is there any elegant
(ie minimal work) solution?

Thanks

I do love this news group - it has saved me soooo much time (and hair
pulling out) over the last few years. Thanks especially to the regular
contributors - you, and everyone else, know who you are.

Regards

Bob Collinson

bob leave this out at colsoft dot com dot au


嗨Bob,


如果您通过ODBC连接到sql server并再次运行查询

来自访问的sql server表几乎是击败了想法

使用sql server。想法是让sql server完成工作。

你在sql server中运行查询(存储过程)并将

结果集下拉到Access。问题是你没有使用ODBC。使用ADO。我已经看到人们在我的地方对sql服务器表运行查询

来自Access - 45分钟后他们仍在等待结果集。

最后,我在sql

服务器中为同一个查询编写了一个存储过程。 45分钟变成450毫秒。不开玩笑!


这里有一个示例如何从

访问中运行sql server中的存储过程并下拉结果集 - 这个例子假设你是

拉取指定日期范围内的数据,因此您有日期参数

开始日期和结束日期 - 注意:您需要在
中做出参考
工具/参考/ Microsoft ActiveX数据对象2.6库或更高版本。

您可以从MDAC2.6获得此信息。另请注意,您需要加载MDAC2.5

,因为MDAC2.5是包含Jet4.0的最后一个MDAC。从MDSN获取此信息

或google it。


Sub GetResultSetFromSqlSvr()

Dim cmd As New ADODB.Command,RSado As新的ADODB.Recordset

Dim RSdao作为DAO.Recordset,j作为整数,Retval作为变体


cmd.ActiveConnection =" Provider = SQLOLEDB;" _

& Data Source = yourServer; _

& Initial Catalog = yourSqlServerDB; UID = sa; PWD =;"

cmd.CommandTimeout = 600

cmd.CommandType = adCmdStoredProc

cmd.CommandText =" stp_YourStoredProc"

cmd.Parameters(" @ bDate")。Value = sDate

cmd.Parameters(" @ eDate")。Value = eDate

设置RSado = cmd.Execute

设置RSdao = CurrentDb.OpenRecordset(" yourAccessTable")

DoEvents

做不到RSado.EOF

RSdao.AddNew

对于i = 0到RSado.Fields.Count - 1

RSdao( i)= RSado(i)

下一页

RSdao.Update

RSado.MoveNext

j = j + 1

RetVal = SysCmd(acSysCmdSetStatus,j)

循环

RSado.Close

结束子

Rich


***通过开发人员指南 http发送://www.developersdex.com ***
Hi Bob,

If you are connecting to sql server via ODBC and running queries agains
the sql server tables from access you are pretty much defeating the idea
of using a sql server. The idea is for the sql server to do the work.
You run the queries (stored procedures) in sql server and pull down the
result set to Access. The catch is that you don''t use ODBC. Use ADO. I
have seen people at my place run queries against the sql server tables
from Access - 45 minutes later they are still waiting for a result set.
Eventually, I wrote them a stored procedure for the same query in sql
server. 45 minutes turned into 450 miliseconds. No kidding!

Here is a sample how you run a stored procedure in sql server from
access and pull down the result set - this example assumes you are
pulling data for a specified date range, so you have date parameters for
start date and end date -- Note: you need to make a reference in
Tools/Reference/Microsoft ActiveX Data Objects 2.6 Library or higher.
You get this from MDAC2.6. Note also, you need to have MDAC2.5 loaded
because MDAC2.5 was the last MDAC to include Jet4.0. Get this from MDSN
or google it.

Sub GetResultSetFromSqlSvr()
Dim cmd As New ADODB.Command, RSado As New ADODB.Recordset
Dim RSdao As DAO.Recordset, j As Integer, Retval As variant

cmd.ActiveConnection = "Provider=SQLOLEDB;" _
& "Data Source=yourServer;" _
& "Initial Catalog=yourSqlServerDB;UID=sa;PWD=;"
cmd.CommandTimeout = 600
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "stp_YourStoredProc"
cmd.Parameters("@bDate").Value = sDate
cmd.Parameters("@eDate").Value = eDate
Set RSado = cmd.Execute
Set RSdao = CurrentDb.OpenRecordset("yourAccessTable")
DoEvents
Do While Not RSado.EOF
RSdao.AddNew
For i = 0 To RSado.Fields.Count - 1
RSdao(i) = RSado(i)
Next
RSdao.Update
RSado.MoveNext
j = j + 1
RetVal = SysCmd(acSysCmdSetStatus, j)
Loop
RSado.Close
End Sub
Rich

*** Sent via Developersdex http://www.developersdex.com ***


Rich,


看起来很棒,它将数据从sql server复制到一个访问权限




是否可以做类似的事情,但是使用ado记录集作为表格

/报告记录来源而不是更新访问表?

Rich P写道:
Rich,

this looks great, it copies the data from sql server into an access
table

is it possible to do something similar, but use ado recordset as a form
/ report recordsource instead of updating an access table ?
Rich P wrote:
嗨鲍勃,
来自访问的SQL服务器表几乎打败了使用SQL服务器的想法。想法是让sql server完成工作。
在sql server中运行查询(存储过程)并将
结果集下拉到Access。问题是你没有使用ODBC。使用ADO。我已经看到人们在我的地方对来自Access的SQL服务器表运行查询 - 45分钟后他们仍在等待结果集。
最后,我给他们写了一个存储过程sql
服务器中的相同查询。 45分钟变成450毫秒。不开玩笑!

下面是一个示例如何从
访问中运行sql server中的存储过程并下拉结果集 - 此示例假设您正在为数据提取数据指定的日期范围,因此您有
开始日期和结束日期的日期参数 - 注意:您需要在
工具/参考/ Microsoft ActiveX数据对象2.6库或更高版本中进行参考。
你从MDAC2.6得到这个。另请注意,您需要加载MDAC2.5
因为MDAC2.5是包含Jet4.0的最后一个MDAC。从MDSN得到这个或google它。

Sub GetResultSetFromSqlSvr()
Dim cmd作为新的ADODB.Command,RSado作为新的ADODB.Recordset
Dim RSdao作为DAO .Recordset,j As Integer,Retval As variant

cmd.ActiveConnection =" Provider = SQLOLEDB;" _
& Data Source = yourServer; _
& Initial Catalog = yourSqlServerDB; UID = sa; PWD =;"
cmd.CommandTimeout = 600
cmd.CommandType = adCmdStoredProc
cmd.CommandText =" stp_YourStoredProc"
cmd.Parameters(" @ bDate")。Value = sDate
cmd.Parameters(" @ eDate")。Value = eDate
Set RSado = cmd.Execute
Set RSdao = CurrentDb .OpenRecordset(" yourAccessTable")
DoEvents
做不到RSado.EOF
RSdao.AddNew
对于i = 0到RSado.Fields.Count - 1
RSdao(i)= RSado(i)
下一页
RSdao.Update
RSado.MoveNext
j = j + 1
RetVal = SysCmd(acSysCmdSetStatus,j) RSado.Close
结束Sub

Rich

***通过Developersdex发送 http://www.developersdex.com ***






Rich P写道:
嗨鲍勃,

如果你是通过ODBC连接到sql server并再次运行查询从访问你的sql server表几乎打败了使用sql server的想法。这个想法是让sql server完成这项工作。


现在,下面的代码示例看起来像某个类型的应用程序的一个非常好的实现,但是上面的语句让我这样说

真的很生气。我已经不得不支持用户创建的最糟糕的代码,因为他们在某些论坛上读到了一些像这样的虚假陈述当

时,他们的应用程序将更容易实现和维护,并且如果他们刚刚使用了Access查询,那么
的错误会少得多。


事实上,在大多数情况下,Access使用Access查询对服务器表的链接以及许多
$非常有效地查询数据

b $ b功能使Access成为开发

数据库应用程序的便利平台,因为shemes只使用存储的

程序来查询来自后端的数据。 br />

具体来说 - Access必须读取整个后端

表到客户端以执行Access查询。访问通常

将查询逻辑转发到后端进行处理,即使加入2

或更多表,只要它们链接到同一个ODBC源。 br />

在Access无法使用Access查询对服务器

表运行高效查询的情况下,其中90%是由于可能出现的问题而导致的问题

可以通过调整查询来解决,以消除诸如在GROUP BY字段中包含

VBA函数之类的问题。

您在sql中运行查询(存储过程)服务器并将结果集下拉到Access。问题是你没有使用ODBC。使用ADO。我已经看到人们在我的地方对来自Access的SQL服务器表运行查询 - 45分钟后他们仍在等待结果集。
最后,我给他们写了一个存储过程sql
服务器中的相同查询。 45分钟变成450毫秒。开玩笑吧!


当然可以,但这并不意味着你必须将整个应用程序内外。

如果调整Access查询没有给出同样的好处你是b $ b描述,那么也许一个存储过程应该用在特定情况下的



这是一个示例如何从
访问中运行sql server中的存储过程并下拉结果集 - 此示例假设您正在拉取指定日期范围的数据,因此您有
开始日期的日期参数和结束日期 - 注意:您需要在
工具/参考/ Microsoft ActiveX数据对象2.6库或更高版本中进行参考。
您可以从MDAC2.6获得此参考。另请注意,您需要加载MDAC2.5
因为MDAC2.5是包含Jet4.0的最后一个MDAC。从MDSN得到这个或google它。

Sub GetResultSetFromSqlSvr()
Dim cmd作为新的ADODB.Command,RSado作为新的ADODB.Recordset
Dim RSdao作为DAO .Recordset,j As Integer,Retval As variant

cmd.ActiveConnection =" Provider = SQLOLEDB;" _
& Data Source = yourServer; _
& Initial Catalog = yourSqlServerDB; UID = sa; PWD =;"
cmd.CommandTimeout = 600
cmd.CommandType = adCmdStoredProc
cmd.CommandText =" stp_YourStoredProc"
cmd.Parameters(" @ bDate")。Value = sDate
cmd.Parameters(" @ eDate")。Value = eDate
Set RSado = cmd.Execute
Set RSdao = CurrentDb .OpenRecordset(" yourAccessTable")
DoEvents
做不到RSado.EOF
RSdao.AddNew
对于i = 0到RSado.Fields.Count - 1
RSdao(i)= RSado(i)
下一页
RSdao.Update
RSado.MoveNext
j = j + 1
RetVal = SysCmd(acSysCmdSetStatus,j) RSado.Close
结束Sub

Rich

***通过Developersdex发送 http://www.developersdex.com ***