Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Security 服务器主体“用户”无法在当前安全上下文下访问db“Metals”_Security_Cursor_Ssms_Dynamic Sql_Multiple Databases - Fatal编程技术网

Security 服务器主体“用户”无法在当前安全上下文下访问db“Metals”

Security 服务器主体“用户”无法在当前安全上下文下访问db“Metals”,security,cursor,ssms,dynamic-sql,multiple-databases,Security,Cursor,Ssms,Dynamic Sql,Multiple Databases,我正在尝试创建一个查询,该查询访问单个服务器上的多个数据库。我正在使用光标访问多个其他链接服务器,以访问运行查询的基本服务器。我遇到的问题如下: 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

我正在尝试创建一个查询,该查询访问单个服务器上的多个数据库。我正在使用光标访问多个其他链接服务器,以访问运行查询的基本服务器。我遇到的问题如下:

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 db的查询是正确的,因为它在动态sql代码之外运行时返回正确的数据。我认为问题与权限有关,但我不确定应该为用户更改哪些权限。目前,Bluer只有“连接”和“选择”权限。我是否应该添加其他权限以允许他们访问数据库

查询如下:

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
我在网络上找到的当前解决方案,所有概述的都不适合我。这尤其没有意义,因为我在对象资源管理器详细信息的左栏中没有将数据库视为选项


如果您能帮助解决此问题,我们将不胜感激

此问题是由于用户在光标引用的五个服务器中的两个服务器上没有适当的权限造成的。更正权限后,问题就解决了