截断表的权限

发布于 2024-10-12 11:11:51 字数 86 浏览 2 评论 0原文

在 MSSQL 中,我需要授予用户什么权限才能截断表?

我试图授予最小的权限集,但我无法使用 DELETE,因为表非常大,并且我希望操作快速。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半窗疏影 2024-10-19 11:11:51

您需要 ALTER 权限:请参阅此处<的权限部分/a>.

请注意,您还可以将存储过程与 EXECUTE AS 一起使用,这样运行存储过程的用户甚至不需要被授予 ALTER 权限。

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.

剑心龙吟 2024-10-19 11:11:51

您可以创建一个存储过程并以所有者身份执行

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

然后向需要截断该表的任何人授予执行权限:

grant execute on TruncTable to TheUser

现在TheUser可以截断该表,如下所示:

exec dbo.TruncTable

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
一人独醉 2024-10-19 11:11:51

所需的最低权限是
对表名进行 ALTER。截断表
权限默认为表
所有者、系统管理员固定成员
服务器角色,以及 db_owner 和
db_ddladmin 固定数据库角色,以及
不可转让。但是,您可以
合并截断表
模块内的语句,例如
存储过程,并授予
给模块适当的权限
使用 EXECUTE AS 子句。了解更多
信息,请参阅使用 EXECUTE AS 来
创建自定义权限集。

来源

The minimum permission required is
ALTER on table_name. TRUNCATE TABLE
permissions default to the table
owner, members of the sysadmin fixed
server role, and the db_owner and
db_ddladmin fixed database roles, and
are not transferable. However, you can
incorporate the TRUNCATE TABLE
statement within a module, such as a
stored procedure, and grant
appropriate permissions to the module
using the EXECUTE AS clause. For more
information, see Using EXECUTE AS to
Create Custom Permission Sets.

Source

不必在意 2024-10-19 11:11:51

不要授予,隐藏...

CREATE TRIGGER TRG_MyTable_Foo 
WITH EXECUTE AS OWNER
INSTEAD OF DELETE
AS
IF CONTEXT_INFO() = 0x9999
BEGIN
    TRUNCATE TABLE MyTable
    SET CONTEXT_INFO 0x00
END
GO

SET CONTEXT_INFO 0x9999
DELETE MyTable WHERE 1=0

SET CONTEXT_INFO may be 毫无疑问更好将正常的 DELETE 与 TRUNCATE TABLE 分开

我还没有尝试过这个...

编辑:更改为使用 SET CONTEXT_INFO。

Don't GRANT, hide...

CREATE TRIGGER TRG_MyTable_Foo 
WITH EXECUTE AS OWNER
INSTEAD OF DELETE
AS
IF CONTEXT_INFO() = 0x9999
BEGIN
    TRUNCATE TABLE MyTable
    SET CONTEXT_INFO 0x00
END
GO

SET CONTEXT_INFO 0x9999
DELETE MyTable WHERE 1=0

SET CONTEXT_INFO may be is without any doubt better to separate a normal DELETE from a TRUNCATE TABLE

I haven't tried this...

Edit: changed to use SET CONTEXT_INFO.

此生挚爱伱 2024-10-19 11:11:51

您可以创建一个仅对一个表以所有者身份执行的存储过程,或对任何表创建一个存储过程:

CREATE PROCEDURE [dbo].[spTruncate]
    @nameTable varchar(60)  
    WITH EXECUTE AS OWNER
    AS

SET NOCOUNT OFF;
DECLARE @QUERY NVARCHAR(200);

SET @QUERY = N'TRUNCATE TABLE ' + @nameTable + ';'

EXECUTE sp_executesql @QUERY;

You can create a stored procedure with execute as owner to only one table or a store procedure to any table:

CREATE PROCEDURE [dbo].[spTruncate]
    @nameTable varchar(60)  
    WITH EXECUTE AS OWNER
    AS

SET NOCOUNT OFF;
DECLARE @QUERY NVARCHAR(200);

SET @QUERY = N'TRUNCATE TABLE ' + @nameTable + ';'

EXECUTE sp_executesql @QUERY;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文