Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect whether I have CREATE TABLE permission?

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.

like image 210
Joe White Avatar asked Apr 21 '11 19:04

Joe White


1 Answers

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

like image 54
Steve Mayne Avatar answered Nov 15 '22 05:11

Steve Mayne