1、首先创建一个账号和一个数据库并添加这个用户为这个数据库的使用者。 [xhtml] view plaincopy USE MASTERGO CREATE LOGIN test_user WITH PASSWORD = 'test_user_pwd';GO CREATE DATABASE DenySelectStar;GO USE DenySelectStarGO CREATE USER test_user FROM LOGIN test_user WITH DEFAULT_SCHEMA = DenySelectStar;GO EXEC sp_addrolemember N'db_datareader', N'test_user'GO
2、再创建一个新表,并命名一列为dummycolumn,并插入几条数据: [xhtml] view plaincopy CREATE TABLE dbo.Table_1( IdentityKey INT IDENTITY(1,1) PRIMARY KEY CLUSTERED ,ColumnOne INT NULL ,DummyColumn CHAR(1) NULL);GO INSERT INTO dbo.Table_1 (ColumnOne) SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5;GO DENY SELECT ON OBJECT:: dbo.Table_1(DummyColumn) TO test_user;GO
3、再去查询一下select * from : [xhtml] view plaincopy USE DenySelectStarGO SELECT * FROM dbo.Table_1; --Result Msg 230, Level 14, State 1, Line 1The SELECT permission was denied on the column 'DummyColumn' of the object 'Table_1, database 'DenySelectStar', schema 'dbo'.