SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

CREATE SCHEMA can do what?!?!?

So I was studying for the 70-451 and ended up reading the BOL entry for CREATE SCHEMA. I honestly can’t remember why, but what I do remember is thinking “CREATE SCHEMA can do what?!?!?” but with maybe a few more exclamation points.

Now I had always thought that CREATE SCHEMA did just that, created a schema. I mean I knew it had the option to specify the owner. Use the keyword AUTHORIZATION in case you were wondering. But that was it. I mean what else was there going to be?


Seems simple enough right? I couldn’t think of anything else that might have been added. But there was just a little bit more.

Would you believe this not only works but is a single command?

-- Set up the owner of the schema
-- Create a table within the schema
CREATE TABLE MyTable (Id INT, MyString varchar(20))
-- Create a table not within the schema
CREATE TABLE dbo.MyTable2 (Id INT, MyString varchar(20))
-- Create a view within the schema
-- Create a view not within the schema
CREATE VIEW dbo.MyView2 AS SELECT * FROM MySchema.MyTable
-- Grant/Deny/Revoke permissions to objects within the schema
GRANT SELECT ON MyView to guest
-- Grant/Deny/Revoke permissions to objects not within the schema

I have no idea why it was set up this way. Maybe so that you could create tables and views and grant permissions all in the same batch as creating the schema itself. Since the CREATE SCHEMA command has to be the only command in a batch.

Quick note on something I found while creating my sample script, the schema being created is the default schema for any object created while in the command. It’s also the default schema for a GRANT/DENY/REVOKE statements performed in the command. And just to make things more fun, during the GRANT/DENY/REVOKE commands it will also find objects owned by the DBO schema when using a single part name. I believe it looks for the schema being created first then searches through the DBO schema, but I haven’t gotten around to testing it yet.

I’m still somewhat surprised by the scope of it though. A single command that could create a schema, create dozens, or more, tables and views, and then grant/deny the permissions for the entire database. Absolutely amazing.

Filed under: Microsoft SQL Server, SQLServerPedia Syndication, T-SQL Tagged: code language, language sql, microsoft sql server, sql statements, T-SQL


My name is Kenneth Fisher and I am Senior DBA for a large (multi-national) insurance company. I have been working with databases for over 20 years starting with Clarion and Foxpro. I’ve been working with SQL Server for 12 years but have only really started “studying” the subject for the last 3. I don’t have any real "specialities" but I enjoy trouble shooting and teaching. Thus far I’ve earned by MCITP Database Administrator 2008, MCTS Database Administrator 2005, and MCTS Database Developer 2008. I’m currently studying for my MCITP Database Developer 2008 and should start in on the 2012 exams next year. My blog is at www.sqlstudies.com.


Leave a comment on the original post [sqlstudies.com, opens in a new window]

Loading comments...