• Use dynamic SQL to fix openrowset parameter and Insert xml files into database under given folder


    --SQL CODE TO CREATE SP

    Create procedure usp_ImportMultipleFiles2 @filepath nvarchar(500),
    @pattern nvarchar(100)
    as
    set quoted_identifier off

    -- Set query files under folder
    declare @sql nvarchar(1000)
    set @sql ='master.dbo.xp_cmdshell "dir '+@filepath+@pattern +' /b"'

    --Retrieve files into temp table #temp
    create table #x (name nvarchar(200))
    insert #x exec (@sql)
    delete from #x where name is NULL
    select identity(int,1,1) as ID, name into #temp from #x
    drop table #x

    declare @max1 int
    set @max1 =(select MAX(ID) from #temp)

    declare @count1 int
    set @count1 =0

    declare @filename nvarchar(100)
    declare @xml xml

    While @count1 <= @max1
    begin
    set @count1=@count1+1
    set @filename = @filepath +(select name from #temp where [id] = @count1)
    set @sql ='declare @xml xml
    set @xml =( select *
    from openrowset(bulk '''+@filename+''' ,single_blob) as xmldata
    )
    insert XMLData (RawXml) values (@xml)'

    exec sp_executesql @sql
    end

    drop table #temp

    --SAMPLE CODE TO CALL ABOVE SP

    exec usp_ImportMultipleFiles2 'D:\TEMP\','*.xml'

    --NOTE

    You can't parametrise or concatenate the parameters of openrowset It is constant values only.

    You'll have to use dynamic SQL 

  • 相关阅读:
    11.01T3 实数二分
    11.01T2 树状数组维护动态LIS
    11.1T1打表
    10.31T4 HAOI2010最长公共子序列 计数+容斥原理
    10.31T3 其他算法思想
    10.31T2 点双联通分量+预处理前缀+二分答案
    10.31T1 二分图
    10.30T3 换根
    10.30T2 二分+前缀和(后缀和)
    10.30T1 期望DP
  • 原文地址:https://www.cnblogs.com/mjgb/p/2483404.html
Copyright © 2020-2023  润新知