Ways to validate T-SQL queries?

前端 未结 6 1875
逝去的感伤
逝去的感伤 2021-01-03 10:48

I have access to an Access database and within that database are fields filled with TSQL queries. These queries are processed by T-SQL on a server. So when I write these SQL

6条回答
  •  隐瞒了意图╮
    2021-01-03 11:39

    Actually, a combination of MattMc3's answer and FremenFreedom's answer should work.

    Download SQL Express.

    Then, declare the following stored procedure:

    create procedure IsValidSQL (@sql varchar(max)) as
    begin
        begin try
            set @sql = 'set parseonly on;'+@sql;
            exec(@sql);
        end try
        begin catch
            return(1);
        end catch;
        return(0);
    end; -- IsValidSQL
    

    You can test it with:

    declare @retval int;
    exec @retval = IsValidSQL 'select iif(val, 0, 1) from t';
    select @retval
    

    or with:

    declare @retval int;
    exec @retval = IsValidSQL 'select val from t';
    select @retval
    

    Note: this will catch the IIF() issue. It will not catch anything related to the table structures or column structures. You would need the schema for that and a slightly different approach ("select top 0 * from () t") woudl do it.

    You might be able to do something with SQL Fiddle online. However, I would suggest having a local copy of the database.

提交回复
热议问题