一篇拼SQL字符串的语句

set  ANSI_NULLS  ON
set  QUOTED_IDENTIFIER  ON
go



ALTER      PROCEDURE   [ dbo ] . [ T_Consultation_Select ]  
    
@ConsultationStyleID   TINYINT ,
    
@AnswerUserID   VARCHAR ( 30 ),
    
@IncomeCallBeginTime   [ DATETIME ] ,
    
@IncomeCallEndTime   [ DATETIME ]
    
AS
    
BEGIN
        
DECLARE   @whereStr   VARCHAR ( 8000 ),
        
@count   INT
        
SET   @count = 0
        
SET   @whereStr = ''
        
IF    @ConsultationStyleID   IS   NOT   NULL
          
BEGIN
               
IF ( @count > 0 )
                
BEGIN
                 
SELECT   @whereStr = @whereStr + '     AND T_Consultation.ConsultationStyleID  = ' + Convert ( Varchar ( 10 ), @ConsultationStyleID )
             
END
        
ELSE
             
BEGIN
                 
SELECT   @whereStr = @whereStr + '   T_Consultation.ConsultationStyleID = ' + Convert ( Varchar ( 10 ), @ConsultationStyleID )
               
END
          
SET    @count = @count   + 1
           
END
           
IF    @AnswerUserID   IS   NOT   NULL
          
BEGIN
               
IF ( @count > 0 )
                
BEGIN
                 
SELECT   @whereStr = @whereStr + '     AND T_Consultation.AnswerUserID  = ''' + Convert ( Varchar ( 10 ), @AnswerUserID ) ''
             
END
        
ELSE
             
BEGIN
                 
SELECT   @whereStr = @whereStr + '   T_Consultation.AnswerUserID = ''' + Convert ( Varchar ( 10 ), @AnswerUserID ) ''
               
END
          
SET    @count = @count   + 1
           
END
            
IF   @IncomeCallBeginTime   IS   NOT   NULL
                
BEGIN
                    
IF ( @count > 0 )
                        
BEGIN
                             
SELECT   @whereStr = @whereStr + '  AND  T_Consultation.CreateTime >= '' + CONVERT(VARCHAR(10),@IncomeCallBeginTime) '''
                        
END
            
ELSE
                    
BEGIN
                       
SELECT   @whereStr = @whereStr + '   T_Consultation.CreateTime >= ''  + CONVERT(VARCHAR(10),@IncomeCallBeginTime) '''
                    
END         
                
END
             
IF   @IncomeCallEndTime   IS   NOT   NULL
                
BEGIN
                    
IF ( @count > 0 )
                        
BEGIN                                                                                                             
                             
SELECT   @whereStr = @whereStr + '  AND  T_Consultation.CreateTime <= ''  + CONVERT(VARCHAR(10),@IncomeCallEndTime) '''
                        
END
            
ELSE
                    
BEGIN
                       
SELECT   @whereStr = @whereStr + '   T_Consultation.CreateTime <= ''  + CONVERT(VARCHAR(10),@IncomeCallEndTime) '''
                    
END         
                
END
           
IF ( @count > 0 )
            
SELECT   @whereStr = ' SELECT [CreateTime]
  FROM T_Consultation WHERE T_Consultation.FollowUpMark=
' + ' 1 ' + '   And  '   +   @whereStr
   
ELSE
   
SELECT   @whereStr = ' SELECT ConsultationID,(SELECT ParaName FROM T_SysPara WHERE GroupID=14 AND ParaID=T_Consultation.ConsultationStyleID)AS ConsultationStyleID,
            [ConsultationContent],[ConsultationResults],(SELECT ParaName FROM T_SysPara WHERE GroupID=16 AND ParaID=T_Consultation.HaveOrderingIntention)AS HaveOrderingIntention,[OrderingIntentionContent],
  [CustomerID],[AnswerUserID],[CreateTime]
  FROM T_Consultation where  T_Consultation.FollowUpMark=
' + ' 1 '  
END
select   ( @whereStr )

你可能感兴趣的:(sql)