The difference between openrowset and opendatasource in SQL SERVER

How to open OpenRowset/OpenDatasource

    OpenRowset and OpenDatasource are blocked by default for security reasons. If it is not turned on, the following error message will appear:
    SQL Server has blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component has been turned off as part of the server's security configuration. A system administrator can enable 'Ad Hoc Distributed Queries' by using sp_configure. For more information on enabling 'Ad Hoc Distributed Queries', see "Surface Surface Configuration" in SQL Server Books Online.


   enable:

exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure


    closure:

exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure


 

openrowset

Query: ok

select * from openrowset('Microsoft.Jet.OLEDB.4.0', 'Excel 5.0;Database=c:/test.xls;User ID=;Password=;', 'select * from [sheet1$]')

or: ok

select * from openrowset('Microsoft.Jet.OLEDB.4.0', 'Excel 5.0;Database=c:/test.xls;User ID=;Password=;', [sheet1$])

Insert: ok

insert openrowset('Microsoft.Jet.OLEDB.4.0', 'Excel 5.0;Database=c:/test.xls;User ID=;Password=;', 'select * from [sheet1$]') select '109','ccc','202'

update: ok

update openrowset('Microsoft.Jet.OLEDB.4.0', 'Excel 5.0;Database=c:/test.xls;User ID=;Password=;', 'select * from [sheet1$]') set  a2='bbb',a3=345 where a1= 101

delete:

not support

opendatasource

Query: ok

SELECT * FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0','Data Source="c:/test.xls";Extended Properties="Excel 8.0;HDR=yes;IMEX=2;"')...[sheet1$]

Insert: ok

insert  OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0','Data Source=c:/test.xls; Extended Properties="Excel 8.0;HDR=yes;IMEX=2;"')...[sheet1$] select '1','2','3'

update: ok

update  OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0','Data Source=c:/test.xls;Extended Properties="Excel 8.0;HDR=yes;IMEX=2;"')...[sheet1$] set a2='111',a3='222' where a2='333'

Not supported for numeric fields

delete:

not support

1. The query result has a lot to do with the format of the EXCEL cell;

2. "/" and "\" in the path have the same meaning;

3. For worksheets containing special characters such as '-' in Excel, it cannot be supported in OPENDATASOURCE anyway, and openrowset can solve this problem

pay attention:

Extended Properties='Excel 8.0;HDR=yes;IMEX=1'

A: HDR ( HeaDer Row ) setting

If the specified value is Yes, it means that the first row of the worksheet in the Excel file is the column name

If the specified value is No, it means that the first row of the worksheet in the Excel file is the data, and there is no column name

B: IMEX ( IMport EXport mode ) setting

IMEX has three modes, and the read and write behaviors caused by each are also different, which will be described later:

0 is Export mode is "export mode", the Excel file opened in this mode can only be used for "writing".

1 is Import mode is "import mode", the Excel file opened in this mode can only be used for "reading" purposes.

2 is Linked mode (full update capabilities) is "linked mode", the Excel file opened in this mode can support both "read" and "write" purposes.

Guess you like

Origin blog.csdn.net/simon4055/article/details/130088946