Solved

SQL Server Database Import - Schema Name Wrong

Posted on 2010-08-31
11
408 Views
Last Modified: 2012-05-10
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?
0
Comment
Question by:HKComputer
  • 6
  • 4
11 Comments
 
LVL 28

Expert Comment

by:Ryan McCauley
ID: 33568696
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

0
 
LVL 6

Expert Comment

by:anushahanna
ID: 33569540
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
0
 
LVL 6

Expert Comment

by:anushahanna
ID: 33569550
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)
0
 
LVL 4

Author Comment

by:HKComputer
ID: 33569561
Thanks, we only need to update the tables. We aren't using any stored procedures. Yet.
0
 
LVL 6

Expert Comment

by:anushahanna
ID: 33569764
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.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 4

Author Comment

by:HKComputer
ID: 33583689
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

0
 
LVL 6

Accepted Solution

by:
anushahanna earned 500 total points
ID: 33585999
hmm... in 2005, it works fine. But you are right: it is the right thing to use the ALTER statement instead..

the attached code will script it out for you.
DECLARE @OldOwner varchar(100) , @NewOwner varchar(100)

SET @OldOwner = 'OldDataBaseName'  

SET @NewOwner = 'dbo' -- or New Schema Name

SELECT 'ALTER SCHEMA '+@NewOwner+' TRANSFER '+ '[' + table_schema + '].[' +table_name + ']'

FROM information_schema.tables

WHERE Table_schema = @OldOwner

Open in new window

0
 
LVL 4

Author Comment

by:HKComputer
ID: 33587707
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.
0
 
LVL 6

Expert Comment

by:anushahanna
ID: 33589090
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
0
 
LVL 4

Author Comment

by:HKComputer
ID: 33589138
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.
0
 
LVL 6

Expert Comment

by:anushahanna
ID: 33609112
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

0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sql query 34 35
today minus 2 years SQL server 2008 2 31
SQL Server Question 5 25
Sql Query Datatype 2 16
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now