开发者

List tables involved in a sql statement before it is run?

开发者 https://www.devze.com 2022-12-20 20:14 出处:网络
Is it possible, in .NET, to pass a sql statement to SQL server for parsing and return the tables involved in the statement and the type of operation. So for a statement like this :

Is it possible, in .NET, to pass a sql statement to SQL server for parsing and return the tables involved in the statement and the type of operation. So for a statement like this :

select * from Table1
left outer join Table2 on Table1.id=Table2.foreignid;
delete from Table2 where date < 2009/12/12

SQL Server might return the tables involved like this :

Table1    Read
Table2    Read
Table2    Delete

So the statement isn't executed, it just returns the tables involved.

The reason I ask is that the application I am working on has application level table permissions that I want to apply on a table by table basis. The current method开发者_如何学C of parsing out the tables involved in a statement uses a regular expression and fails in anything other than simple statements. Obviously you can't really be using a regular expression for this kind of job


Would SET SHOWPLAN_ALL do it for you? It simulates the call query on the server and you may be able to parse the results.

Edit: It looks like SET SHOWPLAN_TEXT would be easier to parse.


The reason I ask is that the application I am working on has application level table permissions that I want to apply on a table by table basis.

It seems to me you should create a database level role to capture those permissions, and apply them on that level. You paid for your database management system - don't spend your time coding what is best done by a solution that's already in place.

Application level permissions may make sense on the row level, and even those can be implemented at the database. But if they are on the table level, I don't see any excuse that justifies coding enforcement yourself. Use your database, you paid for it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号