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

时间:2017-04-14 09:54:44

标签: sql sql-server

我已经从使用Sql Server导出/导入管理器的方式将sql表导出到ms访问文件,但这不是动态使用的,有没有办法在存储过程中执行此操作? 就像执行查询并将其保存为ms访问文件中的表一样,如导出/导入管理器,但只是在代码中?

提前致谢!

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我想你可以试试几件事。如果您有权访问SSIS,这肯定会为您完成工作。您可以使用“导入/导出”向导来帮助您完成任务。

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

配置并测试链接服务器

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'

将数据导入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

最后,请考虑这个选项......

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