Export view data programmatically in Access/SQL Server

[亡魂溺海] 提交于 2019-12-11 18:07:51

问题


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?


回答1:


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

`




回答2:


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




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/2713598/export-view-data-programmatically-in-access-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!