Morning all Riddle me this. Trying to find a way to parse sql scripts stored in a text field in a database. These sql statements are to determine system health and also perform intricate data manipulation at certain times during the month or after certain events. The statements can contain anything from simple select statements to large CRUDs containing recursion, sp calls, variables, temp tables, etc.
I have now cobbled together a solution that sort of does what I need I can parse the sql for syntax validity, check. I can check the sql if columns referenced exists. check. I can report back to the user any error that exists at "design time" i.e. syntactically
The problem is that it is not picking up if a table referenced does not exist. There are a bunch of cases that the parser will only pick up at "run time" when sql is executed.
My proposed solution would be (and this is where I need the communities feedback) 1. Create a .net SQL Connection, transaction and command object 2. Hook the Tx onto the connection and command 3. Execute the SQL in the command and address errors that come up. 4. Irrespective always roll back the Tx
DISCLAIMER: Please do not go on about best practice, I know it isn't, but need to provide usable feedback to the GUI and as far as I can determine, this is the only way to achieve the requirement.
I already use NOEXEC, PARSEONLY and Microsoft.SqlServer.TransactSql.ScriptDom, but again that is only for syntax not actual validity.
Your thoughts and feedback would be greatly appreciated.
Thank you
NOEXEC?xp_cmdshellfor example.SET FMTONLY ON- but that is kinda deprecated; annoyingly, the replacements only process as far as the first grid, and don't work with your example. The advantage ofFMTONLYover using a transaction is that it has no IO costs and no blocking - but it still could suffer from things like extended stored procedures (you cannot roll back anxp_cmdshell)