在 Access/SQL Server 中以编程方式导出视图数据

发布于 2024-08-29 22:38:37 字数 297 浏览 2 评论 0原文

我们有一个连接到 SQL Server 2000 数据库的 Access 应用程序前端。我们希望能够以编程方式将某些视图的结果导出为我们可以使用的任何格式(最好是 Excel,但 CSV/制表符分隔也可以)。到目前为止,我们只是按 F11,打开视图,然后按“文件”->“另存为”,但我们开始获得超过 16,000 个结果,且无法导出。

我想要某种我们可以触发的服务器端存储过程来执行此操作。我知道 sp_makewebtask 过程可以执行此操作,但是它需要服务器上的管理权限,并且由于显而易见的原因,我们无法将其授予每个人。

有什么想法吗?

We have an Access application front-end connected to a SQL Server 2000 database. We would like to be able to programmatically export the results of some views to whatever format we can (ideally Excel, but CSV / tab delimited is fine). Up until now we've just hit F11, opened up the view, and hit File->Save As, but we're starting to get results with more than 16,000 results, which can't be exported.

I'd like some sort of server side stored procedure we can trigger that will do this. I'm aware of the sp_makewebtask procedure that does this, however it requires administrative rights on the server, and for obvious reasons we can't give that to everyone.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

人海汹涌 2024-09-05 22:38:37

您可能无法授予每个人管理权限,但也许您可以:

  1. 创建一个特殊用户,例如“WebTaskUser”,具有读取权限
    从所需的视图并执行存储的 sp_makewebtask
    程序。您将向一个用户授予权限,而不是向所有人授予权限。

  2. 然后创建一个包装存储过程(如下),允许您的用户执行它,同时它包含执行对 sp_makewebtask 过程的特定预定义调用的代码,每个视图一个,仅授予该单个 sp_makewebtask 过程的执行权限。单个用户帐户 - 并非所有管理权限都被授予 - 仅执行 - 只对一个帐户:-)。

  3. 根据您的喜好从 SSMS 测试和优化存储过程,
    Access-vba,或任何最适合您的内容

  4. 当您对过程感到满意时,授予任何进一步的必要
    授予您的用户或用户角色的权限,以便他们可以执行它
    好吧。

`

--Example code to create user, and add permissions I might be able to add later

USE [some_database_x];

CREATE PROCEDURE EXPORT_VIEWS_TO_EXCEL
@TARGET_FOLDER NVARCHAR(100) DEFAULT 'C:\temp';
@FILE_TAG NVARCHAR(20) DEFAULT '';
@VIEWNAME NVARCHAR(100);
WITH EXECUTE AS 'WebTaskUser'
AS
BEGIN
IF @VIEWNAME IS NOT NULL
BEGIN
     DECLARE @myOUTPUTFILE NVARCHAR(100);
     SET @myOUTPUTFILE = @TARGET_FOLDER + '\' + @VIEWNAME + COALESCE(@FILE_TAG,'');
     DECLARE @myQUERY NVARCHAR(150);
     IF @VIEWNAME = 'mydb.dbo.firstview'
     BEGIN
          SET @myQUERY = 'Select * from mydb.dbo.firstview',
     END
     IF @VIEWNAME = 'mydb.dbo.secondview'
     BEGIN
          SET @myQUERY = 'Select * from mydb.dbo.secondview'
     END
     EXECUTE sp_makewebtask
     @outputfile = @OUTPUTFILE,
     @query = @myQUERY,
     @colheaders = 1, @FixedFont = 0, @lastupdated = 0, @resultstitle='My Title'
END

RETURN 0;
END
GO

`

You might not be able to give everyone administrative rights, but maybe you could:

  1. Create a special user, e.g. 'WebTaskUser', with permissions to read
    from the desired views and to execute the sp_makewebtask stored
    procedure. You would be giving permissions to one user-not to everyone.

  2. Then create a wrapper stored procedure (below) that allows your users to execute it, while it contains code to execute specific predefined calls to the sp_makewebtask procedure, one per view, granting only execute permissions for that single sp_makewebtask procedure, to one single user account-not all administrative permissions are granted-only execute-only to one account :-).

  3. Test and refine the stored procedure to your liking from SSMS,
    Access-vba, or whatever suits you best

  4. When you're happy with the proc, grant any further necessary
    permissions to your users or user roles, so they can execute it as
    well.

`

--Example code to create user, and add permissions I might be able to add later

USE [some_database_x];

CREATE PROCEDURE EXPORT_VIEWS_TO_EXCEL
@TARGET_FOLDER NVARCHAR(100) DEFAULT 'C:\temp';
@FILE_TAG NVARCHAR(20) DEFAULT '';
@VIEWNAME NVARCHAR(100);
WITH EXECUTE AS 'WebTaskUser'
AS
BEGIN
IF @VIEWNAME IS NOT NULL
BEGIN
     DECLARE @myOUTPUTFILE NVARCHAR(100);
     SET @myOUTPUTFILE = @TARGET_FOLDER + '\' + @VIEWNAME + COALESCE(@FILE_TAG,'');
     DECLARE @myQUERY NVARCHAR(150);
     IF @VIEWNAME = 'mydb.dbo.firstview'
     BEGIN
          SET @myQUERY = 'Select * from mydb.dbo.firstview',
     END
     IF @VIEWNAME = 'mydb.dbo.secondview'
     BEGIN
          SET @myQUERY = 'Select * from mydb.dbo.secondview'
     END
     EXECUTE sp_makewebtask
     @outputfile = @OUTPUTFILE,
     @query = @myQUERY,
     @colheaders = 1, @FixedFont = 0, @lastupdated = 0, @resultstitle='My Title'
END

RETURN 0;
END
GO

`

打小就很酷 2024-09-05 22:38:37

如果您想在访问中执行所有操作,您可以将视图链接为链接表,然后使用 TransferSpreadsheet 方法将该“表”导出为 csv 文件

编辑:

当您想要在服务器端执行此操作时,请查看

http://www.mssqltips.com/tip.asp?tip=1633

我有以前用过这个,效果很好

If you wanted to do everything in access you could link the view as a linked table and then using the TransferSpreadsheet method you could export that “table” as a csv file

EDIT:

As you want to do it server side check this out

http://www.mssqltips.com/tip.asp?tip=1633

I have used this before and it worked just fine

鸠魁 2024-09-05 22:38:37

您可能想看看 SSIS - 它允许创建服务器端包以在服务器端导出数据。

另一种选择是右键单击数据库并运行数据导出向导(下面使用 SSIS)。

另一种选择是创建命令行实用程序 (SQLCMD) 将数据导出到平面文件中。

You may want to look at SSIS - it allows creating server side packages to export data on server side.

Another option is to right-click your database and run through data export wizard (which is using SSIS underneath).

Yet another option is to create command line utility (SQLCMD) to export the data into a flat file.

美人迟暮 2024-09-05 22:38:37

你用过VB或宏吗?

  1. 创建一个看起来像视图结构的本地表
  2. 创建一个删除表内容的查询
  3. 创建一个将视图内容插入本地表的查询
  4. 使用“使用 Excel 分析”功能或内置导出功能之一
  5. 创建一个运行前两个 qry 的宏(或 vba),然后单击一下即可导出

我刚刚尝试了 26k 行,它工作没有问题

HTH

Have you used VB or Macros?

  1. Create a local table the looks like the view structure
  2. create a query that deletes the contents of the table
  3. create a query that inserts the contents of the view to the local table
  4. use the "analyze with excel" feature or one of the built in export features
  5. Create a macro (or vba) that runs the first two qrys and the export with a single click

I just tried it with 26k rows and it worked without a problem

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文