Sql Dynamic Query问题

时间:2011-10-08 17:07:26

标签: sql sql-server

我试图写一个动态的sql.following是我的存储过程,并且它有一个错误,将SQL查询附加到@SQLSTATEMENT可以帮我解决这个问题吗?它在+标记

下显示错误
  

EX - SET @SQLSTATEMENT + ' AND TA.AreaID =' + @ AreaID +' '

ALTER PROCEDURE spGetOverDueDocuments
@AreaID INT =NULL,
@DepartmentID INT= NULL,
@ABSID INT =NULL,
@DisciplineID INT = NULL,
@CPOID INT = NULL,
@DocumentTypeID INT = NULL,
@DueType VaRCHAR(20) = NULL
AS
BEGIN
DECLARE @SQLSTATEMENT VARCHAR(MAX)


SELECT @SQLSTATEMENT = 'SELECT * '+ 
'FROM tblActionHeader AH INNER JOIN '+
'tblDocumentRevisionActionHeader DAH ON AH.ActionHeaderID=DAH.ActionHeaderID INNER JOIN '+
'tblDocumentRevision DR ON DAH.DocumentRevisionID=DR.DocumentRevisionID INNER JOIN '+
'tblDocumentHeader DH ON DR.DocumentHeaderID=DH.DocumentHeaderID INNER JOIN tblABS TB ON DH.ABSID=TB.ABSID ' +
'INNER JOIN tblArea TA ON TA.AreaID=TB.AreaNo INNER JOIN  tblContractPODocumentHeader CDH ON '+
' CDH.DocumentHeaderID=DH.DocumentHeaderID INNER JOIN tblDiscipline DC ON '+
' DH.DisciplineID=DC.DisciplineID INNER JOIN tblContractPO CPO ON CPO.CPOID=DH.CPOID INNER JOIN tblDocumentType DT '+
' ON DT.DocumentTypeID=DH.DocumentTypeID '+
'WHERE AH.ActionTypeID=4 '
IF @AreaID<>'0'
BEGIN
SET @SQLSTATEMENT + ' AND TA.AreaID='+@AreaID+' '
END

IF @ABSID<>'0' 
BEGIN
SET @SQLSTATEMENT + ' AND TB.ABSID='+@ABSID+' '
END

IF @DisciplineID<>'0' 
BEGIN
SET @SQLSTATEMENT + ' AND DC.DisciplineID='+@DisciplineID+' '
END

IF @CPOID<>'0' 
BEGIN
SET @SQLSTATEMENT + ' AND CPO.CPOID='+@CPOID+' '
END

IF @DocumentTypeID<>'0' 
BEGIN
SET @SQLSTATEMENT + ' AND DT.DocumentTypeID='+@DocumentTypeID+' '
END

EXEC(@SQLSTATEMENT)

END

3 个答案:

答案 0 :(得分:2)

尝试

SET @SQLSTATEMENT = @SQLSTATEMENT  +  <rest>

   SET @SQLSTATEMENT = @SQLSTATEMENT + ' AND TA.AreaID='+STR(@AreaID)+' '

答案 1 :(得分:2)

@Area ID被声明为INT,您需要将其投放到varchar才能添加。

试试这个:

SET @SQLSTATEMENT + ' AND TA.AreaID='+cast(@AreaID as varchar(30))+' '

答案 2 :(得分:0)

另一种选择是使用不需要动态SQL的查询

一种可能的方法是:

SELECT * 
FROM tblActionHeader AH 
INNER JOIN tblDocumentRevisionActionHeader DAH ON AH.ActionHeaderID=DAH.ActionHeaderID 
INNER JOIN tblDocumentRevision DR ON DAH.DocumentRevisionID=DR.DocumentRevisionID 
INNER JOIN tblDocumentHeader DH ON DR.DocumentHeaderID=DH.DocumentHeaderID 
INNER JOIN tblABS TB ON DH.ABSID=TB.ABSID  
INNER JOIN tblArea TA ON TA.AreaID=TB.AreaNo 
INNER JOIN  tblContractPODocumentHeader CDH ON CDH.DocumentHeaderID=DH.DocumentHeaderID 
INNER JOIN tblDiscipline DC ON DH.DisciplineID=DC.DisciplineID 
INNER JOIN tblContractPO CPO ON CPO.CPOID=DH.CPOID 
INNER JOIN tblDocumentType DT ON DT.DocumentTypeID=DH.DocumentTypeID 
WHERE TA.AreaID = (CASE WHEN @AreaID<>'0' THEN @AreaID ELSE TA.AreaId END) 
AND TA.ABSID = (CASE WHEN @ABSID<>'0' THEN @ABSID ELSE TA.ABSID END) 
AND DC.DisciplineID = (CASE WHEN @DisciplineID<>'0' THEN @DisciplineID ELSE DC.DisciplineID END) 
AND CPO.CPOID = (CASE WHEN @CPOID<>'0' THEN @CPOID ELSE TA.ABSID END) 
AND DT.DocumentTypeID = (CASE WHEN @DocumentTypeID<>'0' THEN @DocumentTypeID ELSE TA.ABSID END) 

在此查询中,现在将动态添加的搜索谓词设置为与自身进行比较,除非指定了其他值。只要它们都不为null,这将起作用(因为测试NULL = NULL或NULL = x将始终为false)。

这可能会导致更好的查询,可以缓存和重用。需要注意的一件事是参数嗅探:如果你第一次运行这个SP并且所有参数都是0,那么优化器将选择(和缓存)一个非常不同的执行计划,而不是大多数参数非零的时候。