When I want to know if a stored procedure or views references a given name (table, view or column), I need a version of the object definition stripped of all comments.
Is this possible by a SQL query in an usual DBMS system?
Edit:
For SQL Server I found this solution using [Microsoft.Data.Schema.ScriptDom.Sql]. Here is the minimal PowerShell Code:
$sql = @'
select * from PowerShell -- a comment
where
psRefnr = 1
'@
$options = new-object Microsoft.Data.Schema.ScriptDom.Sql.SqlScriptGeneratorOptions
$sr = new-Object System.IO.StringReader($sql)
$sg = new-object Microsoft.Data.Schema.ScriptDom.Sql.Sql100ScriptGenerator($options)
$parser = new-object Microsoft.Data.Schema.ScriptDom.Sql.TSQL100parser($true)
$errors = $null
$fragment = $parser.Parse($sr,([ref]$errors))
$out = $null
$sg.GenerateScript($fragment,([ref]$out))
$out
The advantage of this approach is that it uses the official Microsoft parser.
3rd Edit:
The basic methods to get the procedure definitions in T-SQL are as mentioned [here][1] are
- sp_help proc_name
- SELECT [text] FROM sys.syscomments WHERE OBJECT_NAME(id) = 'proc_name'
- SELECT OBJECT_DEFINITION(OBJECT_ID('proc_name'))
The second and third option return columns of type varchar(max) which can be used in LIKE clauses.
Historical Note:
When '*=' and '=*' became obsolete, syscomments returned the definition in chunks of 255 characters and the two could be placed into different rows.
But the second problem isn't resolved yet. Some faltering developers tended to keep old code as C - style comments, making scanning for code containing deprecated syntax difficult.