Code to validate SQL Scripts

前端 未结 4 831
挽巷
挽巷 2020-11-28 08:19

How can I validate sql scripts before executing them using .net 2.0 and c#?

If the sql is not valid I want to return error rows.

4条回答
  •  感情败类
    2020-11-28 08:43

    What does 'valid' SQL mean? The syntax or the results?

    The only sure way to validate the syntax is the execute the SQL in SQL Server. Have you considered running the SQL in a Transaction and then do a rollback at the end?

    Begin Transaction
    
    --execute your code between the 'Begin Transaction' and the 'rollback' keywords.
    ...
    
    --example
    Insert into mytable(ID)Values(2)
    
    ...
    
    Rollback
    

    MSDN Documentation on rollback

提交回复
热议问题