15

When creating tables in SSMS, I've realized that if you execute the following statement:

CREATE TABLE [tableName];

the table will be created under your own schema (and not dbo). So in order to create it under the dbo schema, you'll need to explicitly say so, like this:

CREATE TABLE [dbo].[tableName];

Does anyone know of a way (eg. server-wide setting) so that when a table is created, the [dbo] part doesn't need to be specified?

migrated from stackoverflow.com Mar 2 '12 at 18:55

This question came from our site for professional and enthusiast programmers.

8

The schema that will be used when schema is omitted will be the default schema of the database user. Therefore, to have dbo schema have the created table without specifying, you'd have to set that database user's default schema to dbo.

Server-wide setting? I don't think so. But you could write a query to convert all database users in a particular database to their default schema changed to dbo if that's what you so choose.

  • @KAJ: SQL2008R2 – Chinesinho Mar 2 '12 at 18:21
  • 1
    But you can't set the default schema to dbo for Windows groups... – Chinesinho Mar 2 '12 at 18:21
  • 5
    @Chinesinho You can't set the default schema of any server principal. You're confusing server logins and database users. Database users contain default schemas. – Thomas Stringer Mar 2 '12 at 19:29
  • Setting the default schema for users mapped to Windows group logins isn't supported prior to SQL Server 2012. – db2 Oct 11 '13 at 14:31
7

Maybe this can help

USE [YourDBName]
GO
ALTER USER [YourUserName] WITH DEFAULT_SCHEMA=[dbo]
GO
  • 3
    but if user is in windows Authentication then it cannot set the default schema – user29261 Oct 11 '13 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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