开发者

Getting error while export data from excel

开发者 https://www.devze.com 2023-01-26 16:37 出处:网络
I am getting following error ERROR:- Incorrect syntax near \'+\'. while executing following T-Sql DECLARE @DatabasePath VARCHAR(MAX)

I am getting following error

ERROR:- Incorrect syntax near '+'.

while executing following T-Sql

DECLARE @DatabasePath VARCHAR(MAX)
SET @DatabasePath = 'E:\ABC.xls'
开发者_高级运维
INSERT INTO [dbo].[Table_1]
SELECT  *
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
        'Excel 8.0;Database='+@DatabasePath+'',
        'SELECT * FROM [Sheet1$]') AS xlsTable 

Is there something wrong in T-Sql. Actually i want to create a stored procedure where i will pass excel sheet path in @DatabasePath input parameter.


I dont thing this will work for you.

From OPENROWSET (Transact-SQL)

'datasource'

Is a string constant that corresponds to a particular OLE DB data source.

Have you tried something like

DECLARE @DatabasePath VARCHAR(MAX) 
SET @DatabasePath = 'C:\tada.xlsx'

DECLARE @RowSetString VARCHAR(MAX)
SELECT @RowSetString = 'SELECT  * FROM OPENROWSET(''Microsoft.Jet.OLEDB.4.0'',''Excel 8.0;Database=' + @DatabasePath + ''',''SELECT * FROM [Sheet1$]'')'

SELECT @RowSetString

EXEC(@RowSetString)


You will need to use dynamic SQL:

DECLARE @DatabasePath VARCHAR(MAX)
SET @DatabasePath = 'E:\ABC.xls'

DECLARE @sql      nvarchar(MAX)
SET @sql = '
INSERT INTO [dbo].[Table_1]
SELECT  *
FROM OPENROWSET(''Microsoft.Jet.OLEDB.4.0'',
        ''Excel 8.0;Database=' + @DatabasePath + ',
        ''SELECT * FROM [Sheet1$]'') AS xlsTable'

EXEC sp_executesql @sql
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号