为sql server创建用户

时间:2013-03-06 16:27:10

标签: sql sql-server

下面的代码允许我通过使用查询在sql server中创建用户。它当前创建了一个用户配置文件,但它允许用户访问服务器上的所有数据库。我只希望他们能够访问“TEST”数据库,我将如何进行此操作

CREATE LOGIN [SupportUser]
WITH PASSWORD=N'SupportUser',
DEFAULT_DATABASE=[master],
DEFAULT_LANGUAGE=[us_english],
CHECK_EXPIRATION=OFF,
CHECK_POLICY=OFF
GO

2 个答案:

答案 0 :(得分:1)

CREATE LOGIN [SupportUser] WITH PASSWORD=N'SupportUser', DEFAULT_DATABASE=[MASTER], 
CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO
USE [TEST]
GO
CREATE USER [SupportUser] FOR LOGIN [SupportUser]
GO
USE [TEST]
GO
EXEC sp_addrolemember N'db_datareader', N'SupportUser'
GO
USE [TEST]
GO
EXEC sp_addrolemember N'db_datawriter', N'SupportUser'
GO
USE [TEST]
GO
EXEC sp_addrolemember N'db_ddladmin', N'SupportUser'
GO

答案 1 :(得分:0)

试试这个。

CREATE LOGIN [SupportUser] 
WITH PASSWORD=N'SupportUser', 
DEFAULT_DATABASE=[master], 
CHECK_EXPIRATION=OFF, 
CHECK_POLICY=OFF
GO
USE [TEST]
GO
CREATE USER [SupportUser] FOR LOGIN [SupportUser]
GO
use [TEST]
GO
GRANT SELECT ON [dbo].[myView] TO [SupportUser]
GO