问题
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:
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.
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 :-).
Test and refine the stored procedure to your liking from SSMS, Access-vba, or whatever suits you best
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?
- Create a local table the looks like the view structure
- create a query that deletes the contents of the table
- create a query that inserts the contents of the view to the local table
- use the "analyze with excel" feature or one of the built in export features
- 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