我正在尝试首次迁移到Azure,但我收到以下错误:
在用作部件的架构中找到了一个或多个不受支持的元素 一个数据包。错误SQL71564:验证元素[develop]时出错: 元素[develop]已从其登录中孤立而无法进行 部署。 (Microsoft.SqlServer.Dac)
我有点困惑,因为'develop'是我们用于测试的用户名,仅此而已。
背景:
答案 0 :(得分:9)
此孤立情况的常见问题是还原数据库时。如果使用默认策略还原数据库,并且不执行还原命令以外的其他步骤,则还将还原在该还原的数据库中创建的所有数据库用户。由于这些数据库用户没有登录或者已经切断了与SQL登录的连接,因此需要采取步骤重新挂接这些对象。
<强>脚本强>
SET NOCOUNT ON
USE {your-db-name}
GO
DECLARE @loop INT
DECLARE @USER sysname
DECLARE @sqlcmd NVARCHAR(500) = ''
IF OBJECT_ID('tempdb..#Orphaned') IS NOT NULL
BEGIN
DROP TABLE #orphaned
END
CREATE TABLE #Orphaned (UserName sysname,IDENT INT IDENTITY(1,1))
INSERT INTO #Orphaned (UserName)
SELECT [name] FROM sys.database_principals WHERE [type] IN ('U','S') AND is_fixed_role = 0 AND [Name] NOT IN ('dbo','guest','sys','INFORMATION_SCHEMA')
IF(SELECT COUNT(*) FROM #Orphaned) > 0
BEGIN
SET @loop = 1
WHILE @loop <= (SELECT MAX(IDENT) FROM #Orphaned)
BEGIN
SET @USER = (SELECT UserName FROM #Orphaned WHERE IDENT = @loop)
IF(SELECT COUNT(*) FROM sys.server_principals WHERE [Name] = @USER) <= 0
BEGIN
IF EXISTS(SELECT 1 FROM sys.database_principals WHERE [Name] = @USER AND type_desc = 'WINDOWS_USER')
BEGIN
SET @sqlcmd = 'CREATE LOGIN [' + @USER + '] FROM WINDOWS'
Exec(@sqlcmd)
PRINT @sqlcmd
END
IF EXISTS(SELECT 1 FROM sys.database_principals WHERE [Name] = @USER AND type_desc = 'SQL_USER')
BEGIN
SET @sqlcmd = 'CREATE LOGIN [' + @USER + '] WITH PASSWORD = N''password'''
Exec(@sqlcmd)
PRINT @sqlcmd
END
END
SET @sqlcmd = 'ALTER USER [' + @USER + '] WITH LOGIN = [' + @USER + ']'
Exec(@sqlcmd)
PRINT @USER + ' link to DB user reset';
SET @loop = @loop + 1
END
END
SET NOCOUNT OFF
在脚本中,我们将数据库用户映射到SQL登录并将默认密码设置为“密码”这可能不起作用,具体取决于SQL Server上设置的密码策略。请根据您的服务器策略更改默认密码。
http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/fixing-orphaned-database-users/