question

Murali avatar image
Murali asked

how to apply permission

somebody is deleting data in tables in the database. I want to take out delete permission for that login on all the tables.
dba
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ThomasRushton avatar image ThomasRushton ♦♦ commented ·
Just a thought... are you using different schemas within the database? 'cos I have my doubts over the efficacy of both solutions so far proposed in that case...
0 Likes 0 ·
ThomasRushton avatar image
ThomasRushton answered
Why are they deleting data? Is it just an education issue? Assuming that they're deleting data for no particularly good reason, then you'll want to [REVOKE][1]. Or, if the user is a member of a group that also has delete privs, then use [DENY][2]. Something like: DENY DELETE ON < > TO < > If you want to do this for all tables, then you could wrap it up with the `sp_msforeachtable` stored procedure. sp_msforeachtable 'DENY DELETE ON ? TO < >' [1]: http://msdn.microsoft.com/en-us/library/ms187728.aspx [2]: http://msdn.microsoft.com/en-us/library/ms188338.aspx
4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Fatherjack avatar image Fatherjack ♦♦ commented ·
+1 - but just to add a caveat that the `sp_msforeach...` procs are unsupported.
2 Likes 2 ·
WilliamD avatar image WilliamD commented ·
@Fatherjack - I'd still have no real problems with sp_msforeach..., but posted an alternative anyway.
2 Likes 2 ·
Fatherjack avatar image Fatherjack ♦♦ commented ·
@ThomasRushton + @WilliamD - Hell, I use it all over the place and would have a shed-load of work to create work-arounds if it got removed but what I 'do' isnt always best practice. Here, however, I like to appear to follow the rules....
1 Like 1 ·
ThomasRushton avatar image ThomasRushton ♦♦ commented ·
Good point... but I've never been one to conform. ;-)
0 Likes 0 ·
WilliamD avatar image
WilliamD answered
How about doing the same as @ThomasRushton suggests, just without the unsupported `sp_msforeach`: DECLARE @User varchar(255), @TSQL varchar(MAX) SELECT @User = 'YourBadUser', @TSQL = '' SELECT @TSQL = @TSQL + 'DENY DELETE ON ' +QUOTENAME(s.name)+'.'+ QUOTENAME(t.name) + ' TO ' + QUOTENAME(@user)+';'+CHAR(10)+CHAR(13) FROM sys.tables T INNER JOIN sys.schemas S ON T.schema_id = S.schema_id WHERE type = 'u' PRINT @TSQL --EXEC (@TSQL)
5 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ThomasRushton avatar image ThomasRushton ♦♦ commented ·
+1 for a much quicker alternative, and one that's more conducive to being updated should it be found that tables are spattered through various schemas...
0 Likes 0 ·
Fatherjack avatar image Fatherjack ♦♦ commented ·
beauty :) +1
0 Likes 0 ·
WilliamD avatar image WilliamD commented ·
@ThomasRushton - got the message, edited to include schemas. I really have to pay more attention to that. We are lazy where I work and use [dbo], so I rarely consider schemas in scripts like this. Our production code is schema qualified though!
0 Likes 0 ·
ThomasRushton avatar image ThomasRushton ♦♦ commented ·
@WilliamD - dude, you missed the '.' between schema & tablename!
0 Likes 0 ·
WilliamD avatar image WilliamD commented ·
@ThomasRushton - *blush* bah! fixed
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.