如何检测我是否具有CREATE TABLE权限?

时间:2021-04-16 15:05:29

How can I programmatically detect whether the current user has CREATE TABLE permissions in the current database?

如何以编程方式检测当前用户是否在当前数据库中具有CREATE TABLE权限?

I'm writing a tool that will do some lengthy processing, and then create database tables to save the results. I only want to create the tables if the processing runs to completion. But if the user won't be able to create the tables, there's no point spending time processing. I'd like to detect the permission issue and fail fast.

我正在编写一个可以进行一些冗长处理的工具,然后创建数据库表来保存结果。我只想在处理完成时创建表。但是如果用户无法创建表格,那么花时间处理是没有意义的。我想检测权限问题并快速失败。

This will be from a C# application, and I can't assume that any special libraries will be installed, which probably rules out SQL-DMO and SMO. If there's an easy way to check permissions with a T-SQL query/script, that would be ideal.

这将来自C#应用程序,我不能假设将安装任何特殊的库,这可能排除了SQL-DMO和SMO。如果有一种简单的方法来检查T-SQL查询/脚本的权限,那将是理想的选择。

1 个解决方案

#1


10  

Why not create the table first, and if that fails, go no further?

为什么不首先创建表,如果失败,不再进一步?

That said, you can also use the following function HAS_PERMS_BY_NAME:

也就是说,您还可以使用以下函数HAS_PERMS_BY_NAME:

SELECT HAS_PERMS_BY_NAME(db_name(), 'DATABASE', 'CREATE TABLE');

More information here:

更多信息:

http://msdn.microsoft.com/en-us/library/ms189802.aspx

#1


10  

Why not create the table first, and if that fails, go no further?

为什么不首先创建表,如果失败,不再进一步?

That said, you can also use the following function HAS_PERMS_BY_NAME:

也就是说,您还可以使用以下函数HAS_PERMS_BY_NAME:

SELECT HAS_PERMS_BY_NAME(db_name(), 'DATABASE', 'CREATE TABLE');

More information here:

更多信息:

http://msdn.microsoft.com/en-us/library/ms189802.aspx