Loading Multiple Excel files dynamically

  • This is my code:

    Set @ID = '11'

    Set @Dest = C:\data.xls

    INSERT INTO OPENROWSET('Microsoft.Jet.OLEDB.4.0',

    'Excel 8.0; Database=@Destination ;',

    'Select tab1,tab2 FROM [Sheet1$]')

    select tab1,tab2 from dbo.mytable

    where ID = @ID

    )

    I need to pass this @Dest, @ID

    dynamically since I have multiple files to load.

    Iam getting error when I do this

    Is this Possible..?

    Thanks for your help...!!

  • From BOL at

    ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/f47eda43-33aa-454d-840a-bb15a031ca17.htm

    OPENROWSET does not accept variables for its arguments.

    You could use dynamic sql to perform the import something like:

    DECLARE @sql NVARCHAR(500)

    Set @sql = 'INSERT INTO OPENROWSET(''Microsoft.Jet.OLEDB.4.0',

    'Excel 8.0; Database=' + @Dest + ' ;',

    'Select tab1,tab2 FROM [Sheet1$]')

    'select tab1,tab2 from dbo.mytable

    where ID = @ID)'

    EXEC @sql

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply