Database - Multiple ways to import Excel data

1. Import Excel data from SQL Server

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
Next, click NEXT until it is completed.
insert image description here
At this time, the EXCEL data is imported into SQL Server:
insert image description here

insert image description here

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:
insert image description here
right-click the PTYPES table, select the first 2002 rows to edit:
insert image description here
directly right-click and paste:
insert image description here
At this point, the data can be imported directly:
insert image description here

3. Import Excel data using Microsoft.ACE.OLEDB

First install the package:
insert image description here
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',[类别$]);

insert image description here

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,'细粮')

insert image description here
insert image description here

5. Manually add multiple rows of data

	INSERT INTO PTYPES (PTID,PTNAME) VALUES
	(102,'有机蔬菜'),(103,'有机粮食'),(104,'有机水果')

insert image description here

5. Solve the import failure caused by type mismatch

insert image description here

insert image description here
It was found that the following import failed: The
insert image description here
reason is that the types of suppliers and categories are defined INT, but in excelthem they are of type string, so the import fails:
insert image description here

insert image description here
Workaround:
Ignore both. Accordingly, both should be set to allow null values ​​in the database.
insert image description here
insert image description here

insert image description here
At this point, the import is successful:
insert image description here

Guess you like

Origin blog.csdn.net/wxfighting/article/details/123878353