将文件路径作为存储过程中的参数传递

时间:2017-05-20 07:22:33

标签: sql sql-server stored-procedures filepath

我有一个使用文件路径加载其数据的存储过程。当我在如下所示的过程中直接给出路径时,过程有效并且数据已加载。

CREATE PROCEDURE main.usp_importXML(@file VARCHAR)
AS
BEGIN
    DECLARE @xmlFile as XML

    SET @xmlFile = (SELECT CONVERT(XML,BulkColumn) as BulkColumn
    FROM OPENROWSET (BULK  'C:\Users\User\Desktop\people.xml', SINGLE_BLOB) AS t)
    INSERT INTO main.tempXML (name,surname,dob,gender,locality,postcode,job,salary,email)
    SELECT
     name = t.value ('name[1]', 'NVARCHAR(20)'),
     surname = t.value ('surname[1]', 'NVARCHAR(20)'),
     dob = t.value ('dob[1]', 'DATETIME'),
     gender = t.value ('gender[1]', 'CHAR'),
     locality = t.value ('locality[1]', 'NVARCHAR(20)'),
     postcode = t.value ('postcode[1]', 'NVARCHAR(20)'),
     job = t.value ('job[1]', 'NVARCHAR(50)'),
     salary = t.value ('salary[1]', 'INTEGER'),
     email = t.value ('email[1]', 'NVARCHAR(50)')
    FROM @xmlFile.nodes('/persons/person') AS xTable(t);
END
GO

但是当我尝试将路径作为参数传递时,如下所示的代码,会发生以下错误:

  

Msg 4860,Level 16,State 1,Procedure usp_importXML,Line 6 [Batch Start Line 38]
  无法批量加载。文件“+ @ file +”不存在。

代码:

CREATE PROCEDURE main.usp_importXML(@file VARCHAR)
AS
BEGIN
    DECLARE @xmlFile as XML

    SET @xmlFile = (SELECT CONVERT(XML,BulkColumn) as BulkColumn
    FROM OPENROWSET (BULK  '+@file+', SINGLE_BLOB) AS t)
    INSERT INTO main.tempXML (name, surname, dob, gender, locality, postcode, job, salary, email)
        SELECT
            name = t.value ('name[1]', 'NVARCHAR(20)'),
            surname = t.value ('surname[1]', 'NVARCHAR(20)'),
            dob = t.value ('dob[1]', 'DATETIME'),
            gender = t.value ('gender[1]', 'CHAR'),
            locality = t.value ('locality[1]', 'NVARCHAR(20)'),
            postcode = t.value ('postcode[1]', 'NVARCHAR(20)'),
            job = t.value ('job[1]', 'NVARCHAR(50)'),
            salary = t.value ('salary[1]', 'INTEGER'),
            email = t.value ('email[1]', 'NVARCHAR(50)')
        FROM 
            @xmlFile.nodes('/persons/person') AS xTable(t);
END
GO

EXEC main.usp_importXml [C:\Users\User\Desktop\people.xml]

main.tempXML表结构:

CREATE TABLE main.tempXML
(
    id       UNIQUEIDENTIFIER DEFAULT NEWID() PRIMARY KEY,
    [name]   VARCHAR(100), 
    surname  VARCHAR(100),
    dob      VARCHAR(100),
    gender   VARCHAR(10),
    locality VARCHAR(100),
    postcode VARCHAR(100),
    job      VARCHAR(100),
    salary   VARCHAR(20),
    email    VARCHAR(100)
);

people.xml数据结构:

<?xml version="1.0"?>
  -<persons>
    -<person>
      <name>Jacob</name>
      <surname>Naizer</surname>
      <dob>19840717</dob>
      <gender>M</gender>
      <locality>Mellieha</locality>
      <postcode>MEL876</postcode>  
      <job>JAVA Developer</job>
      <salary>20733</salary>
      <email>Jacob.Naizer1145@gmail.com</email>
  </person>
 </persons>

1 个答案:

答案 0 :(得分:4)

1. Make use of dynamic query. 
2. Increase filename variable size. 

Create PROCEDURE main.usp_importXML(@file VARCHAR(1000))

AS
BEGIN

Declare @Query varchar(8000)

SET @Query ='
    DECLARE @xmlFile as XML
    SET @xmlFile = (SELECT CONVERT(XML,BulkColumn) as BulkColumn
    FROM OPENROWSET (BULK  '''+@file+''', SINGLE_BLOB) AS t)
    INSERT INTO main.tempXML (name,surname,dob,gender,locality,postcode,job,salary,email)
    SELECT
     name = t.value (''name[1]'', ''NVARCHAR(20)''),
     surname = t.value (''surname[1]'', ''NVARCHAR(20)''),
     dob = t.value (''dob[1]'', ''DATETIME''),
     gender = t.value (''gender[1]'', ''CHAR''),
     locality = t.value (''locality[1]'', ''NVARCHAR(20)''),
     postcode = t.value (''postcode[1]'', ''NVARCHAR(20)''),
     job = t.value (''job[1]'', ''NVARCHAR(50)''),
     salary = t.value (''salary[1]'', ''INTEGER''),
     email = t.value (''email[1]'', ''NVARCHAR(50)'')
    FROM @xmlFile.nodes(''/persons/person'') AS xTable(t);'
   Print @Query
    exec(@Query)
END