5

Following is my schema detail:

  • DB_A : schema_1, schema_2, schema_3
  • DB_B : schema_3

some procedures in schema_3 access resources(table, view, sp) from schema_1 and schema_2.

All procedures in schema_3 are same on both the dbs. How do I access schema_1 from schema_3 for both the dbs.

Now I can hard code DB_A in my procedures but when I move code to client machine, it will create a problem since DB_A may not be same(one of the reason being client is miser and having QA, Dev and Prod on same machine).

Second option is getting DB_A name as a parameter, but it will make all the schema_3 SPs dynamic (as I did not get any method to access something like @DBName.schema_name.ResourceName).

Third option is creating linked servers, which again do not solve my problem because of same reason as first.

Any idea how to proceed, where I do not want my procedures to be dynamic just because 80% of them are straight.

Edit Start:

So I can restate it as I have multiple databases with a database having resources (table/view/schema) which needs to be shared and then having other databases (one or more) which have stored procedures which computes on data from shared database and self database.

Shared database name is not going to be constant on all the environments and I want to change them(environment specific). I have come out with a solution where I will be creating synonym for all the shared resources and all procedures will be using them, that way they are all referring to shared resources from first database.

For each installation I need to modify synonyms definition to reflect correct share database name. Is there any SYNONYM For Database Name, that way I will have way less synonyms to handle.

3 Answers 3

3

Well the best choice I found is as follows.

Create Synonym (independent database DB_B) for individual objects (in shared database DB_A) with same name in same schema. That way your existing procedures need not change, and will work as required. Synonym gives a good reference on this. I will soon be creating an app to ease creating synonyms for these kind of situations.

CREATE SYNONYM DB_B.schema_1.proc_1 FOR DB_A.schema_1.proc_1
0

You can run your procedure in DB_A and create a view from DB_A to DB_B:

create view dbo.vw_B_Schema_3
as
select  *
from    DB_B.dbo.Schema_3

You'd have to create three versions of the view (dev, QA, prod.) But the view will be the only difference: procedure definitions can remain identical.

2
  • Schema_1, Schema_2, Schema_3 are not resources but schema names like dbo, employee, etc. Second thing DB_B is not fixed, its name can change dynamically in different environment. Commented Feb 18, 2014 at 12:52
  • I have found Synonym for objects, is their something like synonym for database names Commented Feb 18, 2014 at 13:56
0

If DB_A and DB_B are on same server, only sure you that the login have permission in two database.

Now, use [database].[schema].[object], when you use object of others database

eg: I have two database, ("helpdesk", "intranet")

from heldesk to intranet

create view dbo.users
as 
select login, name, lastname
from intranet.dbo.user // [database].[schema].[object] user is a table in dbo schema from intranet database.
where status = 1
;
1
  • We know about that solution, but the problem is not only with tables/views but also with executing procedures from other database. So any alternate for Synonym for tables like we have synonym link for table/procedures/views. So in detail we would like to have some global variable which holds database name and we can change it on environment base and get data out of there or execute procedures from there. Commented Feb 18, 2014 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.