我正在探索设置SQL 2016数据库服务器的选项,以支持教学生数据库基础知识。该课程将有近200名学生。
我想通过脚本自动创建所有用户帐户和相关数据库 - 这意味着需要同时创建每个用户的数据库。基本上每个学生的用户登录名和数据库将被命名为学生的ID。登录身份验证将是SQL身份验证,而不是Windows,因为实验室PC不在AD上。
如果有人能指出我可以修改或使用它的任何脚本的方向将是非常有帮助的 - 基本上如果我沿着这条路线走,我将需要每年都这样做,所以脚本将是最佳的。
由于 d。
答案 0 :(得分:0)
使用CREATE LOGIN命令。 以下示例将创建用户myDBUser。
USE [master]
GO
CREATE LOGIN [myDBUser] WITH PASSWORD=N'myPassword' MUST_CHANGE, DEFAULT_DATABASE=[myDB], CHECK_EXPIRATION=ON, CHECK_POLICY=ON
GO
当然,为这样的登录创建数据库用户
USE [myDB]
GO
CREATE USER myDBUser FOR LOGIN myDBUser;
答案 1 :(得分:0)
我认为这样的事情会起作用,你可能需要改变登录时的动态SQL位,因为它有点被抛在一起。
--Create table for bulk insert
Create Table UserNameCSV (UserID int, UserName Varchar(100))
--Insert login names from C:\csvtest.txt
BULK
INSERT UserNameCSV
FROM 'c:\csvtest.txt'
WITH
(FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')
GO
-- Run Cursor
Declare @UserName Varchar(100)
Declare db_Cursor CURSOR FAST_FORWARD FOR
Select UserName from UserNameCSV
OPEN db_Cursor
FETCH NEXT FROM db_Cursor INTO @UserName
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @Sql varchar(max) ='CREATE DATABASE ' + @UserName
DECLARE @Sql1 varchar(max) = 'USE [' + @UserName + ']'
DECLARE @Sql2 varchar(max) = 'CREATE LOGIN ' + @UserName + ' WITH PASSWORD=N''myPassword123!'' MUST_CHANGE, CHECK_EXPIRATION=ON, CHECK_POLICY=ON'
EXECUTE(@Sql)
EXECUTE (@Sql1)
EXECUTE (@Sql2)
FETCH NEXT FROM db_Cursor into @UserName
END
CLOSE db_Cursor
DEALLOCATE db_Cursor
答案 2 :(得分:0)
使用Dynamic Sql并使用while循环,我们可以循环学生名称并创建数据库和登录。希望它可以帮助你
IF OBJECT_ID('Tempdb..#USerTable') IS NOT NULL
DROP TABLE #USerTable
IF OBJECT_ID('Tempdb..#USerNameINSERT') IS NOT NULL
DROP TABLE #USerNameINSERT
IF OBJECT_ID('Tempdb..#PasswordsINSERT') IS NOT NULL
DROP TABLE #PasswordsINSERT
--ADD Your student Names Passwords in a single line as below
DECLARE @Usernames NVARCHAR(max) = 'Stud1,Stud2,Stud3,Stud4'
,@Password NVARCHAR(max) = 'Password1,Password2,Password3,Password4'
,@Usernameselect NVARCHAR(max)
,@DatabaseNameselect NVARCHAR(max)
,@PasswordSelect NVARCHAR(max)
,@Usernameselect1 NVARCHAR(max)
,@DatabaseNameselect1 NVARCHAR(max)
,@PasswordSelect1 NVARCHAR(max)
DECLARE @USerName TABLE (
ID INT IDENTITY
,Usernames NVARCHAR(max)
,Passwords NVARCHAR(max)
)
INSERT INTO @USerName (
UserNAmes
,Passwords
)
SELECT @Usernames
,@Password
SELECT ID,UserNAmes INTO #USerNameINSERT
FRom (
SELECT Row_number()Over(Order by (SELECT NULL)) AS ID,Split.a.value('.', 'VARCHAR(1000)') AS UserNAmes
FROM (
SELECT ID, CAST('<S>' + REPLACE(UserNAmes, ',', '</S><S>') + '</S>' AS XML) AS UserNAmes
FROM @USerName
) AS A
CROSS APPLY UserNAmes.nodes('/S') AS Split(a)
)DT
SELECT ID,Passwords INTO #PasswordsINSERT
FRom (
SELECT Row_number()Over(Order by (SELECT NULL)) AS ID,Split.a.value('.', 'VARCHAR(1000)') AS Passwords
FROM (
SELECT CAST('<S>' + REPLACE(Passwords, ',', '</S><S>') + '</S>' AS XML) AS Passwords
FROM @USerName
) AS A
CROSS APPLY Passwords.nodes('/S') AS Split(a)
)DT
SELECT u.ID
,u.UserNAmes
,u.UserNAmes+'Db' AS DatabaseName
,p.Passwords
INTO #USerTable
FROM #USerNameINSERT u
INNER JOIN #PasswordsINSERT p ON p.ID = u.ID
DECLARE @minId INT
,@maxId INT
,@SqlQuery NVARCHAR(max)
,@SqlQuery1 NVARCHAR(max)
,@SqlQuery2 NVARCHAR(max)
SELECT @minId = MIN(ID)
,@maxId = Max(Id)
FROM #USerTable
WHILE (@minId <= @maxId)
BEGIN
SELECT @Usernameselect = UserNAmes
,@DatabaseNameselect = DatabaseName
,@PasswordSelect = Passwords
FROM #USerTable
WHERE ID = @minId
SET @SqlQuery = 'CREATE DATABASE ' + @DatabaseNameselect
SET @SqlQuery1 = 'USE [' + @DatabaseNameselect + ']'
SET @SqlQuery2 = 'CREATE LOGIN ' + @Usernameselect + ' WITH PASSWORD=N''' + @PasswordSelect + ''' MUST_CHANGE, CHECK_EXPIRATION=ON, CHECK_POLICY=ON'+ CHAR(13) + CHAR(10) + 'GO'
SET @SqlQuery = ISNULL('', 'GO') + CHAR(13) + CHAR(10) + @SqlQuery
+ CHAR(13) + CHAR(10) + 'GO' + CHAR(13) + CHAR(10) + @SqlQuery1
+ CHAR(13) + CHAR(10) + 'Go' + + CHAR(13) + CHAR(10) + @SqlQuery2
--EXEC (@SqlQuery)
PRINT @SqlQuery
SET @minId = @minId + 1
END