2017-04-11 41 views
-1

我卡住了 - 我有這個漂亮的小文本搜索器我放在一起。我的存儲過程文本搜索是錯誤

我意識到我需要計算存儲過程存儲在單獨的文本塊中的lnies,所以我想我已經想通了一種方法來做到這一點。

但它沒有獲得存儲過程中搜索項的每個實例 - 我認爲這是相同的原因......搜索項分割在文本邊界上。

我已經看過如何更改我的查詢 - 而且我會空白。涉及改變它的技能超出了我的想象!

--Text searcher. 
DECLARE @searchString VARCHAR(255), 
     @doesNotContain VARCHAR(255), 
     @previewLength INTEGER, 
     @findStoredProcedures VARCHAR(3), 
     @findTableFunction VARCHAR(3), 
     @findScalerFunction VARCHAR(3), 
     @findTrigger VARCHAR(3), 
     @findView VARCHAR(3), 
     @findUserTable VARCHAR(3), 
     @onlyInName VARCHAR(3) 

--------------------------------------------------------  
-- Search criteria: 

SET @searchString = 'My search Term' 

SET @findStoredProcedures  = 'yes' 
SET @findTableFunction  = 'yes' 
SET @findScalerFunction  = 'yes' 
SET @findUserTable   = 'yes' 
SET @findTrigger    = 'yes' 
SET @findView     = 'yes' 

SET @doesNotContain = '' 
SET @previewLength = 30 

--------------------------------------------------------  

SELECT DISTINCT 
    ISNULL(
     (SELECT REPLACE(CONVERT(VARCHAR(20), (CAST(SUM(LEN(SC2.text)) AS MONEY)), 1), '.00', '') 
     FROM syscomments SC2 WHERE SC2.id = SO.id GROUP BY SC2.id) 
     , '') 
    AS [Object length] 
    , 
    SO.name AS [Object name] 
    , 
    CASE 
      WHEN SO.xtype = 'P' THEN 'Stored Procedure' 
      WHEN SO.xtype = 'TF' THEN 'Table Function' 
      WHEN SO.xtype = 'FN' THEN 'Scaler Function' 
      WHEN SO.xtype = 'U' THEN 'User Table' 
      WHEN SO.xtype = 'TR' THEN 'Trigger' 
      WHEN SO.xtype = 'V' THEN 'View' 
    END 
    + ISNULL((SELECT ' - ' + name FROM sysobjects WHERE id = SO.parent_obj), '') 
    AS [Object type] 
    , 
    ISNULL(SUBSTRING(SC.text, CHARINDEX(@searchString, SC.text) - @previewLength, @previewLength) + 
      SUBSTRING(SC.text, CHARINDEX(@searchString, SC.text), @previewLength + LEN(@searchString)) 
    , '') AS [Preview of code] 
    , 
    (SELECT 
     COALESCE(
      SUM(LEN(SC3.text) - LEN(REPLACE(SC3.text, CHAR(13), '')) + 1) + 4 
      + 
      (
      SELECT 
       (LEN(LEFT(SC4.text, CHARINDEX(@searchString, SC4.text))) - 
       LEN(REPLACE(LEFT(SC4.text, CHARINDEX(@searchString, SC4.text)), CHAR(13), ''))) 
      FROM syscomments SC4 
      WHERE 
       SC4.id = SO.id 
       AND SC4.colid = SC.colid 
      ) 
     , 
      SUM(LEN(SC3.text) - LEN(REPLACE(SC3.text, CHAR(13), '')) + 1) + 4 
     , 
      (
      SELECT 
       (LEN(LEFT(SC4.text, CHARINDEX(@searchString, SC4.text))) - 
       LEN(REPLACE(LEFT(SC4.text, CHARINDEX(@searchString, SC4.text)), CHAR(13), '')) + 1) 
      FROM syscomments SC4 
      WHERE 
       SC4.id = SO.id 
       AND SC4.colid = SC.colid 
      ) 
     ) 
    FROM syscomments SC3 
    WHERE 
     SC3.id = SO.id 
     AND SC3.colid < SC.colid 
    ) 
    AS [Line number] 

FROM sysobjects SO 

LEFT JOIN syscomments SC 
ON SO.id = SC.id 

WHERE 
    (
     (SO.type = 'P' AND @findStoredProcedures = 'yes') 
     OR 
     (SO.type = 'TF' AND @findTableFunction = 'yes') 
     OR 
     (SO.type = 'FN' AND @findScalerFunction = 'yes') 
     OR 
     (SO.type = 'TR' AND @findTrigger = 'yes') 
     OR 
     (SO.type = 'U' AND @findUserTable = 'yes') 
     OR 
     (SO.type = 'V' AND @findView = 'yes') 
    ) 
    AND SO.category = 0 
    AND 
    (
     (CHARINDEX(@searchString, SC.text) > 0 
     AND CHARINDEX(@doesNotContain, SC.text) = 0) 
     OR 
     (SO.type = 'U' 
     AND CHARINDEX(@searchString, SO.name) > 0 
     AND CHARINDEX(@doesNotContain, SO.name) = 0) 
    ) 
ORDER BY 
    [Object type], [Object name], [Line number] 

回答

0

你的where子句似乎有一個問題,這一點在這裏

(
    (CHARINDEX(@searchString, SC.text) > 0 
    AND CHARINDEX(@doesNotContain, SC.text) = 0) 
    --OR 
    --(SO.type = 'U' 
    --AND CHARINDEX(@searchString, SO.name) > 0 
    --AND CHARINDEX(@doesNotContain, SO.name) = 0)