我正在尝试创建一个访问单个服务器上的多个数据库的查询。我正在使用游标访问运行查询的基本服务器的多个其他链接服务器。我遇到的问题如下:
Msg 916, Level 14, State 1, Line 43
The server principal "USER" is not able to access the database "Metals" under the current security context.
Msg 3930, Level 16, State 1, Line 104
The current transaction cannot be committed and cannot support operations that write to the log file. Roll back the transaction.
Msg 916, Level 14, State 1, Line 43
The server principal "User" is not able to access the database "Metals" under the current security context.
Msg 3930, Level 16, State 1, Line 104
The current transaction cannot be committed and cannot support operations that write to the log file. Roll back the transaction.
服务器显示我已登录,那么为什么数据库使用“USER”和“User”来尝试访问“Metals”数据库?
我相信访问Metals数据库的查询是正确的,因为它在动态sql代码之外运行时返回正确的数据。我认为这个问题与权限有关,但我不确定我应该为用户更改哪些权限。目前,BLUser只具有连接和选择权限。我是否应该添加其他权限以允许他们访问Metals数据库?
查询如下:
DECLARE @location as varchar(50)
DECLARE @srv as varchar(20)
DECLARE @alphaDb as varchar(20)
DECLARE LabCursor Cursor FOR
SELECT Location, SQLServer, AlphaDB
FROM Labs
OPEN LabCursor
DECLARE @sql as varchar(max)
CREATE TABLE #tmpCombinedResults
(
Lab varchar(50) NULL,
Department varchar(50) NULL,
Instrument varchar(50) NULL,
Method varchar(50) NULL,
Matrix varchar(50) NULL,
StudyDate datetime NULL,
StudyNumber int NULL
)
FETCH NEXT FROM LabCursor INTO @location, @srv, @alphaDb
WHILE @@FETCH_STATUS = 0
BEGIN
-- query with both metals and alpha
SET @sql =
'
CREATE TABLE #tmpResults
(
Lab varchar(50) NULL,
Department varchar(50) NULL,
Instrument varchar(50) NULL,
Method varchar(50) NULL,
Matrix varchar(50) NULL,
StudyDate datetime NULL,
StudyNumber int NULL
)
INSERT INTO #tmpResults(Department, Instrument, Method, Matrix, StudyDate, StudyNumber)
SELECT t.Dept,
oms.InstrumentID,
oms.Method,
oms.Matrix,
MAX(oms.DateOfStudy) StudyDate,
oms.StudyNum
FROM [' + @alphaDb + '].[dbo].AnalRunSeq ars
INNER JOIN [' + @alphaDb + '].[dbo].ottMDL1Studies oms ON ars.TestNo = oms.Method
INNER JOIN [' + @alphaDb + '].[dbo].Tests t ON ars.TestCode = t.TestCode
INNER JOIN [' + @alphaDb + '].[dbo].AnalRuns ar ON ars.RunID = ar.RunID
AND oms.InstrumentID = ar.InstrumentID
AND oms.Analyst = ar.Analyst
INNER JOIN [' + @alphaDb + '].[dbo].Instruments i ON oms.InstrumentID = i.InstrumentID
WHERE oms.ActiveStudy <> 0
AND oms.TypeOfStudy = ''MDL''
GROUP BY oms.InstrumentID,
oms.Method,
oms.Matrix,
oms.StudyNum,
t.Dept,
i.InActive
HAVING t.Dept Not In
(''sub-org'',''sub'',''subpr'')
AND i.InActive = 0
ORDER BY oms.InstrumentID
--error occurs in this part of the code
IF (SELECT COUNT(*) as Qty FROM ' + @srv + '.master.sys.databases where name = ''MetalData'') > 0
BEGIN
UPDATE #tmpResults
SET Department = ''ME'',
Instrument = ms.InstrumentID,
Method = ms.TestNo,
Matrix = ms.Matrix,
StudyDate = (SELECT MAX(ms.InUseDate)
FROM [Metals].[dbo].MDLStudies ms
WHERE ms.InUseDate = InUseDate)
FROM #tmpResults tmp
INNER JOIN ' + @srv + '.[Metals].[dbo].MDLStudies ms ON tmp.Instrument = ms.InstrumentID
WHERE ms.Active = 1
END
SELECT ''' + @location + ''' AS Lab,
Department,
Instrument,
Method,
Matrix,
StudyDate,
StudyNumber
FROM #tmpResults
ORDER BY Lab, Department, Instrument
DROP TABLE #tmpResults
'
IF DB_NAME() <> @alphaDb
BEGIN
SET @sql = 'EXEC(''' + REPLACE(@sql, '''', '''''') + ''') at ' + @srv
END
INSERT INTO #tmpCombinedResults
EXEC(@sql)
FETCH NEXT FROM LabCursor INTO @location, @srv, @alphaDb
END
CLOSE LabCursor
DEALLOCATE LabCursor
SELECT *
FROM #tmpCombinedResults
DROP TABLE #tmpCombinedResults
我在网上找到的当前解决方案,所有概述here也不适用于我。这个one特别没有意义,因为我没有在对象资源管理器详细信息的左栏中看到数据库作为选项。
非常感谢任何有助于解决此问题的帮助!
答案 0 :(得分:0)
此问题是由用户在光标引用的五个服务器中的两个服务器中没有适当权限引起的。权限得到纠正后,问题就解决了。