2012-07-27 90 views
0

我想将excel的动态URL传递给“OPENROWSET”。将excel的动态文件路径传递给“OPENROWSET”

注 - 我将excel文件的返回结果传递给光标。 我想通过文件路径“@excelpath”, 我已经尝试了很多方法,但它给出的语法错误。

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 

回答

3

您必须使用动态SQL构建您的查询,如this question所示。将查询中的数据插入永久表中可能最简单,然后将光标移动到永久表上。这样可以最大限度地减少需要动态使用的SQL数量。

+0

我觉得这是个好主意。 :) 谢谢 – Abhi 2012-07-27 16:29:51