1

I am designing a multi-tenant database. Many actions that are needed on a per-tenant basis have been written as stored procedures. Almost all of them use dynamic SQL execution since I have not found a way to differentiate between the schemas other than with dynamic statements.

EXEC( 'SELECT * FROM ' + @SchemaName + '.Contacts' )

Is there any way to create a variable representing the schema so I can call the select statements without building them dynamically?

SELECT * FROM @TheSchema.Contacts

SQL Server 2008

4

3 に答える 3

2

FWIW I have found it much more useful to separate customers by database than by schema. Reasons?

  1. All schemas can have identical procedures, thus derive them from model instead of having to create anything. Deployment to multiple databases is no more complex than deployment to multiple schemas, actually less so I'd argue, and the only difference is the application would have to know which database to reference instead of which schema.

  2. Different customers can have different recovery models, be backed up on different schedules, etc. This can improve your backup/restore plan especially as the data set gets larger.

  3. Having each customer in a separate database makes it very easy to move a tenant to a different server if they get too big for the current one. Extracting the data and objects for their schema only will be quite convoluted.

  4. Some customers may legally and/or contractually require that you not store their data in the same database as other customers.

于 2012-07-05T16:25:55.637 に答える
0

A simpler solution is to keep the stored procedure in each of the tenant databases and execute it inside them.

于 2012-07-05T16:07:55.033 に答える
0

This question is a bit old, but I am looking into this option as a cost savings measure - since many cloud providers charge per database and the costs associated with storage are less - especially useful for multi-tenant small databases.

From what I understand, a default schema can be set for the login, thus, setting a schema per login would automatically use that schema for all queries and sprocs. Something to consider for those that comes across this question.

于 2014-06-07T13:20:01.310 に答える