By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,763 Members | 1,609 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,763 IT Pros & Developers. It's quick & easy.

How to omit Schema name when referrring to Tables?

P: n/a
Hi guys, please help.

What I did is :
Step1. create a Database named [Test], it's default owner is [sa]
Step2. create a User in Database [Test] named [kimliuTest], which maps
to the Login [kkhad\kimliu] on this Database Server, it's a windows
domain user.
Step3. create a Schema in Database [Test] named [schTest], whose Schema
Owner is User [kimliuTest]
Step4. created two tables: [dbo].[t1] and [schTest].[t2]
Step5. run the script

ALTER USER kimliuTest WITH DEFAULT_SCHEMA = schTest

to set the default schema.

Now my problem is, when I try to run

select * from schTest.t2

, everything is OK, but if I run

select * from t2

the error message says:

Msg 208, Level 16, State 1, Line 1
Invalid object name 't2'.

why? I'm loggin in with my [kkhad\kimliu] windows domain id , Active
Monitor also shows that. but the command does not work?

Yeah, it's a good habit to write the full name of the Table with Schema
name as prefix, however, we did our development in SQL 2000 and trying
to upgrade to SQL2005, so trying to avoid scanning codes and inserting
in thousand of places.

How could we omit the Schema name when referrring to Tables? Thanks.
ps. I posted in another topic days ago, but nobody is replying, when I
tried to bring it forward by replying myself, it failed. please help me
, thanks!

Jan 9 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Because when you do not specify a schema, SQL Server first looks for the
object name under a schema which matches your login (kkhad\kimliu). When it
doesn't find an object matching that name under your schema, then it looks
in the dbo schema. If it doesn't find it there, it throws the error message
you are seeing.

--
Mike
Mentor
Solid Quality Learning
http://www.solidqualitylearning.com
"athos" <at*******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi guys, please help.

What I did is :
Step1. create a Database named [Test], it's default owner is [sa]
Step2. create a User in Database [Test] named [kimliuTest], which maps
to the Login [kkhad\kimliu] on this Database Server, it's a windows
domain user.
Step3. create a Schema in Database [Test] named [schTest], whose Schema
Owner is User [kimliuTest]
Step4. created two tables: [dbo].[t1] and [schTest].[t2]
Step5. run the script

ALTER USER kimliuTest WITH DEFAULT_SCHEMA = schTest

to set the default schema.

Now my problem is, when I try to run

select * from schTest.t2

, everything is OK, but if I run

select * from t2

the error message says:

Msg 208, Level 16, State 1, Line 1
Invalid object name 't2'.

why? I'm loggin in with my [kkhad\kimliu] windows domain id , Active
Monitor also shows that. but the command does not work?

Yeah, it's a good habit to write the full name of the Table with Schema
name as prefix, however, we did our development in SQL 2000 and trying
to upgrade to SQL2005, so trying to avoid scanning codes and inserting
in thousand of places.

How could we omit the Schema name when referrring to Tables? Thanks.
ps. I posted in another topic days ago, but nobody is replying, when I
tried to bring it forward by replying myself, it failed. please help me
, thanks!

Jan 9 '06 #2

P: n/a
athos schrieb:
Hi guys, please help. ALTER USER kimliuTest WITH DEFAULT_SCHEMA = schTest

to set the default schema.

Now my problem is, when I try to run

select * from schTest.t2

, everything is OK, but if I run

select * from t2

the error message says:

Msg 208, Level 16, State 1, Line 1
Invalid object name 't2'.

why? I'm loggin in with my [kkhad\kimliu] windows domain id , Active
Monitor also shows that. but the command does not work?


Please refer to the BOL on CREATE USER / ALTER USER.
There are some restrictions on how DEFAULT_SCHEMA can be used:

http://msdn2.microsoft.com/en-US/ms173463.aspx
hth
Stefka
Jan 9 '06 #3

P: n/a
athos (at*******@gmail.com) writes:
What I did is :
Step1. create a Database named [Test], it's default owner is [sa]
Step2. create a User in Database [Test] named [kimliuTest], which maps
to the Login [kkhad\kimliu] on this Database Server, it's a windows
domain user.
Step3. create a Schema in Database [Test] named [schTest], whose Schema
Owner is User [kimliuTest]
Step4. created two tables: [dbo].[t1] and [schTest].[t2]
Step5. run the script

ALTER USER kimliuTest WITH DEFAULT_SCHEMA = schTest

to set the default schema.

Now my problem is, when I try to run

select * from schTest.t2

, everything is OK, but if I run

select * from t2

the error message says:

Msg 208, Level 16, State 1, Line 1
Invalid object name 't2'.

why? I'm loggin in with my [kkhad\kimliu] windows domain id , Active
Monitor also shows that. but the command does not work?


It should work. See the script below. I create a user without login
to make the script simpler, but it should not matter.

What I think may be the mistake is that you never changed context
to the test user, so you were still dbo when you ran the SELECT.
If you comment out the EXECUTE AS in the script, you will see this
effect.

EXECUTE AS LOGIN = 'sa'
go
CREATE DATABASE athos
go
REVERT
go
USE athos
go
CREATE USER test FROM LOGIN [KESÄMETSÄ\sommar]
-- change to your name here.
GRANT CONTROL TO test
go
CREATE SCHEMA schtest
go
CREATE TABLE dbo.t1 (a int NOT NULL)
CREATE TABLE schtest.t2 (b int NOT NULL)
go
EXECUTE AS user = 'test'
go
ALTER USER test WITH DEFAULT_SCHEMA = schtest
go
SELECT * FROM t1
go
SELECT * FROM t2
go
REVERT
go
USE master
DROP DATABASE athos

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jan 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.