Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permissions for truncating a table

What permission do I need to GRANT a user, in MSSQL, in order to be able to truncate a table?

I'm trying to grant the minimal set of permissions, but I can't use DELETE, because the table is very large, and I want the operation to be quick.

like image 283
Shahar Mosek Avatar asked Jan 19 '11 12:01

Shahar Mosek


People also ask

What is the procedure for truncating a table?

To resolve it, you could run the command as Execute Immediate. Change your stored procedure to call the TRUNCATE statement like this: CREATE PROCEDURE testProc IS BEGIN EXECUTE IMMEDIATE 'TRUNCATE TABLE tablename'; END testProc; This procedure should now run successfully.

How do you truncate a table in access?

When you want to Truncate the table just 'DROP' the table en copy 'MyTable_Template' to 'MyTable'. Now the autoincrementfield of your 'new' table 'MyTable' will start at 1 again.


2 Answers

You need the ALTER permission: see the Permissions section here.

Note that you can also use a stored procedure with EXECUTE AS, so that the user running the stored procedure does not need to even be granted the ALTER permission.

like image 121
Shahar Mosek Avatar answered Oct 07 '22 16:10

Shahar Mosek


You can create a stored procedure with execute as owner:

create procedure dbo.TruncTable with execute as owner as truncate table TheTable go 

Then grant execution permissions to whoever needs to truncate that table:

grant execute on TruncTable to TheUser 

Now TheUser can truncate the table like:

exec dbo.TruncTable 
like image 33
Andomar Avatar answered Oct 07 '22 16:10

Andomar