-1

I wish to create a table T1. And when I execute the query that table should be include in HumanResources schema which already exists in the database.

How should I change my query to do this? To get table T1 into the HumanResources schema?

Create Table T1
(
    Id  int,
    Name varchar(20)
)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gt_R
  • 225
  • 1
  • 3
  • 10
  • 3
    **RTFM:** [this is the official MSDN documentation on `CREATE TABLE`](https://msdn.microsoft.com/en-us/library/ms174979.aspx) which explains this **In great detail** ..... – marc_s Mar 21 '15 at 10:04

2 Answers2

6
Create Table HumanResources.T1 (...);

In your attempt, you are trying to add it to a database called HumanResources and to the schema dbo. It's database.schema.object.

Edit

In response to the OP's comment, the question has already been answered here: How do I create a SQL table under a different schema?

Community
  • 1
  • 1
Yatrix
  • 13,361
  • 16
  • 48
  • 78
  • If I am creating the table using Graphic Interface, where can I find the option to select the schema? – Gt_R Mar 21 '15 at 06:30
  • @Gt_R You should add that information to your question. No one's going to assume you're doing it that way since you included the script statement. – Yatrix Mar 21 '15 at 06:32
  • I understand that. Your first answer was something that I really needed and was very helpful. Meanwhile I was trying to create the table using graphic interface while expecting the answers for my question. Sorry for the inconvenience. – Gt_R Mar 21 '15 at 06:37
  • @Gt_R It's cool. If my answer helped, upvote it and accept it. Honestly, it's faster to script it than do it through the designer. =) – Yatrix Mar 21 '15 at 06:42
3

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

In your case try running:

CREATE TABLE [schemaname].[tableName](...)
A_Sk
  • 4,532
  • 3
  • 27
  • 51
Samarth
  • 61
  • 4