2015-07-10 48 views
0

我正在嘗試創建一個查詢來訪問單個服務器上的多個數據庫。我正在使用遊標訪問運行查詢的基本服務器的多個其他鏈接服務器。那我遇到的問題是:服務器主體「用戶」無法在當前安全上下文中訪問數據庫「金屬」

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」和「用戶」的數據庫,以嘗試訪問「金屬」分貝?
我相信訪問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

此問題是由兩個,正在由光標引用的五個服務器中沒有適當的權限用戶造成的。一旦權限得到糾正,問題就解決了。

相關問題