简体   繁体   中英

SQL Server - data is not written in to the correct schema

In my database I have two schema - default .dbo and another called .ptx Almost all tables in the two schema are identical. We need two schema to handle data separately for two different products. How I switch between these two schema is by changing the SQL server login user from application level. ie, we have two SQL users ANF_DEFAULT and ANF_PTX. If the product is default the user is ANF_DEFAULT and if the product is PTX, I switch the user to ANF_PTX and create the SqlConnection accordingly.

Everything has been working good so far, the two schema were populated correctly. But suddenly after we move in to a new production environment, the PTX product data goes in to the .dbo schema instead of the .ptx schema. This is still working fine in my local environment but the issue is visible in production. The only visible change is that I use SQL Server 2008, where as the production runs SQL Server 2012, but I think this is hardly the issue. I also checked the properties of the ANF_PTX user:

USE [ANX_GLF]
GO

/****** Object:  User [ANF_PTX]    Script Date: 8/27/2014 5:29:04 AM ******/
CREATE USER [ANF_PTX] FOR LOGIN [ANF_PTX] WITH DEFAULT_SCHEMA=[PTX]
GO

Please help me figure out the issue here.

I figured out the reason for this issue. Apparently, in the new deployment, the _CMS user looked fine, with its default schema being pointed to .cms. However, the problem was with the login FMSI_APP_CMS in the SQL Server. In this login, the FMSI_APP_CMS user was assigned to the super admin role. When the user is a super admin, its default schema is ignored and it always points to the .dbo schema. When the superadmin role was removed from the login, the schema switch was working fine again. More information about this can be found in the following: http://msdn.microsoft.com/en-us/library/ms176060.aspx

Here it says: "he value of DEFAULT_SCHEMA is ignored if the user is a member of the sysadmin fixed server role. All members of the sysadmin fixed server role have a default schema of dbo."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM