Article directory
1. Import Excel data from SQL Server
Next, click NEXT until it is completed.
At this time, the EXCEL data is imported into SQL Server:
Two, tips to import Excel data
The trick is to use the copy-paste method directly:
Note: This method is only suitable for adding a small amount of data. If it is hundreds of thousands of rows of data, it cannot be imported in this way.
As shown in the figure, we directly copy the data in Excel:
right-click the PTYPES table, select the first 2002 rows to edit:
directly right-click and paste:
At this point, the data can be imported directly:
3. Import Excel data using Microsoft.ACE.OLEDB
First install the package:
execute the following code to import the data in the Excel table:
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;Database=D:\S072003DBS\食品销售数据\类别.XLSX',[类别$]);
If an error occurs, refer to the following code:
-- 报错一:SQLServer阻止了对组件'AdHocDistributedQueries'的STATEMENT'OpenRowset/OpenDatasource'的访问,
-- 因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用。
-- sp_configure启用'AdHocDistributedQueries'。
-- 解决方法:
-- 启用Ad Hoc Distributed Queries:
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
-- 为了安全,使用完成后,关闭Ad Hoc Distributed Queries
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure
-- 报错二:无法创建链接服务器“(null)”的 OLE DB 访问接口“Microsoft.ACE.OLEDB.12.0”的实例。
-- 解决方法:
--允许在进程中使用ACE.OLEDB.12
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1
--允许动态参数
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1
Fourth, manually add a row of data
INSERT INTO PTYPES (PTID,PTNAME) VALUES(101,'细粮')
5. Manually add multiple rows of data
INSERT INTO PTYPES (PTID,PTNAME) VALUES
(102,'有机蔬菜'),(103,'有机粮食'),(104,'有机水果')
5. Solve the import failure caused by type mismatch
It was found that the following import failed: The
reason is that the types of suppliers and categories are defined INT
, but in excel
them they are of type string, so the import fails:
Workaround:
Ignore both. Accordingly, both should be set to allow null values in the database.
At this point, the import is successful: