views:

50

answers:

1

I'm working on a Delphi/WIN32 application that uses an SQL Server database as back-end, using ADO to access the data. There are many users who use this application, but one user is using a special setup: they have multiple database schema's and every schema contains the complete datamodel for the application. Every schema also has a database user which defaults to the specific schema. They also have a separate login account for every database user, allowing them to control which schema to use simply by using a different login account in the connection string. They use this setup to have a single, centralized database which supports multiple offices. Normally, every office would have it's own database but here, every office has their own schema.

I like this solution that they're using. I haven't thought about this before simply because the application is normally used by single offices. Only this customer had a need to have a centralized database. The application works just fine, even though it's unaware of these schema's, simply because the login account will default to the correct schema.

But now they've asked if it's possible to change the code in a way that the user can select the schema to which they want to connect. Thus, a user needs to be able to switch between schema's in the application. And I don't want to rewrite the code to support these schema's simply because I need to keep the SQL code database neutral. So I'm looking for a way to switch a user to another schema without much impact on the code itself.

Any suggestions?

A: 

How about changing the default schema of the user?

ALTER USER <user name> 
  WITH DEFAULT_SCHEMA = <desired schema>;

Of course you will need to execute this under escalated privileges as I'm sure you don't have all users with ALTER USER capabilities.

Aaron Bertrand
I've considered that, but it would affect the database user, not the login account. There could be multiple login accounts linked to the database user, who would all be looking to the other schema. Not a good idea.
Workshop Alex
I don't understand, you seemed to indicate that logins:users were 1:1 : "separate login account for every database user"
Aaron Bertrand
Unfortunately, no. There's an 1:n relation between users and logins. But every login connects to only 1 user.
Workshop Alex
I suggest you enforce that users and logins are 1:1 such that changing the login in the connection string changes both the user and the default schema. Otherwise, it is going to be a big ball of string.
Aaron Bertrand