pass dynamic file path of excel to “OPENROWSET”

♀尐吖头ヾ 提交于 2019-12-20 07:13:06

问题


I want pass dynamic URL of excel to "OPENROWSET".

NOTE - I am passing returned result of excel file to cursor. I want to pass file path to "@excelpath", I have tried many ways but its giving syntax error.

 ALTER procedure [dbo].[import_excel]
(
    @excelpath as nvarchar(max)
)
as      
begin
  set nocount on 
  DECLARE insert_cursor CURSOR FOR 
  select * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 
    'Excel 12.0;Database=C:\memberdata.xlsx', [Sheet1$])
  OPEN insert_cursor;
  FETCH NEXT FROM insert_cursor
      INTO @id_number, @memberName

  WHILE @@FETCH_STATUS = 0
  BEGIN
  -- body of cursor
      FETCH NEXT FROM insert_cursor
      INTO @id_number, @memberName
  END
  CLOSE insert_cursor;
  DEALLOCATE insert_cursor;
END

回答1:


You have to build your query using dynamic SQL, as shown in this question. It would probably be simplest for you to insert the data from your query into a permanent table, then run the cursor over the permanent table. In that way you minimize the amount of SQL you need to work with dynamically.



来源:https://stackoverflow.com/questions/11683338/pass-dynamic-file-path-of-excel-to-openrowset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!