3

I have two different SQL Server databases (on the same server - if it helps) that need to share the same stored procedure logic. The solution I'm trying to achieve looks like this:

Database1
  Table: TestTable
  Synonym: sp_MyProc pointing at SharedDatabase.dbo.sp_MyProc

Database2
  Table: TestTable
  Synonym: sp_MyProc pointing at SharedDatabase.dbo.sp_MyProc

SharedDatabase
  Proc: sp_MyProc which runs queries against TestTable

My hope was to use the synonyms so that if I execute sp_MyProc while in the context of Database1, it would use Database2.TestTable. And if I execute sp_MyProc while in the context of Database2, it would go against Database2.TestTable. However, when I execute sp_MyProc through either of the synonyms, it ignores the context of the synonym and executes looking for a local copy of TestTable, which is not found.

Is there a way to implement a shared stored procedure that executes against different copies of tables in different databases, either through synonyms or some other mechanism?

Edit
I should mention that in my case I am looking to do this with a large set of existing tables and procs, so any solution that requires modifying the procs or tables themselves are not ideal.

1
  • This is also what I am looking for.
    – diwatu
    Commented Aug 25, 2016 at 22:34

2 Answers 2

3

Something like this would work for the definition of the procedure. Be sure to guard against SQL injection since this is built dynamically.

CREATE PROCEDURE [dbo].dosomething 
  @databaseName sysname,
  @schema sysname,
  @tableName sysname
  as
  declare @cmd as nvarchar(max)
  set @cmd = N'select * from ' + quotename(@schema) + N'.' + quotename(@tableName)
  exec sp_executesql @cmd

Then use it like this:

  dosomething 'SampleDb', 'dbo', 'sampleTable'
1
  • Sorry, I should have mentioned in my question that I am looking to do this with a very large and complex set of existing procs. So changing the procs to accomodate is not an option. But this is a valid answer to the general question so +1. Commented Jul 23, 2012 at 18:26
1

If the stored proc is in the SharedDatabase, then it will always run in context of SharedDatabase. To accomplish what you are trying to do to centralize code, I would maybe pass in a parameter to designate which server it is coming from, so then you can execute the query against that specific TestTable. Basically, you will need to refer to each table using their fully qualified name - i.e. Database1.dbo.TestTable



  USE SharedDatabase

  CREATE PROCEDURE [dbo].sp_MyProc 
  @dbsource varchar(50)
  as 

  if(@dbsource == 'DB1')
  begin

    select * from Database1.dbo.TestTable

  end
  else
  begin

    select * from Database2.dbo.TestTable

  end


  GO 




The other alternative is to make a view in SharedDatabase, which will be called TestTableComposite, with an extra column to identify where the source data is. And then pass that in as the parameter, and your SP on SharedDatabase will always be in context of that DB.

5
  • These are both options but not ideal as I'm trying to refactor a large, existing set of tables / procs to this solution. This would require a lot of changes and potential breakage. Commented Jul 23, 2012 at 18:27
  • So why don't you run the stored procs directly from the two databases, but from SharedDatabase... i.e. issue USE DATABASE1 exec sp_myproc OR USE DATABASE2 exec sp_myproc
    – M.R.
    Commented Jul 24, 2012 at 4:36
  • That doesn't seem to work. The proc doesn't see the tables in db1 or db2, and fails with a "cannot find table" type error. Maybe I'm misunderstanding your idea. Can you expand on it? Commented Jul 24, 2012 at 11:45
  • So - what I meant was, once you have a connection to SharedDatabase, send the command "USE Database1" - once you do that, it should be running everything in context of Database1....
    – M.R.
    Commented Jul 24, 2012 at 18:51
  • This does not work, at least through SSMS. I explicitly chose to connect initially to the Shared database and even with a "use database1" it still tries to find the table in the Shared database. Commented Jul 25, 2012 at 12:13

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