执行动态SQL

时间:2015-03-18 06:52:37

标签: sql-server sql-server-2008 dynamic-sql

我正在使用动态SQL来执行查询。

为此,我创建了这个存储过程:

CREATE PROCEDURE MySP    
(    
   @StateId CHAR(3),    
   @DeptId VARCHAR(15),    
   @Query NVARCHAR(MAX)    
)    
AS    
BEGIN   
  DECLARE @Where NVARCHAR(500);    
  DECLARE @FinalQuery NVARCHAR(MAX)='';    

  SET @Where=' where emppersonal.stateid=Cast('''+@StateId+''' AS INT) AND emppersonal.deptid=LTRIM(RTRIM('''+@DeptId+'''))';    

  SET @FinalQuery = 'SELECT '+@Query+' '+@Where

  EXEC(@FinalQuery) 
END 

我用这些参数执行了该存储过程:

exec MySP '2','HPD0002',
'
 emppersonal.empfname,
 mdesigmast.designame AS CurrentDesignation,
 ISNULL(CONVERT(varchar, pisempprofessional.appdate, 103),'') AS appdate,
 IDPT.deptname AS InitialJoinDept, 
 MO1.officeName,pisempprofessional.apporder, DSG.designame AS Designation, 
 mClassMaster.Name AS ClassName,
 mpisrecruitmentmode.Name AS RecruitmentMode,
 pisempprofessional.DesigName 

 FROM emppersonal 
 LEFT JOIN pisempprofessional ON emppersonal.stateid=pisempprofessional.stateid AND emppersonal.deptid=pisempprofessional.deptid And emppersonal.empcd=pisempprofessional.empcd 

 LEFT JOIN mdesigmast ON emppersonal.stateid=mdesigmast.stateid AND emppersonal.currdesig=mdesigmast.desigcode 

 LEFT JOIN mdeptmaster IDPT ON pisempprofessional.stateid=IDPT.stateID AND pisempprofessional.InitialJoiningdeptid=IDPT.deptid 
 LEFT JOIN mofficemaster MO1 ON pisempprofessional.stateid=MO1.stateid AND pisempprofessional.officeid=MO1.officeid 
 LEFT JOIN mdesigmast DSG  ON pisempprofessional.StateID=DSG.StateID AND pisempprofessional.iphdesg=DSG.desigcode 
 LEFT JOIN mClassMaster ON pisempprofessional.iphclass=mClassMaster.ID  
 LEFT JOIN mpisrecruitmentmode ON  pisempprofessional.modeofrec=mpisrecruitmentmode.id

'

当我执行该程序时,我收到此错误:

  

Msg 102,Level 15,State 1,Line 19
  ' 2'附近的语法不正确。

     

Msg 105,Level 15,State 1,Line 19
  字符串后面的未闭合引号')'。

如何解决上述错误?

由于

2 个答案:

答案 0 :(得分:2)

就像你在其他地方一样,你需要使用双撇号:

ISNULL(CONVERT(varchar, pisempprofessional.appdate, 103),'''')

应该这样做

  

撇号或单引号是SQL中的一个特殊字符,用于指定字符串数据的开头和结尾。这意味着要将它用作文字字符串数据的一部分,您需要转义特殊字符。通过单引号,通常可以通过加倍报价来实现。

答案 1 :(得分:0)

与提及的@Mackan一样

  

撇号或单引号是SQL中的一个特殊字符   指定字符串数据的开头和结尾。这意味着要使用   它作为你的文字字符串数据的一部分,你需要逃避特殊   字符。通过单引号,这通常由   加倍你的报价。

从{<1}}改变

Param

exec MySP '2','HPD0002',
'emppersonal.empfname,
 mdesigmast.designame AS CurrentDesignation,
 /*Change the below line*/
 ISNULL(CONVERT(varchar, pisempprofessional.appdate, 103),'') AS appdate,
 IDPT.deptname AS InitialJoinDept, 
 MO1.officeName,pisempprofessional.apporder, DSG.designame AS Designation, 
 mClassMaster.Name AS ClassName,
 mpisrecruitmentmode.Name AS RecruitmentMode,
 pisempprofessional.DesigName 

 FROM emppersonal 
 LEFT JOIN pisempprofessional ON emppersonal.stateid=pisempprofessional.stateid AND emppersonal.deptid=pisempprofessional.deptid And emppersonal.empcd=pisempprofessional.empcd 

 LEFT JOIN mdesigmast ON emppersonal.stateid=mdesigmast.stateid AND emppersonal.currdesig=mdesigmast.desigcode 

 LEFT JOIN mdeptmaster IDPT ON pisempprofessional.stateid=IDPT.stateID AND pisempprofessional.InitialJoiningdeptid=IDPT.deptid 
 LEFT JOIN mofficemaster MO1 ON pisempprofessional.stateid=MO1.stateid AND pisempprofessional.officeid=MO1.officeid 
 LEFT JOIN mdesigmast DSG  ON pisempprofessional.StateID=DSG.StateID AND pisempprofessional.iphdesg=DSG.desigcode 
 LEFT JOIN mClassMaster ON pisempprofessional.iphclass=mClassMaster.ID  
 LEFT JOIN mpisrecruitmentmode ON  pisempprofessional.modeofrec=mpisrecruitmentmode.id'