How can I detect whether I have CREATE TABLE permi

2020-03-19 06:56发布

问题:

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

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.

回答1:

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:

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

More information here:

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