提取excel时出现语法错误

时间:2010-04-30 09:51:57

标签: sql-server-2005

Procedure USP_UploadTraining  
@filePath varchar(100)
AS  

BEGIN  

 Insert into Training_TRNS (EmpName,EmpJobFunction,EmpRegion,SpeCourse_ID,status,DueDate)  
 sCreate elect   F2+' '+F3 as Name,F4 as JobFunction, F6 as EVPRegion, F7 as CourseTitle,F8 as Status, convert(varchar,F9,101) as DateAcq   
 FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',  
 'Excel 8.0;Database='@filePath'', 'SELECT * FROM [Sheet1$]')  
 where f2!='First Name'  

END


it's give syntax error can anyone this modify.

1 个答案:

答案 0 :(得分:0)

你需要使用Sp_ExecuteSql,因为你必须在运行查询时替换@filePath

尝试以下查询

 Create Procedure USP_UploadTraining
@filePath varchar(100) AS

BEGIN
DECLARE @ParmDefinition nvarchar(500);
SET @ParmDefinition = N'@filePath1 varchar(100)';
DECLARE @SQLString nvarchar(max);

SET  @SQLString='Insert into Training_TRNS (EmpName,EmpJobFunction,EmpRegion,SpeCourse_ID,status,DueDate)
select F2+'' ''+F3 as Name,F4 as JobFunction, F6 as EVPRegion, F7 as CourseTitle,F8 as Status, convert(varchar,F9,101) as DateAcq
FROM OPENROWSET(''Microsoft.Jet.OLEDB.4.0'',
''Excel 8.0;Database= @filePath'', ''SELECT * FROM [Sheet1$]'')
where f2!=''First Name'''


EXECUTE sp_executesql
     @SQLString
    ,@ParmDefinition
    ,@filePath1 = @filePath
    ;


END