简体   繁体   English

使用存储过程将SQL表/数据导出到MS Access

[英]Export SQL Tables/Data to MS Access using stored procedure

i heard already from the way to use the Sql Server Export/Import Manager to export a sql table to a ms access file, but this is not dynamically to use, is there a way to do it in a stored procedure? 我已经从使用Sql Server导出/导入管理器将sql表导出到ms访问文件的方式中听说过,但这不是动态使用的,有没有办法在存储过程中做到这一点? Like execute a query and save this as table in a ms access file, like the Export/Import Manager but just in code? 像执行查询并将其另存为表格一样在ms访问文件中,例如“导出/导入管理器”,只是在代码中?

Thanks in advance! 提前致谢!

您可能可以创建并保存SSIS包,以执行从Sql Server到Access的导入/导出任务,并使用所需参数从存储过程中调用它。

I suppose there are several things you can try. 我想您可以尝试几种方法。 If you have access to SSIS, this will do the work for you, for sure. 如果您有权访问SSIS,那么这一定会为您完成工作。 You can use the Import/Export wizard to help you with the task. 您可以使用“导入/导出”向导来帮助您完成任务。

https://docs.microsoft.com/en-us/sql/integration-services/import-export-data/start-the-sql-server-import-and-export-wizard https://docs.microsoft.com/zh-cn/sql/integration-services/import-export-data/start-the-sql-server-import-and-export-wizard

Configure and Test the Linked Server 配置和测试链接服务器

Connect to the SQL Server and create a new linked server. This can be done using the following SQL script, which will create a new liked server called "AccessDB." (The value after @datasrc is the path to the Access database on the computer that is running SQL Server.)

EXEC sp_addlinkedserver
@server = 'AccessDB',
@provider = 'Microsoft.Jet.OLEDB.4.0',
@srvproduct = 'OLE DB Provider for Jet',
@datasrc = 'C:\Path\to\Access\Database.mdb'
GO

Test the linked server by selecting from an existing table in the Access database (this is optional). For example, if there is a table called "Table1" in the Access database, the following SQL script will select all data from it.

SELECT *
FROM OPENQUERY(AccessDB, 'SELECT * FROM Table1')

If necessary, you can remove the linked server using the sp_dropserver procedure.

EXECUTE sp_dropserver 'AccessDB'

Importing Data into the Access Database 将数据导入Access数据库

To import data into an existing table in the Access database, the INSERT OPENQUERY(...) syntax must be used. For example, if the SQL Server table is called SrvTable and the Access table is called Table1 and both tables contain the same columns, the following SQL script will copy the data from the SQL Server table into the Access table.

INSERT OPENQUERY(AccessDB, 'SELECT * FROM Table1')
SELECT * FROM SrvTable

If necessary, you can also update existing data in the Access database from the SQL Server using the UPDATE OPENQUERY syntax, as seen below.

UPDATE OPENQUERY(AccessDB, 'SELECT * FROM Table1')
SET Col1 = 'Testing...'

You can also delete data from the Access database using the DELETE OPENQUERY syntax. (Note the escaped single-quotes [''] inside the OPENQUERY statement.)

DELETE OPENQUERY(AccessDB, 'SELECT * FROM Table1 WHERE Col1 = ''Testing...''')

Finally, create a stored procedure that utilizes any combination of the OPENQUERY statements to accomplish your data import task. For example, to copy all records from the SrvTable to Table1 the Access database, then update Col1 to "Testing...", use the following SQL script.

CREATE PROCEDURE CopyToTable1 AS BEGIN
INSERT OPENQUERY(AccessDB, 'SELECT * FROM Table1')
SELECT * FROM SrvTable

UPDATE OPENQUERY(AccessDB, 'SELECT * FROM Table1')
SET Col1 = 'Testing...'
END

Finally, consider this option.... 最后,考虑此选项。

Follow These Steps: (Video also available)
1. Open control Panel
2. open “ODBC” from “Administrative tools”
3. Create a Data Source for the SQL database from which you need to import
4. open MS Access, Select “External Data” Tab
5. Select “ODBC Database”
6. In “Machine Data source” tab, select the Data Source you have created.
7. Then you will find an Import Object window, Select the tables that you need to Import
8. Then Press “OK”

If you prefer to do using ADO : http://www.vb-helper.com/howto_ado_import_sql_server.html

Else use this code:

SELECT * FROM [ODBC;Driver=SQL Server Native Client 10.0;SERVER=Your_Server_Name;DATABASE=DB_Name;UID=User_Login;PWD=Your_Password] 

https://support.office.com/en-us/article/Import-or-link-to-SQL-Server-data-a5a3b4eb-57b9-45a0-b732-77bc6089b84e?CorrelationId=7b7e6535-5ac3-4fd1-9766-d7ebfa151046&ui=en-US&rs=en-US&ad=US&ocmsassetID=HA010200494#BM1 https://support.office.com/zh-CN/article/Import-or-link-to-SQL-Server-data-a5a3b4eb-57b9-45a0-b732-77bc6089b84e?CorrelationId=7b7e6535-5ac3-4fd1-9766- d7ebfa151046&ui = zh-CN&rs = zh-CN&ad = US&ocmsassetID = HA010200494#BM1

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

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