2011-03-14 89 views
1

我想知道是否有一種方法,使這個充滿活力的SP的非動態SP將轉換爲動態存儲過程

CREATE PROCEDURE [dbo].[GetPeople] 
    (
     @id varchar(8) = NULL, 
     @lastName varchar(50) = NULL, 
     @firstName varchar(50) = NULL, 
     @birthdate date = NULL, 
     @ssn varchar(9) = NULL, 
     @driversLicense varchar(12) = NULL, 
     @gender varchar(1) = NULL, 

     @addressNumber varchar(8) = NULL, 
     @addressFraction varchar(3) = NULL, 
     @addressDirection varchar(3) = NULL, 
     @street varchar(45) = NULL, 
     @addressSuffix varchar(4) = NULL, 
     @addressPostDirection varchar(2) = NULL, 
     @addressUnitType varchar(6) = NULL, 
     @addressUnit varchar(8) = NULL, 
     @city varchar(22) = NULL, 
     @zip varchar(5) = NULL, 
     @addressStatus varchar(1) = NULL 

    ) 
AS 
    SET NOCOUNT ON 

    DECLARE @q nvarchar(4000), 
      @paramlist nvarchar(4000) 

    SELECT @q = ' SELECT * FROM People WHERE 1 = 1' 

    IF @certnum <> '' 
     SELECT @q = @q + ' AND ID = ''' + @id + '''' 
    ELSE 
    BEGIN 
     IF @lastName <> ''           
      SELECT @q = @q + ' AND lastname like ''' + @lastName + '%'''  
     IF @firstName <> ''           
      SELECT @q = @q + ' AND firstname like ''' + @firstName + '%''' 
     IF @birthdate is not null         
      SELECT @q = @q + ' AND birthdate = ''' + cast(@birthdate as varchar(10)) + '''' 
     IF @ssn <> ''         
      SELECT @q = @q + ' AND ssnum = ''' + @ssn + ''''  
     IF @driversLicense <> ''         
      SELECT @q = @q + ' AND drivers_license = ''' + @driversLicense + ''''  
     IF @gender <> ''         
      SELECT @q = @q + ' AND sex = ''' + @gender + '''' 
     IF @street <> ''           
      SELECT @q = @q + ' AND addr_str like ''' + @street + '%''' 
     IF @city <> ''           
      SELECT @q = @q + ' AND cityname like ''' + @city + '%'''  
     IF @zip <> ''           
      SELECT @q = @q + ' AND addr_zip like ''' + @zip + '%''' 
     IF @addressNumber <> ''           
      SELECT @q = @q + ' AND ltrim(rtrim(addr_num)) = ''' + @addressNumber + '''' 

    END 


    SELECT @q = @q + ' ORDER BY lastname, firstname' 


    SELECT @paramlist = ' 
     @id varchar(8) = NULL, 
     @lastName varchar(50) = NULL, 
     @firstName varchar(50) = NULL, 
     @birthdate date = NULL, 
     @ssn varchar(9) = NULL, 
     @driversLicense varchar(12) = NULL, 
     @gender varchar(1) = NULL, 

     @addressNumber varchar(8) = NULL, 
     @addressFraction varchar(3) = NULL, 
     @addressDirection varchar(3) = NULL, 
     @street varchar(45) = NULL, 
     @addressSuffix varchar(4) = NULL, 
     @addressPostDirection varchar(2) = NULL, 
     @addressUnitType varchar(6) = NULL, 
     @addressUnit varchar(8) = NULL, 
     @city varchar(22) = NULL, 
     @zip varchar(5) = NULL, 
     @addressStatus varchar(1) = NULL 
' 

    PRINT @q 
    EXEC sp_executesql @q, @paramlist, 
     @id, 

     @lastName, 
     @firstName, 
     @birthdate, 
     @ssn, 
     @driversLicense, 
     @gender, 

     @addressNumber, 
     @addressFraction, 
     @addressDirection, 
     @street, 
     @addressSuffix, 
     @addressPostDirection, 
     @addressUnitType, 
     @addressUnit, 
     @city, 
     @zip, 
     @addressStatus 

感謝所有幫助

回答

2

你可以轉換,但你不得不代碼這比你的效率低​​。爲什麼要這麼做?這種類型的搜索過程是動態SQL是最好的方法的少數幾個地方之一。

+0

我希望它會加快查詢,但如果效率較低,那麼我會遠離轉換它。謝謝! – zSynopsis 2011-03-14 19:12:17

+0

您可以檢查正在使用的索引。 – HLGEM 2011-03-14 19:15:02