Link to home
Start Free TrialLog in
Avatar of HKComputer
HKComputerFlag for United States of America

asked on

SQL Server Database Import - Schema Name Wrong

First of all, I'll admit I don't know much about SQL Server.

I made a backup of our SQL 2005 database at Network Solutions and had our new webhosting company import the backup. When I connect to the server using SQL Server Management Studio 2008, I can see that each of my tables are prefixed like this: OldDataBaseName.tblTableName. This is going to cause problems in all of my queries.

The new database is named something slightly different than the old one. That's probably why this happened in the first place.

What's the best way to resolve this problem?
Avatar of Ryan McCauley
Ryan McCauley
Flag of United States of America image

You can modify the schema of your objects using a system SP - the attached SQL script will modify all objects from @OldOwner to @NewOwner
declare @OldOwner varchar(100) declare @NewOwner varchar(100) 
set @OldOwner = 'OldOwner' set @NewOwner = 'NewOwner'
 
select 'sp_changeobjectowner ''[' + table_schema + '].[' + 
table_name + ']'', ''' + @NewOwner + ''' go 
 from information_schema.tables where Table_schema = @OldOwner

Open in new window

the above logic is good. there is some syntax error, you can clear up.

I have corrected the syntax here:

DECLARE @OldOwner varchar(100) , @NewOwner varchar(100)
SET @OldOwner = 'OldDataBaseName'  
SET @NewOwner = 'dbo' -- or New Schema Name
SELECT 'sp_changeobjectowner ''[' + table_schema + '].[' +table_name + ']'', ''' + @NewOwner + ''''
FROM information_schema.tables
WHERE Table_schema = @OldOwner
The above is for tables - if you see the ownership problem for procs, then you would need to hit sysobjects

DECLARE @OldOwner varchar(100) , @NewOwner varchar(100)
SET @OldOwner = 'OldDataBaseName'  
SET @NewOwner = 'dbo' -- or New Schema Name
SELECT 'sp_changeobjectowner ''[' + u.[Name] + '].[' +o.[Name] + ']'', ''' + @NewOwner + ''''
FROM dbo.sysobjects o
INNER JOIN dbo.sysusers u
ON o.uid = u.uid
WHERE o.Type = 'P'
AND u.[Name] = 'OldDataBaseName' -- Change OldDataBaseName's Procs to DBO's (or the new schema of choice)
Avatar of HKComputer

ASKER

Thanks, we only need to update the tables. We aren't using any stored procedures. Yet.
OK. Then that script alone should take care of you.

Once those scripts are generated, just copy paste, run one at at time or a bunch at a time, to make sure you are following what is going one or where the error is.

if it there are 100s of them, then you can run them together, and if you do not need to track minutely, then run them all and make sure there are no errors.
OK, I don't know why but the code above didn't work. It ran without errors but it didn't change the schema. The database is a SQL Server 2008 database. Could that have been part of the problem? From what I'm reading, the code recommended above appears to be deprecated.

I ended up using the following code:
ALTER SCHEMA NewSchemaName TRANSFER OldSchemaName.TableName

ASKER CERTIFIED SOLUTION
Avatar of anushahanna
anushahanna
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And I won't say for sure that it won't work in 2008. You'd think if it was totally deprecated I would have gotten an error message and I did not. Instead I got a recordset that contained the schema names and table names but nothing got updated.
HKComputer, actually, the documentation says "This stored procedure only works with the objects available in MicrosoftSQL Server 2000"; but it seemed to work in 2005 for me.

can you please try the below code in SQL 2008 and see if it works for you in the test scenario. it will create a test database and them remove it at the end so you should not have to worry anything about it.
code.txt
I'm sorry but my shared hosting account doesn't give me permissions to create a database. I did try the script but here's the output:

Msg 262, Level 14, State 1, Line 1
CREATE DATABASE permission denied in database 'master'.
Msg 911, Level 16, State 1, Line 1
Database 'pztestdb' does not exist. Make sure that the name is entered correctly.

(1 row(s) affected)
Caution: Changing any part of an object name could break scripts and stored procedures.

(1 row(s) affected)
Caution: Changing any part of an object name could break scripts and stored procedures.
Msg 3701, Level 11, State 1, Line 6
Cannot drop the database 'pztestdb', because it does not exist or you do not have permission.
OK. No problem. Can you create a table? - then pl try this..
IF PERMISSIONS()&2=2

create table dbo.testtable (testcol smallint)
select schema_name(schema_id) from sys.all_objects where name = 'testtable'
create user testuser without login
GO
create schema testuser AUTHORIZATION testuser
GO
exec sp_changeobjectowner '[dbo].[testtable]', 'testuser'
select schema_name(schema_id) from sys.all_objects where name = 'testtable'
exec sp_changeobjectowner '[testuser].[testtable]', 'dbo'
drop schema testuser 
drop table dbo.testtable 

ELSE

   PRINT 'ERROR: No permissions to create a test table'

Open in new window