SQL - 在 OpenRowSet 中动态 SQL 然后插入到临时 Table
SQL - Dynamic SQL inside OpenRowSet then Inserted into Temp Table
我正在尝试利用 Dynamic SQL 将文件目录解析为 OpenRowSet(@Database)。现在我有一个 Excel 文件的硬编码目录。最终结果将是我创建一个应用程序,它将获取用户的文件并导入到 SQL table。从那里我将合并和 Match/Not 匹配(工作正常)。这是拼图的最后一块。我不知道为什么错误信息是在“C:\WINDOWS\system32\”
中寻找我的文件
我当前的错误信息是:
OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" returned message "The Microsoft Access database engine could not find the object 'C:\WINDOWS\system32\ C:\Users\GrimRieber\Desktop\isi test.xlsx'. Make sure the object exists and that you spell its name and the path name correctly. If 'C:\WINDOWS\system32\ C:\Users\GrimRieber\Desktop\isi test.xlsx' is not a local object, check your network connection or contact the server administrator.".
Msg 7303, Level 16, State 1, Line 1
Cannot initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".
代码:
declare @Database varchar(max)
select @Database = 'C:\Users\GrimRieber\Desktop\isi test.xlsx'
declare @sql varchar(Max)
select @sql = 'SELECT * FROM OPENROWSET(
''Microsoft.ACE.OLEDB.12.0'',
''Excel 12.0; Database= ' + @Database + '; HDR=YES; IMEX=1'',
''SELECT [Vendor],[VendorName],[Material],[MaterialName],[Supplier Stock Num],[01],[02],[03],[04],[05],[06],[07],[08],[09],[10],[11],[12],[Year]FROM [Data$]''
)'
IF OBJECT_ID('tempdb.dbo.#TempScorecardInventorySold', 'U') IS NOT NULL
DROP TABLE #TempScorecardInventorySold;
CREATE TABLE #TempScorecardInventorySold
(
[Vendor] [varchar](50) NULL,
[VendorName] [varchar](50) NULL,
[Material] [varchar](50) NULL,
[MaterialName] [varchar](50) NULL,
[Supplier Stock Num] [varchar](50) NULL,
[01] [nVarchar](50) NULL,
[02] [nVarchar](50) NULL,
[03] [nVarchar](50) NULL,
[04] [nVarchar](50) NULL,
[05] [nVarchar](50) NULL,
[06] [nVarchar](50) NULL,
[07] [nVarchar](50) NULL,
[08] [nVarchar](50) NULL,
[09] [nVarchar](50) NULL,
[10] [nVarchar](50) NULL,
[11] [nVarchar](50) NULL,
[12] [nVarchar](50) NULL,
[Year] [Int] Null
) ON [PRIMARY];
INSERT INTO [dbo].#TempScorecardInventorySold ([Vendor],[VendorName],[Material],[MaterialName],[Supplier Stock Num],[01],[02],[03],[04],[05],[06],[07],[08],[09],[10],[11],[12],[Year])
EXECUTE(@sql)
虽然我无法重新创建您的文件路径问题(请务必仔细检查路径和文件扩展名中是否存在文件),但该设置中的 OPENROWSET()
应该在 SELECT
子句中定义字段开始时。最后一个参数应不带引号,指向工作表范围:
select @sql = 'SELECT [Vendor],[VendorName],[Material],[MaterialName],[Supplier Stock Num],
[01],[02],[03],[04],[05],[06],
[07],[08],[09],[10],[11],[12],[Year]
FROM OPENROWSET(''Microsoft.ACE.OLEDB.12.0'',
''Excel 12.0; Database= ' + @Database + '; HDR=YES; IMEX=1'', [Data$])';
或者,考虑 OPENDATASOURCE
:
select @sql = 'SELECT [Vendor],[VendorName],[Material],[MaterialName],[Supplier Stock Num],
[01],[02],[03],[04],[05],[06],
[07],[08],[09],[10],[11],[12],[Year]
FROM OPENDATASOURCE(''Microsoft.ACE.OLEDB.12.0'',
''Data Source=' + @Database + ';Extended Properties=Excel 12.0'')...Data$'
甚至驱动程序版本(字段可以在两个 SELECT
子句中的任何一个中)
select @sql = 'SELECT [Vendor],[VendorName],[Material],[MaterialName],[Supplier Stock Num],
[01],[02],[03],[04],[05],[06],
[07],[08],[09],[10],[11],[12],[Year]
FROM OPENROWSET(''MSDASQL'',
''Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};
DBQ=' + @Database + ''', ''SELECT * FROM [DATA$]'');
我正在尝试利用 Dynamic SQL 将文件目录解析为 OpenRowSet(@Database)。现在我有一个 Excel 文件的硬编码目录。最终结果将是我创建一个应用程序,它将获取用户的文件并导入到 SQL table。从那里我将合并和 Match/Not 匹配(工作正常)。这是拼图的最后一块。我不知道为什么错误信息是在“C:\WINDOWS\system32\”
中寻找我的文件我当前的错误信息是:
OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" returned message "The Microsoft Access database engine could not find the object 'C:\WINDOWS\system32\ C:\Users\GrimRieber\Desktop\isi test.xlsx'. Make sure the object exists and that you spell its name and the path name correctly. If 'C:\WINDOWS\system32\ C:\Users\GrimRieber\Desktop\isi test.xlsx' is not a local object, check your network connection or contact the server administrator.".
Msg 7303, Level 16, State 1, Line 1
Cannot initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".
代码:
declare @Database varchar(max)
select @Database = 'C:\Users\GrimRieber\Desktop\isi test.xlsx'
declare @sql varchar(Max)
select @sql = 'SELECT * FROM OPENROWSET(
''Microsoft.ACE.OLEDB.12.0'',
''Excel 12.0; Database= ' + @Database + '; HDR=YES; IMEX=1'',
''SELECT [Vendor],[VendorName],[Material],[MaterialName],[Supplier Stock Num],[01],[02],[03],[04],[05],[06],[07],[08],[09],[10],[11],[12],[Year]FROM [Data$]''
)'
IF OBJECT_ID('tempdb.dbo.#TempScorecardInventorySold', 'U') IS NOT NULL
DROP TABLE #TempScorecardInventorySold;
CREATE TABLE #TempScorecardInventorySold
(
[Vendor] [varchar](50) NULL,
[VendorName] [varchar](50) NULL,
[Material] [varchar](50) NULL,
[MaterialName] [varchar](50) NULL,
[Supplier Stock Num] [varchar](50) NULL,
[01] [nVarchar](50) NULL,
[02] [nVarchar](50) NULL,
[03] [nVarchar](50) NULL,
[04] [nVarchar](50) NULL,
[05] [nVarchar](50) NULL,
[06] [nVarchar](50) NULL,
[07] [nVarchar](50) NULL,
[08] [nVarchar](50) NULL,
[09] [nVarchar](50) NULL,
[10] [nVarchar](50) NULL,
[11] [nVarchar](50) NULL,
[12] [nVarchar](50) NULL,
[Year] [Int] Null
) ON [PRIMARY];
INSERT INTO [dbo].#TempScorecardInventorySold ([Vendor],[VendorName],[Material],[MaterialName],[Supplier Stock Num],[01],[02],[03],[04],[05],[06],[07],[08],[09],[10],[11],[12],[Year])
EXECUTE(@sql)
虽然我无法重新创建您的文件路径问题(请务必仔细检查路径和文件扩展名中是否存在文件),但该设置中的 OPENROWSET()
应该在 SELECT
子句中定义字段开始时。最后一个参数应不带引号,指向工作表范围:
select @sql = 'SELECT [Vendor],[VendorName],[Material],[MaterialName],[Supplier Stock Num],
[01],[02],[03],[04],[05],[06],
[07],[08],[09],[10],[11],[12],[Year]
FROM OPENROWSET(''Microsoft.ACE.OLEDB.12.0'',
''Excel 12.0; Database= ' + @Database + '; HDR=YES; IMEX=1'', [Data$])';
或者,考虑 OPENDATASOURCE
:
select @sql = 'SELECT [Vendor],[VendorName],[Material],[MaterialName],[Supplier Stock Num],
[01],[02],[03],[04],[05],[06],
[07],[08],[09],[10],[11],[12],[Year]
FROM OPENDATASOURCE(''Microsoft.ACE.OLEDB.12.0'',
''Data Source=' + @Database + ';Extended Properties=Excel 12.0'')...Data$'
甚至驱动程序版本(字段可以在两个 SELECT
子句中的任何一个中)
select @sql = 'SELECT [Vendor],[VendorName],[Material],[MaterialName],[Supplier Stock Num],
[01],[02],[03],[04],[05],[06],
[07],[08],[09],[10],[11],[12],[Year]
FROM OPENROWSET(''MSDASQL'',
''Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};
DBQ=' + @Database + ''', ''SELECT * FROM [DATA$]'');