Solved

SQL Rights to Seperate DBs on Same Server

Posted on 2013-06-06
6
282 Views
Last Modified: 2013-06-17
I have a single instance of SQL 2008R2.  I have two seperate databases...one ERP the other a CRM.  I have users who login to the ERP program and users who login to the CRM program.  I've created a view in the ERP database the queries a table in the CRM database.  And of course not all users in the ERP database have rights to the CRM database and when they run a report from the ERP system that uses that query they will get the following error.

The server principal "username" is not able to access the database "CRM" under the current security context.

I don't want to have to manage each user of the ERP database and give them rights to the CRM database.  So, I've been searching for a way to be able to run the report/query.  Here's what I have to work with...but still not working.
The ERP database has a database role that every user belongs to.
The SQL public server role has been granted 'select' rights to all the tables in CRM.

I know I can get it to work if I grant each user of the ERP database rights to the CRM database, but I was hoping I could do it without having to touch each user of the ERP system...as the accounts are being added/removed often.

Any insight would be greatly appreciated!
0
Comment
Question by:ClowWater
  • 3
  • 2
6 Comments
 

Assisted Solution

by:ClowWater
ClowWater earned 0 total points
ID: 39227102
I've found this article that tells me I won't be able to do what I want unless I'm on 2012.  Any other ideas?

http://technet.microsoft.com/en-us/magazine/hh641407.aspx
0
 
LVL 20

Expert Comment

by:Marten Rune
ID: 39229222
You ought to be able to use a linked table like this:

USE [db2]
GO
CREATE SYNONYM [dbo].[tbl1]
FOR
[db1].[dbo].[tbl1]

Run as a user vith rights in both databases.
Now grant view to user/role in db2, and users belonging to db2 will be able to see data in db1.

Regards Marten
0
 
LVL 15

Expert Comment

by:jorge_toriz
ID: 39229307
1. Create a certificate
2. Create a login mapped to that certificate
3. Create a user for that login in each database
4. Give to that user the right permissions.
5. Create a stored procedure with elevation (SIGNATURE) to take user out from one database and take data from the other database.
6. Enable database ownership chaining
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 15

Accepted Solution

by:
jorge_toriz earned 500 total points
ID: 39229410
I have done a sample
USE master
GO
CREATE DATABASE CRM
GO
CREATE DATABASE ERP
GO
CREATE CERTIFICATE CRM_ERP_Certificate
ENCRYPTION BY PASSWORD = 'certificate_password'
WITH SUBJECT = 'Cross DB'
GO
BACKUP CERTIFICATE CRM_ERP_Certificate
TO FILE = 'D:\Proyectos\Experts-Exchange\DB Ownership Chaining\Certificate.cer'
WITH PRIVATE KEY (
	FILE = 'D:\Proyectos\Experts-Exchange\DB Ownership Chaining\Private.key',
	ENCRYPTION BY PASSWORD = 'certificate_backup_password',
	DECRYPTION BY PASSWORD = 'certificate_password'
)
GO
CREATE LOGIN CRM_ERP
FROM CERTIFICATE CRM_ERP_Certificate
GO
sp_configure 'cross db ownership chaining', 1
GO
RECONFIGURE
GO
CREATE LOGIN CRM WITH PASSWORD = 'password', CHECK_POLICY = OFF
GO
CREATE LOGIN ERP WITH PASSWORD = 'password', CHECK_POLICY = OFF
GO
USE CRM
GO
CREATE USER CRM FOR LOGIN CRM
CREATE USER CRM_ERP FOR LOGIN CRM_ERP
GO
sp_addrolemember 'db_datareader', 'CRM'
GO
CREATE TABLE CRMTable(
	Id SMALLINT
)
GO
INSERT INTO CRMTable (Id)
VALUES (1), (2), (3), (4), (5)
GO
USE ERP
GO
CREATE USER ERP FOR LOGIN ERP
CREATE USER CRM_ERP FOR LOGIN CRM_ERP
GO
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'ERP_master_key_password'
GO
CREATE CERTIFICATE CRM_ERP_Certificate
FROM FILE = 'D:\Proyectos\Experts-Exchange\DB Ownership Chaining\Certificate.cer'
WITH PRIVATE KEY (
	FILE = 'D:\Proyectos\Experts-Exchange\DB Ownership Chaining\Private.key',
	DECRYPTION BY PASSWORD = 'certificate_backup_password'
)
GO
sp_addrolemember 'db_datareader', 'ERP'
GO
CREATE TABLE ERPTable(
	Id SMALLINT
)
GO
INSERT INTO ERPTable (Id)
VALUES (10), (11), (12), (13), (14), (15)
GO
CREATE PROC pGetCRMERP
AS
BEGIN
	SELECT * FROM ERPTable
	UNION ALL
	SELECT * FROM CRM.dbo.CRMTable
END
GO
ADD SIGNATURE TO pGetCRMERP
BY CERTIFICATE CRM_ERP_Certificate
GO
GRANT EXECUTE
ON pGetCRMERP
TO ERP

Open in new window


If you connect to ERP database with ERP user and execute the pGetCRMCRP stored procedure before adding the signature you will get a permission error, but if you add the signature, then each user that can execute pGetCRMERP will get the permission needed to read the table in CRM database.
0
 

Author Comment

by:ClowWater
ID: 39242135
.
0
 

Author Closing Comment

by:ClowWater
ID: 39252590
Thanks jorge.  This is an extensive solution.  Not exactly what I had in mind...which is a good thing...it's a different approach.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now