简体   繁体   中英

Search Stored procedures and return those where search param exist in string ('')

I'm using the below code to get all the stored procedures that contains my word LO .

Problem is that currently it also returns all procedures where tablename or column names contains LO

SELECT name
FROM   sys.procedures
WHERE  Object_definition(object_id) LIKE '%LO%'

Is there some way to restrict it to only search for LO inside a string '' like the example below.

'Please contact LO and return the ticket number'

Maybe a regex would work?

I figures it out. I could be done like:

SELECT name
FROM   sys.procedures
WHERE  Object_definition(object_id) LIKE '%''%LO%''%'

OR

LIKE '%''LO''%'

You could use the query below to identify source lines of procedures that contain the specified text, although it may return false positives in some cases.

SELECT 
      name ProcName
    , value as ProcSourceLine
FROM   sys.procedures
CROSS APPLY STRING_SPLIT(OBJECT_DEFINITION(object_id),CHAR(10))
WHERE  value LIKE '%''%LO%''%';

The most robust way to accomplish this is with the T-SQL script DOM . Below is a PowerShell example that uses the assembly from an existing SSMS installation. Microsoft.SqlServer.TransactSql.ScriptDom.dll is also available as part of the DacFx NuGet package .

$scriptDomAssemblyPath = "C:\Program Files (x86)\Microsoft SQL Server Management Studio 18\Common7\IDE\Extensions\Application\Microsoft.SqlServer.TransactSql.ScriptDom.dll"
$connectionString = "Data Source=.;Initial Catalog=YourDatabase;Integrated Security=SSPI"

try {

    Add-Type -Path $scriptDomAssemblyPath

    $parseErrors = New-Object System.Collections.Generic.List[Microsoft.SqlServer.TransactSql.ScriptDom.ParseError]
    $parser = New-Object Microsoft.SqlServer.TransactSql.ScriptDom.TSql150Parser($true)

    $connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
    $command = New-Object System.Data.SqlClient.SqlCommand(
@"
SELECT
      OBJECT_SCHEMA_NAME(object_id) AS ProcSchema
    , name ProcName
    , OBJECT_DEFINITION(object_id) as ProcDefinition
FROM   sys.procedures;
"@, $connection
    )

    $connection.Open()
    $reader = $command.ExecuteReader()
    while($reader.Read()) {

        $parseErrors.Clear()
        $scriptReader = New-Object System.IO.StringReader($reader["ProcDefinition"])
        $frament = $parser.Parse($scriptReader, [ref]$parseErrors)
        if($parseErrors.Count -gt 0) {
            # in case an existing proc has syntax errors
            Write-Host "$($parser.GetType().Name): $($parseErrors.Count) parsing error(s): $(($parseErrors | ConvertTo-Json))" -ForegroundColor Yellow
            continue
        }

        foreach($token in $frament.ScriptTokenStream) {
            if($token.TokenType -in ([Microsoft.SqlServer.TransactSql.ScriptDom.TSqlTokenType]::AsciiStringLiteral, [Microsoft.SqlServer.TransactSql.ScriptDom.TSqlTokenType]::UnicodeStringLiteral)) {
                if($token.Text.ToUpper().Contains("LO")) {
                    Write-Host "Found literal text in $($reader["ProcSchema"]).$($reader["ProcName"])"
                    break
                }
            }
        }

        $scriptReader.Dispose()
    }
    $connection.Close()
       
}
catch {
    throw
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM