Why are tables created with default schema dbo although I specified a different schema? - sql

If I run a sql script in SQL Server 2005 SSMS (Version 9.00.4035.00) like
CREATE TABLE xxx.MyTable
the table will be created as dbo.MyTable although the schema xxx does exist! No error message!
The user I'm using to run the script as all permissions (tested with windows user and sql user with server role sysadmin)
What's wrong?

You probably have 2 tables now
xxx.MyTable
dbo.MyTable
To check:
SELECT SCHEMA_NAME(schema_id), name, create_date, modify_date
FROM sys.objects
WHERE name = 'MyTable'
Don't rely on SSMS Object Explorer: it needs refreshed (right click on the tables node, refresh).
Or wrong database, wrong server etc.
We use schemas and never had any problems
Edit: now check all databases
EXEC sp_msforeachdb '
USE ?
SELECT SCHEMA_NAME(schema_id), name, create_date, modify_date
FROM sys.objects
WHERE name = ''MyTable''
'

Please take a look at the possible workarounds:
1) Create a SQL login with dbo rights to the database where tables and other objects have to be created. Have the users connect to SSMS using the SQL login that you have created. Tables can be created using SSMS without issues.
2) Have the user of windows security group create table using TSQL. You will see that a new schema and user will be created for this database with the user name of the user. Table gets created with windows user name as the owner .
Now, go to the database user which got created. Change the default schema to xxx.
User of that security group can create tables in SSMS and with dbo as the object owner.
Apparently, this is a microsoft bug and has not been resolved yet.
https://connect.microsoft.com/feedback/viewfeedback.aspx?FeedbackID=238246&wa=wsignin1.0&siteid=68
Hope this helps.

Related

DBA readonly account

I had a schema in one oracle DB as ui_prod. I asked my DBA team guys to create exactly same schema like ui_prod but as read only and name it ui_prod_readonly. Usually I will use Oracle SQL developer to connect a DB and query directly with table name like below.
--Connect to ui_prod
select * from table
but why I requested to put owner name infront when query for readonly schema they created for me, as without putting it, I get error table not exist.
--Connect to ui_prod_readonly
select * from ui_prod.table
I have project files which hardcode the sql query with only table names and adding owner name in front will cause many changes and effort. Can anyone explain me on this? or provide me any document/link to read. Thanks
You should look into synonyms, apparently the user you are connecting to the database as is not the owner of the objects. So to view the object you have to prepend the names with the schema name (the owner of the object themselves).
http://www.techonthenet.com/oracle/synonyms.php
CREATE OR REPLACE SYNONYM ui_prod_readonly.synonym_name
FOR ui_prod.object_name
It seems to me that your dbas have not created another set of tables but just granted the existing tables to the user ui_prod_readonly.
When you log in to Oracle, the current schema is the name of the user you used to log in. So if you log in with ui_prod_readonly Oracle checks that schema for the table if you do not qualify it with the owner (=schema).
If you want to change the current schema so that you don't need to fully qualify the tables, you can do that with ALTER SESSION
alter session set current_schema = ui_prod;
Once you have done that, you don't need to fully qualify the table with the owner (=schema).
if you need a user to read the data only
its simple to create new user and grant it only select privilege
you can create user and grant select privilege using
CREATE USER [user] IDENTIFIED BY [your_password];
grant select on table to [user]

How to query table with different table owner

I have several tables with defined owner user in a SQL Server 2008 database
a_user.[table1]
a_user.[table2]
When I log in as admin I cannot query either table because it doesn't exist [in that user namespace].
How can I make admin or any other user be able to query table a_user.[table1] without referencing the user?
select * from a_user.table1 --> works
select * from table1 --> doesn't work, i'd like this to work
What you're looking at are schemas, not owners. The two concepts are distinct (from SQL Server 2005 onwards).
To resolve any table name, SQL Server will look in your default schema. By default, this will be dbo. The only way to access a table without specifying a schema name is for it to be in your default schema, or for you to add a synonym for the table within your default schema.

SQL Server creating wrong table names, why?

Question: when I create a table (T_TableName) using SQL Server Management-Studio, it always creates the table as
Domain\UserName.T_TableName
instead of
dbo.T_TableName
What's wrong ?
If you don't specify a schema explicitly on your table name to be created, it will be created in the user's current default schema.
I bet the user you're using has its own personal schema set as its default schema - that's why your tables get created in his own personal schema.
You can check what database users you have and what their default schema is by inspecting sys.database_principals (SQL Server 2005 and up):
SELECT name, type_desc, default_schema_name
FROM sys.database_principals
To solve this:
specify the schema you want to use explicitly (best practice anyway!)
CREATE TABLE dbo.T_TableName
change the user's default schema to dbo
ALTER USER [Domain\YourUser] WITH DEFAULT_SCHEMA = dbo
But as a general rule of thumb, I recommend always using the "dbo." prefix explicitly, if you want to have all your database objects in the dbo schema. Helps with performance, too (ever so slightly) since SQL Server won't have to go hunting in different schemas, if you explicitly tell it where your db objects live.
You need to either create your table as "dbo.Whatever", OR you need to change your default schema (or have your SA do it for you) by issuing a command like:
ALTER USER [DOMAINNAME\UserName] WITH DEFAULT_SCHEMA = dbo;
Call it dbo.T_TableName in SSMS. If you have the correct permissions, it will work.
Are you assigned as db_owner for the database you created the table in? If not, this could be the issue. Try adding your user mapping permissions to the database as such.
USE [yourDatabase]
GO
EXEC sp_addrolemember N'db_owner', N'DOMAIN\UserOrGroup'
GO

SQL server : Login issue while querying

I have a web application running with a SQL server 2005 DB as back end.I took the db back of the production site and restored in my development machine.Then i tried to query this database using the login "sa".When trying to execute the "select * from Customers" query, i am getting a message like "Invalid object name 'Customers"
But when i run "SELECT * FROM [352974_mg4l1].[Customers]", It is returning records.
352974_mg4l1 is a user for this database present when i restored the db backup from production.
What i have to do for getting the records using with simple select query which i used initially("select * from Customers" ). I know it is something related to login issue.Can any one tell me how to solve this ?
The Customers database object is not owned by the dbo schema.
And by referencing Customers as 'sa' you are looking for [dbo].[Customers] ?
I would suggest to:
either provide the object's full name
either change it's schema
Edit:
To alter the schema of said table try this:
ALTER SCHEMA dbo TRANSFER [352974_mg4l1].Customers;
Reference: http://msdn.microsoft.com/en-us/library/ms173423.aspx
Look up sp_changeobjectowner in Books on line. That will help you change the owner. The real question is how the object got created to a specific owner to begin with. If this is true on prod, you could have some major issues with other people accesssing the database.

Drop User from SQL Server Database?

How can I drop user from a database without dropping it's logging?
The script should check if the user exists in database, if does then drop the user.
Is this what you are trying to do??
IF EXISTS (SELECT * FROM sys.database_principals WHERE name = N'username')
DROP USER [username]
If you are using SQL Server Management Studio you can browse to the user and right-click selecting delete.
The accepted answer is working good enough. Additionally that is good to know SQL Server added IF EXIST to some DROP commands from version 2016 (13.x) including 'DROP USER' command.
IF EXISTS
Applies to: SQL Server ( SQL Server 2016 (13.x) through current version, SQL Database).
Conditionally drops the user only if it already exists.
So you could just delete user as below:
-- Syntax for SQL Server and Azure SQL Database
DROP USER IF EXISTS user_name
See the full description in this link: DROP USER (Transact-SQL)
Hope this help.