Mik*_*ail 3 schema sql-server-2012
我正在寻找一种从现有数据库创建新模式的方法,只选择数据库对象的子集。即我想要一些“数据库表示”,它只包括明确指定的表的子集。创建包含/无法访问数据库中所有对象的模式的最简单方法是什么。有可能吗?
我正在尝试在 MS SQL Server 2012 R2 中执行此操作。
您可以创建一个新模式并使用同义词公开您需要的对象:
USE master;
GO
IF DB_ID('testDatabase') IS NOT NULL
BEGIN
ALTER DATABASE testDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE testDatabase
END
GO
CREATE DATABASE testDatabase
GO
USE testDatabase;
GO
-- test table in schema dbo
CREATE TABLE dbo.test (
i int primary key clustered
)
GO
INSERT INTO dbo.test VALUES (1),(2),(3)
GO
-- new schema for exposed objects
CREATE SCHEMA schema1
GO
-- synonym for dbo.test
CREATE SYNONYM schema1.test FOR dbo.test
GO
-- new user with no permissions on the dbo schema
CREATE USER testUser WITHOUT LOGIN
GO
-- grant permissions on the new schema only
GRANT SELECT ON schema::schema1 TO testUser
GO
-- change security context to testUser
EXECUTE AS USER = 'testUser'
GO
SELECT * FROM schema1.test; -- works
SELECT * FROM dbo.test; -- fails
-- revert to the original security context
REVERT
GO
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15004 次 |
最近记录: |