How to get default schema when adding a table

Coreysan 1,811 Reputation points
2023-07-19T23:22:27.8366667+00:00

I have an internal database, and when I add a new table, I get a problem:

when a record is written to INFORMATION_SCHEMA.TABLES, the column "TABLE_SCHEMA" has the wrong

default schema. Instead of the default "dbo", it is adding my login domain path and name. like this:

"Mydomain\MyName".

Is there a way to reset this to write "dbo"?

SQL Server Other
{count} votes

Accepted answer
  1. Anonymous
    2023-07-20T09:27:52.0666667+00:00

    Hi @Coreysan

    As a supplement, this is the official documentation for altering a schema.

    https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-schema-transact-sql?view=sql-server-ver16

    Best regards,

    Percy Tang

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Yitzhak Khabinsky 26,586 Reputation points
    2023-07-20T01:12:17.3566667+00:00

    Hi @Coreysan,

    It is very simple.

    ALTER SCHEMA dbo TRANSFER [DOMAIN\user].yourtable;

    ALTER SCHEMA TargetSchema TRANSFER SourceSchema.TableName; 
    
    0 comments No comments

  2. Olaf Helper 47,436 Reputation points
    2023-07-20T05:45:04.9066667+00:00

    the column "TABLE_SCHEMA" has the wrong default schema

    It's neither the "default" schema nor a wrong one, it's the schema where you created the object/table.

    It's the schema where you create the table and if you don't define explicit the schema (already a failure), then the default schema of the surrent user is used.

    0 comments No comments

  3. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2023-07-20T08:57:08.6466667+00:00

    If you say CREATE TABLE MyTable, the table will be created in your default schema. Which may be dbo or something else. You can review your default schema with this query:

    SELECT default_schema_name FROM sys.database_principals WHERE name = USER
    

    To change the default schema, you can use the ALTER USER command.


  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.