I imported a bunch of tables from an old sql server (2000) to my 2008 database. All the imported tables are prefixed with my username, for example: jonathan.MovieData
. In the table properties
it lists jonathan
as the db schema. When I write stored procedures I now have to include jonathan.
in front of all the table names which is confusing.
How do I change all my tables to be dbo instead of jonathan?
Current result: jonathan.MovieData
Desired result: dbo.MovieData
You can run the following, which will generate a set of ALTER sCHEMA statements for all your talbes:
You then have to copy and run the statements in query analyzer.
Here's an older script that will do that for you, too, I think by changing the object owner. Haven't tried it on 2008, though.
Got it from this site.
It also talks about doing the same for stored procs if you need to.
You can batch change schemas of multiple database objects as described in this post:
How to change schema of all tables, views and stored procedures in MSSQL
I just posted this to a similar question: In sql server 2005, how do I change the "schema" of a table without losing any data?
A slight improvement to sAeid's excellent answer...
I added an exec to have this code self-execute, and I added a union at the top so that I could change the schema of both tables AND stored procedures:
I too had to restore a dbdump, and found that the schema wasn't dbo - I spent hours trying to get Sql Server management studio or visual studio data transfers to alter the destination schema... I ended up just running this against the restored dump on the new server to get things the way I wanted.
Move table from dbo schema to MySchema:
Move table from MySchema to dbo schema:
See ALTER SCHEMA.
Generalized Syntax:
I had a similar issue but my schema had a backslash in it. In this case, include the brackets around the schema.