2017-04-14 180 views
0

我听说已经从使用Sql Server导出/导入管理器将sql表导出到ms访问文件的方式,但这不是动态使用的,是一种在存储过程中做到这一点的方法? 像执行一个查询,并将其保存为ms访问文件中的表,如导出/导入管理器,但只是在代码中?使用存储过程将SQL表/数据导出到MS Access

在此先感谢!

回答

1

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

+0

我需要动态地改变Select语句,有没有办法在保存后通过代码更改.dtsx的查询? –

+0

您可以在从SP调用SSIS包的同时将变量中的select查询传递给SSIS包,并可以将该变量用作SSIS源中的命令。您也可以在SSIS中创建自定义任务来执行ETL任务。 – Kapil

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

+0

您好,先生,谢谢您的回答,但有没有办法完全导入表格?所以与colums等 –

相关问题